This command will get the IP address from the interface supplied and then ping it.
ping $(ifconfig enp6s1 | awk '/inet / { print $2 } ' | sed -e s/addr://) |
Here is an example. This shows how well this works.
ubuntu ~ $ ping $(ifconfig eth0 | awk '/inet / { print $2 } ' | sed -e s/addr://) PING 172.31.20.16 (172.31.20.16) 56(84) bytes of data. 64 bytes from 172.31.20.16: icmp_seq=1 ttl=64 time=0.018 ms 64 bytes from 172.31.20.16: icmp_seq=2 ttl=64 time=0.041 ms 64 bytes from 172.31.20.16: icmp_seq=3 ttl=64 time=0.033 ms 64 bytes from 172.31.20.16: icmp_seq=4 ttl=64 time=0.034 ms 64 bytes from 172.31.20.16: icmp_seq=5 ttl=64 time=0.032 ms ^C --- 172.31.20.16 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 3998ms rtt min/avg/max/mdev = 0.018/0.031/0.041/0.009 ms |
Or you could specify the device to be pinged this way…
ubuntu ~ $ DEVICE='eth0'; ping $(ifconfig $DEVICE | awk '/inet / { print $2 } ' | sed -e s/addr://) PING 172.31.20.16 (172.31.20.16) 56(84) bytes of data. 64 bytes from 172.31.20.16: icmp_seq=1 ttl=64 time=0.018 ms 64 bytes from 172.31.20.16: icmp_seq=2 ttl=64 time=0.030 ms 64 bytes from 172.31.20.16: icmp_seq=3 ttl=64 time=0.034 ms 64 bytes from 172.31.20.16: icmp_seq=4 ttl=64 time=0.044 ms 64 bytes from 172.31.20.16: icmp_seq=5 ttl=64 time=0.032 ms 64 bytes from 172.31.20.16: icmp_seq=6 ttl=64 time=0.033 ms ^C --- 172.31.20.16 ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 4998ms rtt min/avg/max/mdev = 0.018/0.031/0.044/0.010 ms |
Anyway, this is a useful one-liner script to ping a device. Another related command is this one. The ioping command can ping a hard drive and tell you if the data packets were returned and the time taken to send and receive them. I am not sure why you would use this command, but here it is if you want it.
ubuntu ~ $ ioping /home/ubuntu/ 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=1 time=275 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=2 time=414 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=3 time=377 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=4 time=426 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=5 time=407 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=6 time=373 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=7 time=392 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=8 time=373 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=9 time=287 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=10 time=400 us 4.0 KiB from /home/ubuntu/ (ext4 /dev/xvda1): request=11 time=393 us ^C --- /home/ubuntu/ (ext4 /dev/xvda1) ioping statistics --- 11 requests completed in 10.3 s, 2.7 k iops, 10.4 MiB/s min/avg/max/mdev = 275 us / 374 us / 426 us / 46 us |