Monday, October 29, 2007

Sorting IP Addresses with Linux/Unix "sort"

Recently I was doing a site survey of all active IPs on our network. I used nmap for this function and dumped all responding IP addresses to a text file. I don't have to worry about PC's not responding to the ping due to a personal firewall since our PC's here do not have one turned on; they are all protected by our corporate firewall. To find all responding IP's I ran the following command line for nmap:
/usr/bin/nmap -n -sP xxx.xxx.xxx.xxx-xxx xxx.xxx.xxx.xxx-xxx >> /home/myUserName/IPdb/site1IPsUp.txt
I did this for several times a day, several days a week for a couple weeks to make sure I had gotten the most complete picture possible, then I tried to sort them...

IP addresses are a strange animal, they aren't really "numbers" per se due to multiple decimal points and they aren't really words, they are a sort of hybrid. Because of this using sort without so pretty specific command line arguments won't give you what you want, which is numerically proper sorting.

Also, nmap outputs a bunch of junk text which I'm not interested in at all, I want this removed and just want the IP addresses themselves. Since I've done this survey over and over again I'm going to have dozens of the same IP address show up in the file, I only want to see it listed once. After all is said and done this is the command line I finished with to give me the output I was looking for:
cat site1IPsUp.txt | grep -v Nmap | grep -v Starting | awk '{ print $2 }' | sort -u -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4

From there I can do with it what I like. ;)

Wednesday, October 3, 2007

Enabling Windows 2000/XP "File and Printer Sharing" at the Command Line

I've searched high and low and there does not seem to be any way to enable Windows File and Printer Sharing without having someone actually perform the function of clicking the stupid check box. Yes, I've gone through the registry and even did the ol' regmon trick to see what windows was modifying, nothing was obvious. Recently this problem has cropped up again causing me to renew my search for an answer, and an answer I found!

The key here is a little program called "snetcfg" which allows you to add or remove services to the windows networking system. I'm assuming snetcfg stands for "service net configurator" or something like that. Now, this program in itself can't actually check that little box that we've been discussing rather we are going to leverage some of windows' default behaviors against it.

When windows installs a service it enables it by default, what we will be accomplishing with snetcfg is that we will be uninstalling the File and Printer Sharing service and then re-installing it. Once Windows has re-installed the service it enables it by default.

Here's how we do it:
  1. Grab snetcfg.zip
  2. The command line to uninstall Win2K File and Printer Sharing Service is:
    1. snetcfg.exe -v -u MS_Server
  3. The command line to re-install Win2K File and Printer Sharing Service is:
    1. snetcfg.exe -l %windir%\Inf\NETSERV.INF -c s -i MS_Server
I've found Windows XP to be similar, you can just put these lines into a batch file and push it out via a domain login script.

Tuesday, October 2, 2007

Force Linux File System Check on Next Reboot

Sometimes you just want to have the system check itself nextime the system reboots, to do that:

As root type:
touch /forcefsck
On the next reboot the filesystem will see the empty file called "forcefsck" in the root partition, this will trigger a full scale disk check.

Force a Kernel Reboot the Not Nice Way

If you use Linux and rely on it every day there will be a day that comes where you will utterly screw yourself. What I mean is you can get a shell but nothing else works...and you're 1000 miles away. So how do you unscrew yourself?
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger
This sends a kernel message to reboot the machine. Now this is a horrible, horrible way to reboot, it's essentially like hitting the reset switch. It does not allow the system to unmount drives or run cleanup scripts, so once things are back up you may have some housekeeping to do.