差異處
這裏顯示兩個版本的差異處。
Both sides previous revision 前次修改 下次修改 | 前次修改 | ||
linux:shell_script:get_local_ip [2016/07/02 10:29] tony [Get IP Address] |
linux:shell_script:get_local_ip [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 19: | 行 19: | ||
* /sys/class/net/eth0/carrier如果在interface down的情況下,會出現如下圖錯誤;因此請先up後再做判斷。 | * /sys/class/net/eth0/carrier如果在interface down的情況下,會出現如下圖錯誤;因此請先up後再做判斷。 | ||
{{:linux:shell_script:check_carrier_when_nic_down.png|}} | {{:linux:shell_script:check_carrier_when_nic_down.png|}} | ||
- | ===== Get IP Address ===== | + | ===== Get IPv4 and IPv6 Address ===== |
- | 許多教學都是透過ifconfig去達成,由於我們script需要能夠執行於Ubuntu Server的安裝環境下,因此我使用ip command。 | + | 許多教學都是透過ifconfig去達成,由於我們script需要能夠執行於Ubuntu Server的安裝環境下(無法用cut與awk),因此我使用ip command。 |
<code bash> | <code bash> | ||
# 可在Ubuntu Server 14.04安裝環境下使用 | # 可在Ubuntu Server 14.04安裝環境下使用 | ||
行 28: | 行 28: | ||
<code bash> | <code bash> | ||
ip addr show eth0 | ip addr show eth0 | ||
+ | </code> | ||
+ | ipv6比較難寫,我改使用以下方法,將不需要的部分去頭去尾: | ||
+ | <code bash> | ||
+ | ip=`ip addr show eth0 | grep -o 'inet6 .*'` | ||
+ | ip=${ip%%/*} | ||
+ | ip=${ip#inet6 } | ||
</code> | </code> | ||
===== Reference ===== | ===== Reference ===== | ||
* [[http://linux.vbird.org/linux_basic/0320bash.php|鳥哥 - 10.2.8 變數內容的刪除、取代與替換]] | * [[http://linux.vbird.org/linux_basic/0320bash.php|鳥哥 - 10.2.8 變數內容的刪除、取代與替換]] | ||
- | * [[http://unix.stackexchange.com/questions/183760/printing-interface-ipaddress-through-single-command-piped|Stackexchange - Printing interface-ipaddress through single command ]] | + | * [[http://unix.stackexchange.com/questions/183760/printing-interface-ipaddress-through-single-command-piped|StackExchange - Printing interface-ipaddress through single command ]] |
+ | * [[http://unix.stackexchange.com/questions/132621/check-for-plugged-in-network-cable-on-startup|StackExchange - Check for plugged in network cable on startup]] | ||
===== ===== | ===== ===== | ||
---- | ---- |