Archive for the 'unix' Category

Shell Utilities

Problem You want to compare 2 streams, with byte offset and line numbers?Pull off content with a byte offset?Display lines before or after a pattern?Send an email – upon job failure? Solution The shell is a powerful beast! Anything is possible … with a little know-how. See the examples. Example md5sum TARBALL | awk ‘{print [...]

openssl many uses

Problem You want to generate a self-signed certificate?Encrypt a message with triple desView base 64 encoded dataGenerate and use random dataTest ciphersMonitor certificate expiry dates Solution openssl to the rescue! See examples. Example openssl code to generate self-signed certencrypt with tripledesopenssl base64 [ -d ]# encrypted with base64.Useful fortransfering control/binary content – like this:dd bs=1 [...]

uuencode send attachments

Problem You want to send an attachment, from UNIX command line or from within a shell script Solution uuencode works a treat with Solaris. Not sure on RH Linux, etc. Example cat filename | uuencode wat_u_want_attatch_2b_called.ext | mail -s “this is the subject and here is wat u want attach 2b called” email_addrSimple as that, [...]

tar many uses

Problem You want to make some backups with tar. Solution tar is very functional and has many uses. Example If you are lucky enough to have GNU tar (tar –version will return GNU if so), then you can compress and backup at the same time. tar zcvf /tmp/filename.tgz .This backups and zips the contents of [...]

Port forwarding with SSH

Problem You want to forward all traffic from a port on your box, to another host/port combination. Solution The command in the example will port forward, from the local box – any traffic sent to port 8081, will be sent to port 8080 on www.example.com: Example ssh -x -g -L 8081:www.example.com:8080 localhostDon't forget, if you [...]

Email upon cron failure

Problem You want to receive an email upon cron job failure. Solution The example code can be wrapped around a normal cronjob, to send an email if the cron fails. Example Add to beginning:bash -c ‘( msg=`Add to the end:2>&1 > /tmp/logfail.log`; if [ ! -z "$msg" ] ; then echo “$msg” |/bin/mail -s “`/bin/uname [...]

awk line length and average

Problem You want to display line lengths – then average line length of file. Solution Snip using awk – see example. Example awk ‘ { thislen=length($0); printf(“%-5s %d\n”, NR, thislen); totlen+=thislen}END { printf(“average: %d\n”, totlen/NR); } ‘ filename Reference Technorati Tags: gawk, Unix Coding SchoolLinux Man Pages – gawk command

Linux IPTables Open port range.

Problem Had to find out how to allow a range with iptables recently, whilst setting up Samba and NFS. Solution Always take a backup first, then vi /etc/sysconfig/iptables. In the example I am allowing access to all ports between 600 and 699 – for udp and tcp traffic. Example # grep 600 /etc/sysconfig/iptables-A RH-Firewall-1-INPUT -p [...]

Encrypt TripleDES

Problem You want to encrypt some text, using the tripleDES Cipher. Solution Openssl is a beautiful command, that performs many functions. See example. Example Openssl tripleDES encrypt command:openssl des3 -salt -in file_to_encrypt -pass pass:_your_password_Place contents to encrypt in the file reference by file_to_encryptReplace your_password with your secretOpenssl will output to stdout – so best to [...]

Generate self-signed cert openssl

Problem You want to generate a self-signed certificate, for use with a web server.This will allow you to communicate with your web server over HTTPS, effectively encrypting your traffic. Solution The very first time – you need to set up your own CA (certifying authority). Do this one time only![ See a run through screen [...]