Thursday, August 5, 2010

examples on ifconfig, grep and awk
















These examples are about "read ip address of linux interfaces" entry;

1) get interface list;

root@ismail-laptop:/usr/lib/cgi-bin# ifconfig | grep "Link encap"| awk -F ' ' '{print $1}'
eth0
eth2
lo
ppp0
root@ismail-laptop:/usr/lib/cgi-bin#


2) get mac address from each interfaces;

root@ismail-laptop:/usr/lib/cgi-bin# ifconfig | grep "HWaddr" | awk -F ' ' '{print $1 " - " $5}'
eth0 - 00:26:b9:9a:65:06
eth2 - c4:17:fe:1e:fe:6d
root@ismail-laptop:/usr/lib/cgi-bin#

3) get upload/download size for specific interface;

root@ismail-laptop:/usr/lib/cgi-bin# ifconfig ppp0 | grep "RX bytes" | awk -F ' ' '{print "download size: " $3 " " $4}'
download size: (5.8 MB)
root@ismail-laptop:/usr/lib/cgi-bin# ifconfig ppp0 | grep "RX bytes" | awk -F ' ' '{print "download size: " $7 " " $8}'
download size: (2.3 MB)
root@ismail-laptop:/usr/lib/cgi-bin#

Using Beanshell Scripts in cgi-bin Directory

In this tutorial, we demonstrate to use Bean Shell Java interpreter as a cgi language. Beanshell is a Java interpreter that can be called dynamically in Java applications or linux console. You can download and install Beanshell in Ubuntu using

# sudo apt-get install bsh

After installing you can test it by typing bsh in linux shell:

root@stdio:/# bsh
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % System.out.println("Hello world!");
Hello world!
bsh % double[] d=new double[]{1,2,3.14159};
bsh % for (int i=0;i
System.out.println("d["+i+"] is "+d[i]); 
}
d[0] is 1.0
d[1] is 2.0
d[2] is 3.14159



You have to create a file called .jline-bsh.Interpreter.history in directory /var/www and this file has to be writable rights. You can create and give writable rights like this:

# touch /var/www/.jline-bsh.Interpreter.history
# chmod a+w /var/www/.jline-bsh.Interpreter.history

Now we can create our Beanshell cgi script using a text editor like vim.

# cd /usr/lib/cgi-bin/
stdio:/usr/lib/cgi-bin# vim 1.bsh

A sample 1.bsh file includes these lines of codes:

#!/usr/bin/bsh
System.out.println("Content-type: text/html\n\n");
System.out.println("Hello from java cgi");
System.out.println(System.getenv("QUERY_STRING"));


In first line of this script, we say this script will be handled using /usr/bin/bsh.
Secondly, content type of this file is text/html so browser will display the output as html.
In third line, we send a hello world string to client and finally we write variables which are defined in URL.

In order to run this code, we have to give executable rights to file using

# chmod a+x 1.bsh

Now, we are ready to run this code using our browser. Be sure that your apache server is running using

# sudo /etc/init.d/apache2 status

and it is not running use

# sudo /etc/init.d/apache2 start

to start it. After that, open a browser and type,

http://localhost/cgi-bin/1.bsh

If everything is ok, we can see something like that:

 
We can pass some variables like this

http://localhost/cgi-bin/1.bsh?var1=3&var2=stdio&var3=aValue




read ip address of linux interfaces

you can read ip configuration with "ifconfig" command. but this command returns lots of information. if you need just some specific interface address, you have to use longer commands ;)

for example;

my notebook's current ifconfig command output is;

ismail@ismail-laptop:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:26:b9:9a:65:06
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Memory:f6ae0000-f6b00000

eth2 Link encap:Ethernet HWaddr c4:17:fe:1e:fe:6d
inet6 addr: fe80::c617:feff:fe1e:fe6d/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:35
TX packets:0 errors:12 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:17 Base address:0xc000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3582 errors:0 dropped:0 overruns:0 frame:0
TX packets:3582 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:456842 (456.8 KB) TX bytes:456842 (456.8 KB)

ppp0 Link encap:Point-to-Point Protocol
inet addr:178.244.166.249 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:5463 errors:0 dropped:0 overruns:0 frame:0
TX packets:5612 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:5031133 (5.0 MB) TX bytes:1609578 (1.6 MB)


i'm using mobile modem now. so i am using ppp0 interface to connect to internet.

for example a)
i want to read only my ppp0 interface's ip address.

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr" | awk -F ' ' '{ print $2}'| awk -F ':' '{print $2}'
178.244.166.249


lets explain step by step this command;

ifconfig ppp0 command output is;

ismail@ismail-laptop:~$ ifconfig ppp0
ppp0 Link encap:Point-to-Point Protocol
inet addr:178.244.166.249 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:5717 errors:0 dropped:0 overruns:0 frame:0
TX packets:5871 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:5204480 (5.2 MB) TX bytes:1678921 (1.6 MB)


i can eliminate other interfaces with ifconfig ppp0 command. if this output is sent to grep command with "|" argument, i can select only the line which has ip address included in. like this;

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr"
inet addr:178.244.166.249 P-t-P:10.64.64.64 Mask:255.255.255.255


i use "inet addr" keyword because, i think this keyword is unique for ifconfig command output. and now i need to start doing some string manipulation. for example we can seperate this line by blanks. awk command is able to do this features as following;

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr" | awk -F ' ' '{print $2}'
addr:178.244.166.249


-F means "find".
after than ' ' means: seperate by the blanks
and after than '{ print $2 }', prints the second seperated element. if we use $1, we can see "inet" output.

and last step. we have to use awk again for seperating by ":" keyword for read only ip address part.

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr" | awk -F ' ' '{print $2}' | awk -F ':' '{ print $2 }'
178.244.166.249


using like things on last command and finally we can read only ip address.

and last trick. if you will use these commands on shell script and define crontab schedule you have to use fullpath for all commands. so, we have to find fullpath informations about using commands. we can use whereis command to find fullpath info.

ismail@ismail-laptop:~$ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
ismail@ismail-laptop:~$ whereis grep
grep: /bin/grep /usr/share/man/man1/grep.1.gz
ismail@ismail-laptop:~$ whereis awk
awk: /usr/bin/awk /usr/lib/awk /usr/share/awk /usr/share/man/man1/awk.1.gz
ismail@ismail-laptop:~$


and final command;

ismail@ismail-laptop:~$ /sbin/ifconfig ppp0 | /bin/grep "inet addr" | /usr/bin/awk -F ' ' '{ print $2}' | /usr/bin/awk -F ':' '{ print $2 }'
178.244.166.249


In these examples, you have to know interface name to extract ip address informations. for example we knew ppp0 interface name. if you don't know interface name, you can read next entry :)