Saturday, October 31, 2015

Simple One-liners

In this post I thought I would share some one-liners that I've have used in the past that were useful for a given task. Although limited in usefulness and in most cases not even the most elegant way to accomplish a task, hopefully someone might find them useful. I plan on updating this post with more in time and am not planning to keep it specific to any OS, shell or language although today most are bash.

*NIX  Shell: Create pseudo random string
Produce a 32 character string using only the base64 character set. Remove the pipe to base64 to increase the character set and change "32" to whatever output length you want:


echo $(head -c 32 /dev/random | base64 | head -c 32)


*NIX  Shell: Loop through pseudo random input and produce pseudo random output
Simple for loop, change 250 to the amount of output lines you want and change the "3" to a number representing the length of the output. Remove the pipe to base64 to increase the character set.


 for ((i=1; i<=250; i++)); do echo `tr -dc '[:alnum:][:punct:]' < /dev/urandom | base64| head -c 3`; done >> file




*NIX  Shell: Sed replace newlines with | for easy input into egrep
Use this when you have a list of strings, one per line in a given file, that you want to use in an egrep search. The egrepfile might look like:
First
Second
Third
Fourth
The output should look like First|Second|Third|Fourth
Use the output file as input for egrep: cat log | egrep 'First|Second|Third|Fourth'


cat egrepfile | sed ':a;N;$!ba;s/\n/|/g' >> egrepable



*NIX  Shell: Grep for specific type of string
Use a regex search in grep to find a specific length string consisting of specific characters, sort and output only unique then count the amount of matching strings:


cat access_log | egrep -o '[0-9A-Z]{16}\b' | sort -u | wc -l


*NIX  Shell: Use ffmpeg to convert mp4 to mp3
This is nice because it loops through all mp4s in a directory and replaces the .mp4.mp3 extension to just .mp3


for x in *.mp4; do ffmpeg -i $x -q:a 0 -map a "`basename "$x" .mp4`.mp3"; done


*NIX  Shell: Grab email addresses from whois output
Not reliable and not elegant due to whois output content placement and lack of regex which would be a smoother way to do this:


whois domainyouwanttoquery(example.com) | grep Email | cut -d "@" -f 1,2 | cut -d " " -f3 | grep "@"


*NIX  Shell: Find domains from target host
See what subdomains exist in index.html of target site. Download the homepage of a website, wait for that process to complete then search through the file (assuming it's called index.html in this example) using some regex to grab what we are looking for, sort out unique results and grep for in-scope results:


wget -U Mozilla --quiet example.com && wait ${!} |cat index.html | grep -o 'http://[^"]*' | cut -d  "/" -f 3 | sort -u | grep -i example


*OS Python: Output 4 digit numbers from 0000 to 9999
This will give 0000 through 9999 but only complete four digit numbers so no 11 or 193 but 0011 and 0193 will be included:


print "\n".join(['{0:04}'.format(num) for num in xrange(0, 10000)])



No comments:

Post a Comment