June 7, 2010

DHCP -> Static Automatically

I recently came across a situation where I was asked to convert a large lab environment from DHCP addresses to statically assigned ones.  Typically I would recommend reserved addresses through DHCP, so that you can still add new systems very easily, but this was a special situation… do what you are told.  It turns out this solution is much more graceful with newer OS versions than XP, as the netsh command acts differently there.  I have two versions of this script, #1 is the more elegant 2003/7 version, and #2 is the XP hacked way.

#1 consists of two files, one that gathers input, and then makes a decision if the current client is DHCP or not, and moves forward if it is.

ipGather.bat:

# Gathers DHCP related info and stores it in a temp file with the hostname and a 1.
netsh interface ip show address | find “DHCP” >> %computername%1.log
# Gathers the current IP address, DHCP or static and stores it in a temp file with the hostname and a 2.
netsh interface ip show address | find “IP Address” >>%computername%2.log
# Parses the first file using a for loop and a CSV type of format since we cannot cut in MS.
for /f “tokens=1,2 delims=Y” %%i in (%computername%1.log) do (
# Takes the variables set up in the for loop and compares the 2nd variable to the “es” from “Yes”.
# If it matches the “es”, the next batch file ipApply.bat is called.
if “%%j” == “es” (ipApply.bat)
)

ipApply.bat:

# Parses the computername2.log file to extra the 2nd variable in another attempt to cut with Windows batch.
for /f “tokens=1,2 delims=:” %%k in (%computername%2.log) do (
# Uses the netsh command line to add in the local IP address as variable %%l and adds the proper gateway, DNS, and WINS info.
netsh interface ip set address local static %%l 255.255.255.0 192.168.20.1 1
netsh interface ip set dns local static 192.168.20.51 primary
netsh interface ip set wins local static 192.168.20.54 primary
)
This all worked really nice in more current OS versions, but stunk in XP.  The netsh command line in XP does not tell you the IP anywhere if it is DHCP, so I had to revert to ipconfig so see the IP, but it has a tab afterwards that I could not cut, so I had to find other interesting ways to make it work.  In essence, I had to increase the XP version to three scripts, the third gets dynamically created and then is run.  The XP version files are below.

ipGather.bat:

# I got tired of seeing the messages, so I included the @echo off, totally optional.
@echo off
# This deletes the dynamically created third script in case you run it twice from the same computer.
del ipApply.bat
# The same method for finding out if the computer is a DHCP client is used, placing the results in computername1.log.
netsh interface ip show address | find “DHCP” >> %computername%1.log
# I have to use ipconfig in order to get the correct IP in the computername2.log.
ipconfig | find “IP Address” >>%computername%2.log
# A for loop is used to read the contents of computername1.log and a delimiter is used to see if it is a DHCP client or no.
# If the computer matches the DHCP Yes option, ipCreate.bat script is called.
for /f “tokens=1,2 delims=Y” %%i in (%computername%1.log) do if “%%j” == “es” ipCreate.bat

ipCreate.bat:

# Once again, the @echo off is optional to show less diagnostic information.
@echo off
# I am using the same type of for loop to grab the second variable in the computername2.log file, it should be the IP address.
# Since I cannot use the netsh command lines as they are due to the extra tab, if the command are echo’ed to another file, the tab does not follow.
# Each netsh line is sent to a third file to dynamically create the next script, ipApply.bat.
for /f “tokens=1,2 delims=:” %%a in (%computername%2.log) do (
echo netsh interface ip set address local static %%b 255.255.255.0 192.168.20.1 1 >> ipApply.bat
echo netsh interface ip set dns local static 192.168.20.51 primary >> ipApply.bat
echo netsh interface ip set wins local static 192.168.20.54 primary >> ipApply.bat
# Run the dynamically created script.
ipApply.bat
# Once everything is completed, let the user know with a success message.
echo Completed Successfully
)

ipApply.bat:

# The third script file gets just the netsh commands that are run to make the computer static.
netsh interface ip set address local static  192.168.20.11 255.255.255.0 192.168.20.1 1
netsh interface ip set dns local static 192.168.20.51 primary
netsh interface ip set wins local static 192.168.20.54 primary
As you can see, it is a bit dirty, but it does get the job done.

April 17, 2010

Tar Backup Plus Email Notification

I have a client that has been wanting a simple tar based backup solution, but want more intelligent email notification.

Read the rest of this entry »

March 22, 2010

BASH IF/ELSE Mail Script

I had a request to track when a leased domain was expiring after we were finished using it.  Instead of manually giving status twice a day I decided to create a script to do the work for me.  The script is pretty well documented already, but the basic process is checking DOMAIN1.COM in a WHOIS search and analyzing the results.  If the DNS names servers point back to DOMAIN2.COM, I know that the domain is still registered to us and is still alive.  To check the WHOIS results, I am running a grep for DOMAIN2.COM, and if found the built-in command success/error shows a “0″.  If the results do not include DOMAIN2.COM, the command with error with a “1″.  Based on the error code value, I send an email telling if the domain is UP or DOWN. Read the rest of this entry »

Switch to our mobile site