Sign in to follow this  
Followers 0

Swap Static Gateway Address

1 post in this topic

Hey all, created a simple program for myself that someone could probably use

at my work we have 2 gateways, one for DSL modem for hosting our site and most of the office use. but we also have a cable modem for internet.

our gateway for DSL is used for email also, to prevent spam from using our mail servers. We only allow people on gateway .1 to send email. also we prevent downloading of .zip and .exe from gateway .1 When i listen to my online XM radio music, or download stuff, i need to go out the cable modem with gateway .5

Here is the code for my program, i pulled out from someone elses code the part about pulling the gateway, so if you are confused by that, i can't help much in that first part.

I am using netsh instead of netdom.exe because i'm running windows 2000 and there is no netdom.exe apparently.


I have the program hotkeyed to a favorite key on my keyboard, so i can quickly swap from listening to music and sending emails

$g_szVersion = "Swap IP Address"
If WinExists($g_szVersion) Then Exit; It's already running

; ----------------------------------------------------------------------------
; AutoIt Version: 3.1.0
; Author:        Mike Dawson <cassetti AT>
; Script Function:
;   Swap the gateway address
; ----------------------------------------------------------------------------

#include <Inet.au3>

;$testing = Run(@ComSpec & " netsh inter ip show address", "", @SW_HIDE, 2)

RunWait(@ComSpec & " /c " & 'netstat -r > c:\netstatout.txt',"",@SW_HIDE)

;Read contents of file
$file1txt = FileRead('c:\netstatout.txt', FileGetSize('c:\netstatout.txt'))

;split to array
$file1txtARRAY = StringSplit($file1txt, @CRLF, 1)

;find/display the line of interest
$cnt = 0
For $i = 1 To $file1txtARRAY[0]
    If StringInStr($file1txtARRAY[$i], "===") <> 0 Then $cnt = $cnt + 1
    If $cnt = 4 Then ExitLoop
$gatewaymsg = $file1txtARRAY[$i - 1]
$gateway = StringRight($gatewaymsg, 11)

#Region --- CodeWizard generated code Start ---
;MsgBox features: Title=Yes, Text=Yes, Buttons=Yes and No, Icon=Warning, Modality=System Modal, Miscellaneous=Top-most attribute
If Not IsDeclared("iMsgBoxAnswer") Then Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(266292,"Gateway Swapper", $gatewaymsg & @CRLF & @CRLF & "   Swap Gateway?")
   Case $iMsgBoxAnswer = 6;Yes

   Case $iMsgBoxAnswer = 7;No
#EndRegion --- CodeWizard generated code End ---

If $gateway = "" Then
    RunWait(@ComSpec & " /c " & 'netsh inter ip set address local static <your IP address> 1',"",@SW_HIDE)
    SoundPlay(@WindowsDir & "\media\ir_inter.wav", 0)

If $gateway = "" Then
    RunWait(@ComSpec & " /c " & 'netsh inter ip set address local static <your IP address> 1',"",@SW_HIDE)
    SoundPlay(@WindowsDir & "\media\ir_inter.wav", 0)

Sleep(10000);wait for soundplay before exit

For security I removed our internal IP address, replace the <IP address> with your internal IP address

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0