Archive for Scripting

Simple Encryption and Decryption

You can use this script any way you want to; in fact it’s written with a few things left out (how you want to deal with shell special characters — another complete post on its own — and if you want to expand on it to read and/or write to STDIN/STDOUT so you can pipe one instance to another, even though that defeats the purpose to a certain degree).



I’ve included usage points in the comments section of the script, but the basic usage would be:

tranz.pl encode your message here
tranz.pl decode 458616e6b6370264f62702659637964796e6760245865602c496e657870216e6460255e6968702d456e6167656279656
<-- The Hex output from a message encoded with this script.




This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License


#!/usr/bin/perl # # 2007 - Mike Golvach - eggi@comcast.net # Sanitize shell special characters in # whatever manner you prefer - or just # backslash them on the command line :) # # Sample usage: # trans.pl encode hi there # trans.pl encode hi there >FILE # trans.pl encode `cat FILE` # trans.pl decode 8696024786562756 # trans.pl decode `cat FILE` # # Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License # $input = $ARGV[0]; shift @ARGV; $message = join(" ",@ARGV); if ( $input eq "encode") { $output=unpack("h*",$message); @length=$output=~/.{0,256}/g; print("$output n"); } elsif ( $input eq "decode" ) { chomp($message); $output.=pack"h*",$message; print "n$outputn"; } else { print "Usage: (.*?) [encode|decode] whatever you want to typen"; exit(1) }



The Linux and Unix Menagerie: Simple Encryption and Decryption For Fun And No Profit.
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Bash Tips & Tricks

bash, or the Bourne again shell, is the default shell in most Linux distributions. The popularity of the bash shell amongst Linux and UNIX users is no accident. It has many features to enhance user-friendliness and productivity. Unfortunately, you can’t take advantage of those features unless you know they exist.

When I first started using Linux, the only bash feature I took advantage of was going back through the command history using the up arrow. I soon learned additional features by watching others and asking questions. In this article, I’d like to share some bash tricks I’ve learned over the years.

» Continue reading “Bash Tips & Tricks”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Easy and safe bash history searches

Often the command you need is in your history, how do you find it?

One simple method is to run the history command and pipe it through grep.
$ history | grep cat
110  cat /tmp/foo

You can then run the command by typing ! and the history line number:
$ !110
cat /tmp/foo

» Continue reading “Easy and safe bash history searches”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Python for Bash scripters

Python is easy to learn, and more powerful than Bash. I wasn’t supposed to tell you this–it’s supposed to be a secret. Anything more than a few lines of Bash could be done better in Python. Python is often just as portable as Bash too. Off the top of my head, I can’t think of any *NIX operating systems, that don’t include Python. Even IRIX has Python installed.

» Continue reading “Python for Bash scripters”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Shell Script Comparing files

I have a file (file1) which is like
host1

host2

host3

host4

the list goes on............

Now I want the above lines in files to be compared with files under
/opt/new/*

» Continue reading “Shell Script Comparing files”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Failover routing

a small shell script that ping a test ip and if it is unreachable switch to the other gateway.

Schedule this script to run every minute or so from cron.
#!/bin/bash
GW1="192.168.10.254"
GW2="192.168.55.254"
TESTIP="192.71.220.10" # Any reliable ip that responds to ping.
CURGW=`/sbin/route -n |awk '/^0.0.0.0/ {print  }'`

if ping -w2 -c3 $TESTIP >/dev/null 2>&1; then
echo "Active Route is Ok."
else
if [ "$CURGW" = "$GW1" ]; then
 NEWGW="$GW2"
else
 NEWGW="$GW1"
fi
/sbin/route del default
/sbin/route add default gw $NEWGW
fi

You probably want some sort of test to see if the new path is
working as well. Other concerns may be iptables/tc reloading
and stuff like that.
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Perl For Loop

The Perl for loop is used to loop through a block of code until a specified condition is met. The for loop statement contains three sections followed by a block of code. Below is an example.

Perl For Loop | HACKTUX.
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Awk 1 liners part3

4. Selective Printing of Certain Lines

45. Print the first 10 lines of a file (emulates “head -10″).
awk 'NR < 11'

Awk has a special variable called “NR” that stands for “Number of Lines seen so far in the current file”. After reading each line, Awk increments this variable by one. So for the first line it’s 1, for the second line 2, …, etc. As I explained in the very first one-liner, every Awk program consists of a sequence of pattern-action statements “pattern { action statements }”. The “action statements” part get executed only on those lines that match “pattern” (pattern evaluates to true). In this one-liner the pattern is “NR < 11″ and there are no “action statements”. The default action in case of missing “action statements” is to print the line as-is (it’s equivalent to “{ print $0 }”). The pattern in this one-liner is an expression that tests if the current line number is less than 11. If the line number is less than 11, Awk prints the line. As soon as the line number is 11 or more, the pattern evaluates to false and Awk skips the line.

A much better way to do the same is to quit after seeing the first 10 lines (otherwise we are looping over lines > 10 and doing nothing):
awk '1; NR == 10 { exit }'

The “NR == 10 { exit }” part guarantees that as soon as the line number 10 is reached, Awk quits. For lines smaller than 10, Awk evaluates “1″ that is always a true-statement. And as we just learned, true statements without the “action statements” part are equal to “{ print $0 }” that just prints the first ten lines!

» Continue reading “Awk 1 liners part3″
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Awk 1 Liners part2

3. Text Conversion and Substitution

21. Convert Windows/DOS newlines (CRLF) to Unix newlines (LF) from Unix.
awk '{ sub(/\r$/,""); print }'

This one-liner uses the sub(regex, repl, [string]) function. This function substitutes the first instance of regular expression “regex” in string “string” with the string “repl”. If “string” is omitted, variable $0 is used. Variable $0 contains the entire line.

The one-liner replaces ‘\r’ (CR) character at the end of the line with nothing, i.e., erases CR at the end. Print statement prints out the line and appends ORS variable, which is ‘\n’ by default. Thus, a line ending with CRLF has been converted to a line ending with LF.

» Continue reading “Awk 1 Liners part2″
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Awk 1 Liners part1

1. Line Spacing

1. Double-space a file.
awk '1; { print "" }'

So how does it work? A one-liner is an Awk program and every Awk program consists of a sequence of pattern-action statements “pattern { action statements }“. In this case there are two statements “1″ and “{ print “” }”. In a pattern-action statement either the pattern or the action may be missing. If the pattern is missing, the action is applied to every single line of input. A missing action is equivalent to ‘{ print }’. Thus, this one-liner translates to:
awk '1 { print } { print "" }'

An action is applied only if the pattern matches, i.e., pattern is true. Since ‘1′ is always true, this one-liner translates further into two print statements:
awk '{ print } { print "" }'

Every print statement in Awk is silently followed by an ORS - Output Record Separator variable, which is a newline by default. The first print statement with no arguments is equivalent to “print $0″, where $0 is a variable holding the entire line. The second print statement prints nothing, but knowing that each print statement is followed by ORS, it actually prints a newline. So there we have it, each line gets double-spaced.

» Continue reading “Awk 1 Liners part1″
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment