Weblog entry #8 for fugit
A Happy Friday post about fun one liners. Please post any one liners you have created that probably should have been a short shell script or any improvements for the below one liner. One improvement would be to get rid of the expr output.
Below was something I wrote just to quickly find if I had retired all the hosts that should have been retired from a txt file. I also used a slight variant of the code to check if DNS had been deleted.
UPDATE:
The reason for the expr was I has having problems nesting the if statements inside the backticks. After mcortese comment I updated the script to use an if statement outside of the backticks. RJC provided an alternative to backticks using posix compliant "$()". I have updated the script but did not nest the if statements which would be easier with "$()".
One Liner for x in `grep KILL list.txt | awk '{ print $6 }'` ; do add=`host $x | grep -v NXDOMAIN | awk '{ print $4 }'` ; if [ "$add" ]; then ping -c1 -W1 $add >/dev/null; expr 1 / $? 2>/dev/null || echo $x ; fi ; done
Example txt from my file below. The servers were all local so I was able to reduce the timeout. In order for the above code to work you will need to be able to lookup the hostname and ping the ip returned:
Text File KILL 300 62 running 10.200.5.25 woody.example.com
KILL 504 65 running 10.200.5.26 potato.example.org
505 15 running 10.200.4.27 etch.example.net
KEEP 506 29 running 10.200.3.28 squeeze.example.co.uk
KILL 511 28 running 10.200.3.29 hamm.example.info
KILL 525 30 running 10.200.1.69 bo.example.xxx
KEEP 526 24 running 10.200.2.254 wheezy.example.tv
The Improved One Liner Based on Comments: for x in $(awk '/KILL/{ print $6 }' /tmp/list.txt ) ; do add=$(host $x | awk '!/NXDOMAIN/{ print $4 }') ; if [ "$add" ]; then ping -c1 -W1 $add >/dev/null; res="$?"; fi; if [ "$res" -eq 0 ] ;then echo $x ; fi ; done
Comments on this Entry
[ Parent | Reply to this comment ]
if ping... ; then echo... ; fi
[ Parent | Reply to this comment ]
[0] http://mywiki.wooledge.org/BashFAQ/082
[1] http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap 02.html#tag_02_06_03
My $0.02
rjc
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
property Id's are bogus....
replace the -- in h--p for http.
for washington county, oregon.
what really was beautiful: having 3 monitors to stretch the ssh window across when writing it!!!
returns (Address) (Year built) (Square Footage)
replace "1N230DB01700" with the appropriate property tax lot id#
wget -q -O - "h--p://washims.co.washington.or.us/gis/ispirits/arcims_proc ess.cfm?IDValue=%271N230DB01700%27&DisplayValue=1&searchs elect=taxmap" > temp.txt && (cat temp.txt | grep 'Site Address' -A 2 | html2 | sed -n 6p | cut -d\= -f 2 && cat temp.txt | grep 'Site Address">' -A 2 | html2 | sed -n 5p | cut -d\= -f 2 && cat temp.txt | grep 'Year Built' -A 2 | html2 | sed -n 5p | cut -d\= -f 2 && cat temp.txt | grep 'Bldg Sq Ft:' -A 2 | html2 | sed -n 6p | cut -d\= -f 2) > cur.txt && tr -d '\n' < cur.txt
For Multnomah County, Oregon:
replace "1N2E23DD%20%209500" with appropriate tax lot #
ID must be in format 1N2E23DD 9500 (2 spaces = %20%20)
I was pulling from excel so I used =LEFT(AH4,8)&"%20%20"&MID(AH4,9,9)
wget -q -O - "h--p://www.portlandmaps.com/detail.cfm?action=assessor& &gmapapiversion=&state_id=1N2E23DD%20%209500" > temp.txt | echo -ne "Address:"&& cat temp.txt | grep 'Site Address</td><td class="data">' | cut -d\> -f 2- | sort | uniq | html2 2>/dev/nul | grep 'td=' | cut -d\= -f 2- && echo -ne "Year Built: " && cat temp.txt | grep 'Actual Year Built</td>' -A 3 | cut -d\> -f 2- | sort | uniq | html2 2>/dev/nul | cut -d\ -f 1 | cut -d\= -f 2 && echo -ne "Main Area: "&& cat temp.txt | grep 'Main' -A 2 | html2 | sed -n 10p | cut -d\= -f 2
[ Parent | Reply to this comment ]
$add is empty: the then block is not executed, so $res is not set and it keeps the value from the previous iteration. When you then test it against 0, you are actually testing the old value. In your example, if woody is pinged with success, res is set to 0, and "woody" is echoed. Then it's potato's turn: if the host|awk command fails to produce a non-null output, "potato" is echoed nonetheless.
I don't understand what problem you have with a simple
if ping; then echo; fiYou could even combine the two
if like this: if [ "$add" ] && ping -c1 -W1 "$add"; then echo $x; fi
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
www.bashoneliners.com
It's a public collection of cool bash one-liners, you can post your own or just enjoy reading the wisdom of others.
[ Parent | Reply to this comment ]
- grep KILL list.txt | awk '{ print $6 }'
+ awk '/KILL/{ print $6 }' list.txt
- grep -v NXDOMAIN | awk '{ print $4 }'
+ awk '!/NXDOMAIN/{ print $4 }'
[ Parent | Reply to this comment ]