Wednesday, July 30, 2008

Important Shell Scripts

Shell Script To Read Linux System Static IP Address

Linux:

ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'



FreeBSD/OpenBSD:

ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'



Solaris:

ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '



Finally 1 Script to detect Os and return ip

OS=`uname`
IO="" # store IP
case $OS in
Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1'|cut -d: -f2 |awk '{ print $1}'`;;
FreeBSD|OpenBSD) IP=`ifconfig |grep -E 'inet.[0-9]' |grep -v '127.0.0.1' | awk '{ print $2}'`;;
SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
*) IP="Unknown";;
esac
echo "$IP"



Shell Script To Read Public Ip

curl -s http://myip.dk/ | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'



Shell Script to Assign Ip to eth device

Suppose eth0 si ethernet device and gateway is 192.168.1.1

sudo ifconfig eth0 down
sudo ifconfig eth0 192.168.1.188 netmask 255.255.255.0 up
sudo route add default gw 192.168.1.1 eth0
sudo /etc/init.d/networking restart

Shell Script To Read Process Id

target=$1
pid=`ps ux awk '/firefox/ && !/awk/ {print $2}'`
echo pid

To know process id of Firefox run it as #./file.sh firefox

Port Mapping using SSH

Suppose on Network postgresql Server is running on port no 5432 and you are unable to connect through that Then you can map that port as your local port.

Code
ssh -l 1234:127.0.0.1:5432 username@ip

Here 1234 is local port number, 5432 is remote port nuber where server is running

No comments: