snmp : find network information of a system centrally

Anyone can login to a system and run ifconfig or netstat or other similar commands to find the network information of a system. But what will be even better? Do it remotely without logging in to each and every system. How? Using snmpwalk one can retrieve all this information provided that subject has both snmpd running, snmpd supporting network information and the querying host is allowed to make SNMP queries. Lets see how.

Interface table is covered by basic SNMP (just like system information, udp, tcp  socket information, address translation and snmp stats etc). Here is how to query the interface table to get the IP address and Subnet mask information.

unixite@sanbox:~/ > snmpwalk -v1 -c public sandboxS:161 1.3.6.1.2.1.4.20.1.1
iso.3.6.1.2.1.4.20.1.1.1.2.3.4 = IpAddress: 1.2.3.4
iso.3.6.1.2.1.4.20.1.1.127.0.0.1 = IpAddress: 127.0.0.1
iso.3.6.1.2.1.4.20.1.1.192.168.1.10 = IpAddress: 192.168.1.10
iso.3.6.1.2.1.4.20.1.1.10.0.0.2 = IpAddress: 10.0.0.2
unixite@sanbox:~/ > snmpwalk -v1 -c public sandboxS:161 1.3.6.1.2.1.4.20.1.3
iso.3.6.1.2.1.4.20.1.3.1.2.3.4 = IpAddress: 255.0.0.0
iso.3.6.1.2.1.4.20.1.3.127.0.0.1 = IpAddress: 255.0.0.0
iso.3.6.1.2.1.4.20.1.3.192.168.1.10 = IpAddress: 255.255.255.0
iso.3.6.1.2.1.4.20.1.3.10.0.0.2 = IpAddress: 255.255.0.0

First one here retrieves the IP addresses on the system while second one get the subnet masks. -c public has to be changed to right community string and also the version if your supports a different one. My system name here is sandboxS and snmpd is listening on default port 161. If not then you can change the port to match yours.

php : find if IP address is in Network range

Using pear Net_IPv4 module one can find if a given IP address is in provided Network range or on the subnet.

<?php
        // check if IP falls in provided subnet
        include("Net/IPv4.php");

        $ipAddr  = "192.168.1.8";
        $netAddr = "192.168.1.0/29"; // 192.168.1.0 - 192.168.1.7

        $objIP = new Net_IPv4();

        echo $objIP->ipInNetwork($ipAddr, $netAddr) ? "$ipAddr is in $netAddrn" : "$ipAddr is not in $netAddrn";
?>

This requires pear Net_IPv4 module which can be installing in one of the following ways.

pear install Net_IPv4
php pyrus.phar install pear/Net_IPv4

perl : find if IP address is in Network range

Using NetAddr::IP one can find if a given IP address is in provided Network range or on the subnet. This can take many different representations of the subnet address. For example you can throw at it the CIDR (e.g. 192.168.1.0/29) or explicit start and end addresses (e.g. 192.168.1.0-192.168.1.7) or even with network mask (e.g. 192.168.1.0 mask 255.255.255.248). Following example shows all of these possible cases.

#!/usr/local/bin/perl

use NetAddr::IP;

my $ipAddr  = "192.168.1.8";
my $netAddr = "192.168.1.0/29"; # 192.168.1.0 - 192.168.1.7

my $network  = NetAddr::IP->new($netAddr);
#my $network  = NetAddr::IP->new("192.168.1.0", "255.255.255.248");
#my $network  = NetAddr::IP->new("192.168.1.0", "29");
#my $network  = NetAddr::IP->new("192.168.1.0-192.168.1.7");
my $ip = NetAddr::IP->new($ipAddr);

if ($ip->within($network)) {
        print $ip->addr() . " is in same subnetn";
}
else {
        print $ip->addr() . " is outside the subnetn";
}

There are multiple ways the input for network can be provided (four ways are shown above with 3 commented).

bash : search multiple file patterns using single find command

One reader asked how find can be used to find various file patterns. For example in a directory which could be littered with various logs and other files, how do I use a single find command to find all shell scripts, perl scripts and say php scripts. Simple answer is to use multiple -name arguments combined with -o (or ORed) and if needed with -a (or ANDed). Other find conditions (like -mtime, -type etc) can be combined as well. Continue reading

VirtualBox : Use Raw Disk to load Windows under Linux

Here I explain how to use a Physical disk partition for a guest OS under VirtualBox. This is also called Raw Disk partition use for VirtualBox. My use case was to run WindowsXP as guest OS from a physical installation under Linux and still be able to boot up the system in same Windows installation when needed. My system is running Ubuntu 11.04 on core2 duo, 3GB memory, two hard disks (one with Ubuntu and other with WindowsXP installation), with VirtualBox 4.0.4. Process is simple but took quite a while to get all information/steps collected and tested. In nutshell, first we need to know the partition that we will use, then the user who is going to use it needs to have access to it. After that an mbr has to be created and finally a vmdk file is created to use the Raw Disk. Keep reading for the full process. Continue reading

Howto rollover a file when size exceeds using unix find

Here is a one liner to rollover a file to file.old when it exceeds the size using find command. Lets say we have a script in cron that runs and prints messages in a log file. Overtime the log file will grow and we would want to rollover the log file to log.old. Many solutions exist by finding the size and comparing it. Here is one elegant solution in one liner. Thanks to my colleague Vlad who gave the idea for using find and exec, and I added the automatic substitution or brace expansion from my knowledge-base. Happy sharing of knowledge.

find /var/log/ -name myapp.log -size +1M -exec mv {}{,.old} ;

Continue reading

bash : Self redirect of Script’s output to a file

Everyone knows how to use redirection operators to send output of a script to a file. Simply use “>” or “>>” on command prompt after the script or application name and it does the magic of storing the output of the script to the file of choice. But what if one wants that their script shall create its own file and store the output there. In other words, self redirect of output.

Lets take a use case. Imagine you are developing a script that will bring up some applications when a system reboots. When it is run from command line it works fine but when it is run during system startup, it misbehaves. How do you troubleshoot? First response is to use “-x” to print how the script is triggering. But what we want is that when our startup script runs, all of its output (both stdout and stderr) stored in a file that we could use for troubleshooting later. For redirecting output of a startup script we have to use a wrapper script to trigger it and use redirection operator to store its output which is simply a workaround. Or, use the magic word “exec“.
Continue reading

cron : List users who have cron jobs scheduled

In multi-user systems with no restrictions on who can and who cannot schedule cron jobs, things could go out of hand pretty quickly. So it is advisable to check time to time who is running what to ensure that not many users are running cpu intensive operations or they are not stepping on each others’ toes and end up wasting CPU.

To find who has cron jobs scheduled on a system there is no built in command. There is a workaround though. But you have to be superuser or root for this. /var/spool/cron directory contains one file for each user (the file is named as the user) that has any cron job scheduled. So a simple ls /var/spool/cron will provide the list of users that have cron jobs scheduled in the system. Continue reading