Do you use let's encrypt?





7690 votes ~ 25 comments

 

XML logo

Bash OneLiners (for fun)
Posted by fugit on Fri 17 Feb 2012 at 15:44
Tags: none.

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

Re: Bash OneLiners (for fun)
Posted by Anonymous (140.221.xx.xx) on Fri 17 Feb 2012 at 16:51
Gotta love awk =)

- 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 ]

Re: Bash OneLiners (for fun)
Posted by fugit (199.2.xx.xx) on Fri 17 Feb 2012 at 17:01
[ View Weblogs ]
Great improvements, I always forget to use the full power of AWK :)

[ Parent | Reply to this comment ]

Re: Bash OneLiners (for fun)
Posted by mcortese (85.158.xx.xx) on Mon 20 Feb 2012 at 09:07
[ View Weblogs ]
What's the reason for the complex contruct with expr? If it's just to test the exiti status of the ping, why not a simpler if?
if ping... ; then echo... ; fi

[ Parent | Reply to this comment ]

command substitution
Posted by rjc (131.111.xx.xx) on Mon 5 Mar 2012 at 14:09
While using backticks or backquotes ('`') for command substitution goes back as far as Bourne Shell, a much more elegant way [0], IMHO, is to use '$()'. It is defined by POSIX [1] so a shell conforming to it should support it (dash being the default /bin/sh does).

[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 ]

Re: command substitution
Posted by fugit (199.2.xx.xx) on Mon 5 Mar 2012 at 18:57
[ View Weblogs ]
Great idea RJC, the links points out a lot of good reasons to use "$()" instead of backticks. The ones I really like are readability and ease of nesting. Thanks, Keith.

[ Parent | Reply to this comment ]

Re: Bash OneLiners (for fun) -using wget, cat,echo, grep, cut, html2, &
Posted by Anonymous (24.21.xx.xx) on Thu 15 Mar 2012 at 06:42
Here's one I just came up with to return property information from local GIS.
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 ]

Re: Bash OneLiners (for fun)
Posted by mcortese (85.158.xx.xx) on Fri 16 Mar 2012 at 11:47
[ View Weblogs ]
The new version doesn't work correctly when $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; fi
You could even combine the two if like this:
if [ "$add" ] && ping -c1 -W1 "$add"; then echo $x; fi

[ Parent | Reply to this comment ]

Re: Bash OneLiners (for fun)
Posted by fugit (199.2.xx.xx) on Wed 21 Mar 2012 at 21:12
[ View Weblogs ]
Thanks for the comment, I'll take look hopefully sooner rather then later at updating the one liner :)

[ Parent | Reply to this comment ]

Re: Bash OneLiners (for fun)
Posted by Anonymous (90.46.xx.xx) on Wed 21 Mar 2012 at 17:46
You guys just loooove bash one-liners don't you. Then you might like this site I recently created:
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 ]