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

Email Domain Migration Phase I

Recently I had to perform an email migration moving domain1.com to domain2.com.  This was an easy task, but made more interesting due to the fact that we wanted to warn the public that we were changing domain names.  The basic setup of the users was each account had a primary address as domain1.com and at least one alias for domain2.com.  Our plan was to flip this relationship so all users would be logging in with the correct address, but still be able to receive mail at the old domain until we decided to remove it.  We use Zimbra for a mail server, so there is a rich command line ability to take advantage of. Read the rest of this entry »

Switch to our mobile site