All posts in the Awesome One-Liners category

Check an SSL Cert Against Key

In case you ever need to check an SSL Cert against a Key to make sure you’ve got the right pair here’s a super minified bash one liner I wrote:

sck(){ c=`echo -e “$1\n$2″|grep -o ‘.*.crt’`;k=`echo -e “$1\n$2″|grep -o ‘.*.key’`;x=`(o(){ openssl $1 -noout -modulus -in $2|openssl md5;};o x509 $c;o rsa $k)|uniq|wc -l`;(( $x – 1 ))&&echo “Fail”||echo “Pass”;}

And yes that is a function in a subshell in a function. o() was built to save about 30 characters, and whitespace was cut wherever possible in the name of the shortest piece of code to accomplish the task. The only output is Pass/Fail, but if you’re the kind of person that would know where to copy paste something like this you’re probably also the kind of person who doesn’t need 20 different colors and an animated ASCII gif in your command output.

One other cool feature lies in:

c=`echo -e “$1\n$2″|grep -o ‘.*.crt’`;k=`echo -e “$1\n$2″|grep -o ‘.*.key’`

This little gem means that it doesn’t care which position the key and cert are at in the parameters ie:

sck supercooldomain.com.crt supercooldomain.com.key

Is equally valid in comparison with:

sck supercooldomain.com.key supercooldomain.com.crt

There it is, hopefully someone else finds it useful.

Test An SSH Connection

In a script I was writing earlier this afternoon I needed a quick way to test an SSH connection for full functionality. Full functionality being the ability to actually login and use the service as opposed to the port simply being open.

I stumbled across this one liner and incorporated it into my script:

ssh -q -o “BatchMode=yes” user@host “echo 2>&1″ && echo “Up” || echo “Down”

Lets break it down.

ssh -q

The -q option tells SSH to suppress it’s output.

-o “BatchMode=yes”

As defined by man ssh_config batch mode means:

If set to “yes”, passphrase/password querying will be disabled. This option is useful in scripts and other batch jobs where no user is present to supply the password. The argument must be “yes” or “no”. The default is “no”.

&& echo “Up” || echo “Down”

The && means that if our ssh connection completes and exits correctly it will echo “Up”. The || stands for OR, if the login did not complete successfully it will execute the code behind it, in this case echo “Down”

This is a nifty one liner to keep in your toolbox, you can do a lot with the concepts inside of it. The next time you need to test a connection try using the same logic, its transferable to almost any service. Something else to think about is setting up ssh keys so the entire process is as painless as possible, I tend to do this whenever I build a new box.

Move All Directories to a Specific Directory

In my never ending quest to keep my downloads folder clean I’ve found it beneficial to simply sort everything based on file type and then go through the categories manually. For example jpg, gif, bmp would all go in the downloads/pics folder. txt, doc, odt would all go in the downloads/docs folder. And directories would go in the downloads/dirs folder. It’s much easier to sort through months worth of random downloading when you have broad categories to work with.

The command looks like:

ls –color=never -1d */ | xargs -I file mv file dirs

Basically it builds a list of all directories in your current working directory with one per line then moves them to a directory called dirs.

Explanation:

ls –color=never -1d */

This builds our directory list. The –color=never makes sure that if you’re using colored ls output ( who isn’t? ) it’s omitted so there are no extraneous ascii characters. The -1 ( that’s a ONE ) means one entry per line.

xargs -I file mv file dirs

Xargs takes the input and iterates through it. The -I option renames what is essentially the variable representing the line of output that is currently being processed from “{}” which is the xargs standard, to “file” which just makes it a little more readable.

In the odd situation where you need this hopefully this post serves you well.

View Your IP Geolocation in Google Maps – One Liner

This little one-liner will automatically find the coordinates of your public IP address and display them in google chrome in web app mode.

curl -s http://geoiplookup.wikimedia.org/|awk -F, ‘{print $3,$4}’|awk -F’”‘
‘{print “http://mm/maps?q=”$4 “,” $8}’ > .t && google-chrome –app=`cat .t` ; rm .t

Shown across two lines to not mangle, run as one.

It’s a little inelegant with the writing to a file but I found that if you try to pipe the data straight to chrome the response is not fast enough and the url is not inserted correctly.

Thanks to j_melis over on commandlinefu for the initial code that I simply added on to.

Find Your Public IP from the Linux Command Line

Here’s another short ass-kicking tip.

Whenever you’re wondering what the public IP of the box your working on is, whether it’s to perform a scan, correctly point DNS, forward ports or any of the other many reasons one needs to find a public IP try this:

curl icanhazip.com

That’s it. If you go to http://www.icanhazip.com in a browser you’ll see it just spits out your IP in plaintext.

It’s no beautiful 10000 character one liner but it’s amazingly helpful to remember when you need it ( and you will, trust me ).

Quickly Find Thinkpad CPU Temp Linux

This one is a little specific but I needed it this morning so I made a quick one liner for it that I thought I would share.

tc=`tc=`cat /proc/acpi/ibm/thermal | awk ‘{ print $2 }’`;tf=$(echo “scale=2;((9/5) * $tc) + 32″ |bc) echo “CPU TEMP => $tf F”

Pretty simple but I’ll break it out for you anyway.

cat /proc/acpi/ibm/thermal

This proc file contains quick readings for each thermal sensor, you can check out the listings for them over on thinkwiki: http://www.thinkwiki.org/wiki/Thermal_Sensors. A reading of -128 means a disconnected sensor.

awk ‘{ print $2 }’`

Print the second collumn, I happen to know this is the CPU Temp colloum.

tf=$(echo “scale=2;((9/5) * $tc) + 32″ |bc) echo “CPU TEMP => $tf F”

Use bc to convert the C reading to F which makes more sense to me, then output a pretty reading.

As you can see it’s a pretty simple little one liner. This is obviously specific to a Thinpad T400 but it would work the same with any other machine, just sub the correct /proc file for your model.

Disk usage from du/df with colored graph output

Here’s a nifty little command that I picked up off my commandlinefu.com RSS feed a couple weeks back for colored graph of du

t=$(df|awk ‘NR!=1{sum+=$2}END{print sum}’);du / –exclude /proc –exclude /sys –max-depth=1|sed ‘$d’|sort -rn -k1 | awk -v t=$t ‘OFMT=”%d” {M=64; for (a=0;a<$1;a++){if (a>c){c=a}}br=a/c;b=M*br;for(x=0;x<b;x++) {printf “\033[1;31m” “|” “\033[0m”}print ” “$2″ “(a/t*100)”% total”}’

Run As Root! This is meant to be used as a one-liner, it’s broken up here to make it readable on the page

Yea it’s a long one. Basically it takes the output from df and du / ( I’ve added some exclusions for /proc and /sys to quiet down the errors ) sorts it, and outputs a rudimentary ASCII graph showing the percentage of disk usage for / level directories ( home, etc, opt, tmp ).

You can change the directories it scans by tweaking the du c0mmand at the start, and you can also tweak the colors the graph outputs by tinkering with these:  printf “\033[1;31m” “|” “\033[0m”}.

Here’s a quick list of ANSI/bash color codes to use:

Black 0;30

Dark Gray 1;30

Blue 0;34

Light Blue 1;34

Green 0;32

Light Green 1;

32 Cyan 0;36

Light Cyan 1;

36 Red 0;31

Light Red 1;31

Purple 0;35

Light Purple 1;35

Brown 0;33

Yellow 1;33

Light Gray 0;37

White 1;37

Test it out in a terminal, if you don’t find it useful I guarantee you’ll find it at least mildly entertaining.