Which terminal command to get just IP address and nothing else?

后端 未结 29 1094
春和景丽
春和景丽 2020-12-04 06:16

I\'m trying to use just the IP address (inet) as a parameter in a script I wrote.

Is there an easy way in a unix terminal to get just the IP address, rather than loo

相关标签:
29条回答
  • 2020-12-04 06:39
    ip addr|awk '/eth0/ && /inet/ {gsub(/\/[0-9][0-9]/,""); print $2}'
    

    shows all your ips

    0 讨论(0)
  • 2020-12-04 06:42

    Few answers appear to be using the newer ip command (replacement for ifconfig) so here is one that uses ip addr, grep, and awk to simply print the IPv4 address associated with the wlan0 interface:

    ip addr show wlan0|grep inet|grep -v inet6|awk '{print $2}'|awk '{split($0,a,"/"); print a[1]}'
    

    While not the most compact or fancy solution, it is (arguably) easy to understand (see explanation below) and modify for other purposes, such as getting the last 3 octets of the MAC address like this:

    ip addr show wlan0|grep link/ether|awk '{print $2}'|awk '{split($0,mac,":"); print mac[4] mac[5] mac[6]}'
    

    Explanation: ip addr show wlan0 outputs information associated with the network interface named wlan0, which should be similar to this:

    4: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether dc:a6:32:04:06:ab brd ff:ff:ff:ff:ff:ff
        inet 172.18.18.1/24 brd 172.18.18.255 scope global noprefixroute wlan0
           valid_lft forever preferred_lft forever
        inet6 fe80::d340:5e4b:78e0:90f/64 scope link 
           valid_lft forever preferred_lft forever
    

    Next grep inet filters out the lines that don't contain "inet" (IPv4 and IPv6 configuration) and grep -v inet6 filters out the remaining lines that do contain "inet6", which should result in a single line like this one:

        inet 172.18.18.1/24 brd 172.18.18.255 scope global noprefixroute wlan0
    

    Finally, the first awk extract the "172.18.18.1/24" field and the second removes the network mask shorthand, leaving just the IPv4 address.

    Also, I think it's worth mentioning that if you are scripting then there are often many richer and/or more robust tools for obtaining this information, which you might want to use instead. For example, if using Node.js there is ipaddr-linux, if using Ruby there is linux-ip-parser, etc.

    See also https://unix.stackexchange.com/questions/119269/how-to-get-ip-address-using-shell-script

    0 讨论(0)
  • 2020-12-04 06:42

    Use the following command:

    /sbin/ifconfig $(netstat -nr | tail -1 | awk '{print $NF}') | awk -F: '/inet /{print $2}' | cut -f1 -d ' '
    
    0 讨论(0)
  • 2020-12-04 06:42

    use this one line script: ifconfig | grep "inet " | grep -v 127.0.0.1|awk 'match($0, /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {print substr($0,RSTART,RLENGTH)}' mac & linux (tested in ubuntu) both works.

    0 讨论(0)
  • 2020-12-04 06:43

    In man hostname there is even more easier way which automatically excluding loopback IP and showing only space separated list of all assigned to host ip addresses:

    root@srv:~# hostname --all-ip-addresses
    11.12.13.14 192.168.15.19 
    
    root@srv:~# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
       link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
       inet 127.0.0.1/8 scope host lo
       inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
    2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
      link/void 
      inet 11.12.13.14/32 scope global venet0:0
      inet 192.168.15.19/32 scope global venet0:1
    
    0 讨论(0)
  • 2020-12-04 06:43

    On Redhat 64bit, this solved problem for me.

    ifconfig $1|sed -n 2p|awk '{ print $2 }'|awk -F : '{ print $2 }'
    
    0 讨论(0)
提交回复
热议问题