Need some help writing a script

mvalpreda
mvalpreda used Ask the Experts™
on
Need some help writing a script.....trying to have the script query the computer name and then run a command based on what is the resulting computer name.

For instance, these are the computer names:
DESKTOP
LAPTOP
LAPTOP-BACKUP

If the result is DESKTOP run
route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF 37
Then quit

If the result is LAPTOP run
route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF 24
Then quit

etc with LAPTOP-BACKUP, the IF # will probably be different

This is to help script VPN connections, so there will be different files based on different networks.

I have to delete the route later, but that command is easier
route delete 192.168.1.0
So that is not computer dependent.

The super bestest thing in the entire world would be if the computer name, and IF (interface) number were tied together. The network address and mask should never change. That way if I change computers or IF changes, I make one change opposed to a bunch of files.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
End-user support
Commented:
Make 2 files:
Route_Stns.txt
DESKTOP 37
LAPTOP 24
LAPTOP-BACKUP 30

Open in new window


Route_Stns.bat
@echo off
for /f "tokens=1,2" %%a in (Route_Stns.txt) do (
  if /i "%computername%" equ "%%a" ECHO route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF %%b
)

Open in new window


In a CMD window, type
Route_Stns.bat

Open in new window


Note: This version ECHOs the result to the screen for debugging reasons. Once satisfied, remove the ECHO fronting route to run it for real

Author

Commented:
I knew it was going to be simple for someone. I am learning a little more about this. I am going to test it on the other computers and then award points. The simplicity of this is absolutely elegant. :)
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
A slight enhancement.  Adjustments made to (1) exit as soon as a match is found in the TXT file, rather than continuing to read lines from it even after a host name match was found, and (2) display a warning message if a computer name is not found in the TXT file in case that needs to be addressed?

@echo off
for /f "tokens=1,2" %%a in (Route_Stns.txt) do (
  if /i "%computername%" equ "%%a" (
    route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF %%b
    exit /b
  )
)
echo *Warning* - Workstation not found in routing table, please contact IT.
pause

Open in new window

~bp
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Another improvement: Directly search the host name.
@echo off
for /F "tokens=1-2" %%a  in ('findstr /c:"%computername%" Route_Stns.txt') do set inf=%%b
if defined inf (
  route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF %inf%
) else (
  echo *Warning* - Workstation %computernamer% not found in routing table, please contact IT.
)

Open in new window

You could also consider to use a VBScript.
Dim arrComputer(99, 1)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
strComputerName = WshNetwork.ComputerName
arrComputer(0,0) = "DESKTOP"
arrComputer(0,1) = "37"
arrComputer(1,0) = "LAPTOP"
arrComputer(1,1) = "24"
arrComputer(2,0) = "LAPTOP-BACKUP"
arrComputer(2,1) = "30"

For i = 0 To UBound(arrComputer)
  If arrComputer(i,0) = "" Then Exit For
  If UCase(strComputerName) = arrComputer(i,0) Then
      Wscript.echo "Creating route for IF " & arrComputer(i,1)
      strCommand = "route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF " & arrComputer(i,1)
      Set objScriptExec = WshShell.Exec(strCommand)
      strResults = objScriptExec.StdOut.ReadAll
      strResults = strResults & objScriptExec.StdErr.ReadAll
      Wscript.echo strResults
      Exit For
  End If
Next

Open in new window

Author

Commented:
Actually found a way to do it with PowerShell that doesn't
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
... that doesn't???

Author

Commented:
Hit send too fast I guess. Found a way in Powershell that does not rely on the interface number, but can use the interface alias. That is the same across computers and makes scripting much easier.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial