Linux 獲取當前IP

2021-07-05 18:56:17 字數 1913 閱讀 9843

linux:

lc_all=c ifconfig

|grep

'inet addr:'

|grep

-v'127.0.0.1'

|cut -d: -f2

|awk

''lc_all=c 英文輸出

ifconfig 輸出ip相關資訊

# lc_all=c ifconfig

eth0 link encap:ethernet hwaddr c2:ac:c3:e7:4a:33inet addr:192.168.1.247 bcast:0.0.0.0 mask:255.255.255.0up broadcast running multicast mtu:1500 metric:1rx packets:84080 errors:0 dropped:80overruns:0 frame:0

tx packets:3595264errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000rx bytes:14627750 (13.9 mib) tx bytes:417627051 (398.2mib)

interrupt:27lo link encap:local loopback

inet addr:127.0.0.1 mask:255.0.0.0up loopback running mtu:16436 metric:1rx packets:127errors:0 dropped:0 overruns:0 frame:0

tx packets:127errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

rx bytes:18079 (17.6 kib) tx bytes:18079 (17.6 kib)

grep 

'inet addr:' 擷取包含ip的那兩行

# lc_all=c ifconfig|grep "inet addr:"

inet addr:192.168.1.247 bcast:0.0.0.0 mask:255.255.255.0inet addr:127.0.0.1 mask:255.0.0.0

grep

-v'127.0.0.1' 去掉本地指向的那行

# lc_all=c ifconfig|grep "inet addr:"|grep -v "127.0.0.1"

inet addr:192.168.1.247 bcast:0.0.0.0 mask:255.255.255.0

cut -d: 

-f2 -d: 以:分割字串 -f2:取第二組資料

# lc_all=c ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2

192.168.1.247 bcast

awk 

'' $1 表示預設以空格分割的第一組 同理 $@表示第二組

# lc_all=c ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2|awk ''

192.168.1.247

# lc_all=c ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2|awk ''

bcast

yii 獲取當前ip

當前網域名稱 除網域名稱外的url 除網域名稱外的首頁位址 除網域名稱外的根目錄位址 假設我們當前頁面的訪問位址是 http localhost cms public index.php?r news id 1 一 結果 http localhost 結果 cms public index.php?...

獲取當前人IP位址

獲取訪問的ip位址 date 2018年11月26日上午11 31 49 對於通過多個 的情況,第乙個ip為客戶端真實ip,多個ip按照 分割 if ipaddress null ipaddress.length 15 return ipaddress 只需要把request的引數傳進去就可以拿到i...

獲取iOS裝置當前ip位址

第一種方法是用系統api的方式獲取,如下 include include include include include include include include include include include include include include include getaddres...