shell脚本练习9———— 抓取主机ip

ifconfig 网卡可以显示此网卡的信息 显示信息中包含此网卡使用的ip地址 请用命令过滤此ip并在输出时只显示ip其他信息不显示

[root@1 mnt]# ifconfig ens160
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.12  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::3d25:939e:7c9b:9354  prefixlen 64  scopeid 0x20<link>
        inet6 2409:8970:3ac0:98bf:8c62:3e5e:5dc6:a83f  prefixlen 64  scopeid 0x0<global>
        ether 00:0c:29:0a:16:0a  txqueuelen 1000  (Ethernet)
        RX packets 4324  bytes 3233423 (3.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1252  bytes 101047 (98.6 KiB)

grep过程中,inet空格,以及inet>(inet后是空格) /<inet (inet前是空格)
1

[root@1 mnt]# ifconfig ens160 | grep "inet " | cut -c 14-26
192.168.3.12 

2

[root@1 mnt]# ifconfig ens160 | cut -c 14-25 | head -n 2 | tail -n 1
192.168.3.12

3

[root@1 mnt]# ifconfig ens160 | grep "inet " | cut -d " " -f 10
192.168.3.12

4

[root@1 mnt]# ifconfig ens160 | awk '/inet\>/{print $2}'  ##inet后面是空格
192.168.3.12
[root@1 mnt]# ifconfig ens160 | awk '/\<netmask/ {print $2}'  ##netmask 前面是空格
192.168.3.12

5

[root@1 mnt]# cat catch_ip.sh
#!/bin/bash
[ -z "$*" ] && {
    
    
	echo erro:no device
        exit
}
ifconfig $* &>/dev/null || {
    
    
	echo "$* have not ip "
        exit
}
ifconfig $* | awk '/inet\>/{print $2}'

猜你喜欢

转载自blog.csdn.net/ninimino/article/details/108910355