cygwin - bash script to read/ping a list of ip addresses from a text file and then write the results to another file? -
i have text file lists of ip addresses called address.txt contains following
172.26.26.1 wlan01 172.26.27.65 wlan02 172.26.28.180 wlan03
i need write bash script reads ip addresses, ping them , output text file this:
172.26.26.1 up
172.26.27.65 down
172.26.28.180 down
i new bash scripting not sure start this. appreciated.
in linux work:
awk '{print $1}' < address.txt | while read ip; ping -c1 $ip >/dev/null 2>&1 && echo $ip || echo $ip down; done
i don't have cygwin test, should work there too.
explanation:
- with
awk
first column input file , pipe loop - we send single
ping
$ip
, , redirect standard output , standard error/dev/null
doesn't pollute our output - if
ping
successful, command after&&
executed:echo $ip up
- if
ping
fails, command after||
executed:echo $ip down
somewhat more readable, expanded format, put in script:
#!/bin/sh awk '{print $1}' < address.txt | while read ip; if ping -c1 $ip >/dev/null 2>&1; echo $ip else echo $ip down fi done
Comments
Post a Comment