需求:取出某个网卡IP地址,并把地址赋值给变量:
当前操作系统版本:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
ifconfig查看当前网卡信息:
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.92.78 netmask 255.255.255.0 broadcast 192.168.92.255
inet6 fe80::2801:f5c2:4e5a:d003 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:46:22:22 txqueuelen 1000 (Ethernet)
RX packets 248 bytes 27453 (26.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 165 bytes 19585 (19.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.92.138 netmask 255.255.255.0 broadcast 192.168.92.255
inet6 fe80::20c:29ff:fe46:222c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:46:22:2c txqueuelen 1000 (Ethernet)
RX packets 100259 bytes 142408805 (135.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 23971 bytes 2507885 (2.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip a 查看当前网卡信息:
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:46:22:22 brd ff:ff:ff:ff:ff:ff
inet 192.168.92.78/24 brd 192.168.92.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::2801:f5c2:4e5a:d003/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:46:22:2c brd ff:ff:ff:ff:ff:ff
inet 192.168.92.138/24 brd 192.168.92.255 scope global noprefixroute dynamic ens37
valid_lft 977sec preferred_lft 977sec
inet6 fe80::20c:29ff:fe46:222c/64 scope link
valid_lft forever preferred_lft forever
取出某个网卡对应的IP地址
方法1:
[root@localhost ~]# ifconfig | grep broadcast | awk "NR==1" | awk -F" " '{print $2}'
192.168.92.78
[root@localhost ~]# ifconfig | grep broadcast | awk "NR==2" | awk -F" " '{print $2}'
192.168.92.138
方法2:
[root@localhost ~]# ip a | grep -w "inet" | grep ens33 |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'
192.168.92.78
方法3:
[root@localhost ~]# ifconfig | grep inet | head -n 1 | awk -F" " '{print $2}'
192.168.92.78
方法4
ip a show dev ens33|grep -w inet|awk '{print $2}'|sed 's/\/.*//'
ip a show dev ens33|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'
ip a | grep ens33 | grep inet | awk '{print $2}' |sed 's/\/.*//'
把取出的IP地址赋值给变量
[root@localhost ~]# ip=$(ip a | grep ens33 | grep inet | awk '{print $2}' |sed 's/\/.*//')
[root@localhost ~]# echo $ip
192.168.92.78