# 取得本機的IP與連線網際網路的IP(NAT)

## 先熟悉以下幾個指令

```
sudo apt install wget html2text dnsutils

ip route get 8.8.8.8
ip r s default | cut -d " " -f 3
dig +short myip.opendns.com @resolver1.opendns.com
wget -U Mozilla/5.0 -qqO- \
'https://myip.com.tw' | html2text
```

當你的電腦在 NAT 主機的後面，你需要知道你使用的內部 private IP 與連線網際網路時所需的 external IP 時，這些指令很有用。

寫成 shell 程式

```bash
!/bin/bash
#author:kawsing
GW=$(ip r s default | cut -d " " -f3)
ext_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
IP=$(ip -o route get "8.8.8.8" 2>/dev/null | sed -e 's/^.* src \([^ ]*\) .*$/\1/')  
echo "我的主機 IP 是 ${IP}"
echo "我的主機閘道 IP 是 ${GW}"
echo "我的WAN IP 是 ${ext_IP}"

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kawsing.gitbook.io/opensystem/andoid-shou-ji/untitled-4/shell-script/qu-de-ben-ji-de-ip-yu-lian-xian-wang-ji-wang-lu-de-ipnat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
