Obtain the Intranet Internet IP address in Linux

Source: Internet
Author: User
ArticleDirectory
    • 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

Original

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/IfconfigEth0| Grep "Inet ADDR" | Awk-F:'{Print $2 }' | Awk '{Print $1 }'10.20.10.1

Create simple bash function (exampleINT-IP) With following command.

FunctionINT-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 (exampleINT-IPS) With following command.

FunctionINT-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

Verified

Create simple bash function (exampleEXT-IP) With following command.

FunctionEXT-IP() { Lynx -- DumpHTTP://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 (exampleEXT-IP) With following command.

FunctionEXT-IP() {Curl http://Ipecho.net/Plain;Echo;} # Example usage ##Ext-ip80.10.10.80
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.