linux 得到內網外網ip

來源:互聯網
上載者:User
文章目錄
  • 1.1 Get Single IP Address by Interface
  • 1.2 Get Every Interfaces IP Address
  • 2.1 Get External IP Address Using Lynx
  • 2.2 Get External IP Address Using Curl

原文

1. Get Internal IP Address(es) on Linux Shell / Command Line1.1 Get Single IP Address by Interface

Returns plain IP address.

/sbin/ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}' ## Example usage ##/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'10.20.10.1

Create simple bash function (example int-ip) with following command.

function int-ip { /sbin/ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'; } ## Example usage ##int-ip eth010.20.10.1
 1.2 Get Every Interfaces IP Address

Returns every interface and IP address pairs.

/sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }' ## Example output ##eth0: 10.20.10.1eth1: 10.20.1.168lo: 127.0.0.1

Create simple bash function (example int-ips) with following command.

function int-ips { /sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'; } ## Example usage ##int-ipseth0: 10.20.10.1eth1: 10.20.1.168lo: 127.0.0.1
 2. Get External IP Address on Linux Shell / Command Line

I use here whatismyip.org service.

2.1 Get External IP Address Using Lynx

Returns plain IP address.

lynx --dump http://ipecho.net/plain ## Example output ##80.10.10.80

已驗證

Create simple bash function (example ext-ip) with following command.

function ext-ip () { lynx --dump http://ipecho.net/plain; } ## Example usage ##ext-ip80.10.10.80
 2.2 Get External IP Address Using Curl

Returns plain IP address.

curl http://ipecho.net/plain; echo ## Example output ##80.10.10.80

Create simple bash function (example ext-ip) with following command.

function ext-ip () { curl http://ipecho.net/plain; echo; } ## Example usage ##ext-ip80.10.10.80
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.