MyIpChanger ( IP Subnet and Gateway)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

MyIpChanger ( IP Subnet and Gateway)

29 Sep 2013, 22:17

MyIpChanger Version 1.1 by DataLife - Written and tested with Autohotkey_L Version 1.1.05.1

MyIpChanger lets you quickly change your IP Address.

On Windows 7 MyIpChanger.ahk must be ran as administrator or it will not change your ip address.

Some features are....

1. MyIpChanger remembers the last 10 IP addresses you used and allows you to quickly set your IP address to any of them.

2. Sets IP address to DHCP or STATIC.

3. Remembers DHCP, Static, AutoFill and Lan Adapter choices.

4. Remembers last Gui position and restores GUI to that position on startup.

5. AutoFills subnet and gateway addresses based on IP address entered. (Optional)

6. Supports the Chinese language. (Thanks BZ_FRW)

Notes:

1. Autofilling attempts to autofill some or all of the subnet and gateway. The numbers used may need to be tweaked. I am not certain of what the standard subnets are based on the IP address.

2. Works on Windows XP and Windows 7, but not heavily tested on Windows XP.

3. Creates IP.ico and places it in the scripts folder.

4. Written with Autohotkey_L Version 1.1.05.1 Ansi 32bit

5. With Windows 7 and UAC turned on you may need to run MyIpChanger in administrator mode.

6. Thanks to Snowy for his example batch file to change the IP address. Snowys Ip Changer can be found here http://www.autohotkey.com/forum/viewtopic.php?t=76790

7. Thanks to BZ_FRW for his additions to support the Chinese language

8. Thanks to Veovis for "Include a bitmap in your uncompiled script!!!" found here http://www.autohotkey.com/forum/topic10957.html
and aCkRiTe modified version of Veovis' writefile function to include a bitmap in an uncompiled script found here http://www.autohotkey.com/forum/topic12220.html

9. Thanks to AVGuy for his example for retreiving the lan adapter names. AVGuys AutoIp - Easy IP script can be found here http://www.autohotkey.com/forum/viewtopic.php?t=40578

Download: https://sourceforge.net/projects/myipchanger/files/MyIpChanger.ahk/download
MyIpChanger.jpg
MyIpChanger.jpg (84.89 KiB) Viewed 13107 times
Feedback is apprieated.

Code: Select all

#SingleInstance, Ignore

if A_Langauge = 0409 ;English_United_States
 {
  DefaultLanConnection = Ethernet
  LanConnections = Dedicated
 }

;German
/*
languageCode_0407 = German_Standard
languageCode_0807 = German_Swiss
languageCode_0c07 = German_Austrian
languageCode_1007 = German_Luxembourg
languageCode_1407 = German_Liechtenstein
*/
if Langauge = 0407 ;German_Standard
 {
  DefaultLanConnection = LAN-Verbindung
  LanConnections = Dediziert
 }

RunAsAdmin()

Custom:
FileDelete, NetChange.tmp
FileDelete, NetChange1.tmp
FileDelete, NetChange.bat
RunWait, %Comspec% /c netsh interface show interface >>NetChange.tmp, , Hide
LanNumber = 0
Loop, Read, NetChange.tmp
   {
      StringReplace, Delimiter, A_LoopReadLine, Dedicated, §, UseErrorLevel
      If ErrorLevel = 0
         Continue
      Loop, Parse, Delimiter, §
         {
            If A_LoopField not contains able
               {
                  Counter++
                  StringReplace, LAN_Name, A_LoopField, %A_Space%%A_Space%, , All
                  if Lan_Name = %DefaultLanConnection%
                   LanNumber = %Counter%
                  FileAppend, %LAN_Name%|, NetChange1.tmp
                  LAN_Cnt++
               }
         }
   }
If lan_Cnt =
 {
  MsgBox Not able to retrieve list of lan connections.`n`nScript will exit now 
  ExitApp
 }
FileRead, Lan, NetChange1.tmp
StringTrimRight, Lan, Lan, 1
FileDelete, NetChange.tmp
FileDelete, NetChange1.tmp
IfWinExist, MyIpChanger
 Gui 1: destroy
Gui 1: add, GroupBox, x10 y1 w300 h40
Gui 1: Add, DropDownList, w155 x20 y14 gSubmitLanChange vLanChoice Choose%LanNumber% R%LAN_Cnt%, %Lan%
Gui 1: font, s8 c000000 , 
Gui 1: add, text, x+10 y+-17, Select Network Adapter

IniRead,type,MyIpChanger.ini,config,Type
if type = 1
	DHCP = Checked
else
	Static = Checked
Gui 1: add, GroupBox, x10 y42 w300 h40
Gui 1: Add, radio,gType %Dhcp%  vType x80  y60,DHCP
Gui 1: Add, radio,gType %Static%    x190 y60,Static 

Gui 1: Font, s12, 
Gui 1: Add, Text, x12  y100  w100 h30 , Ip address
Gui 1: Add, Edit, x122 y100  w35  h25 Limit3 gTab vIPoct1 Number , 
Gui 1: Add, Text, x162 y105  w10  h30 , .
Gui 1: Add, Edit, x172 y100  w35  h25 Limit3 gTab vIPoct2 Number , 
Gui 1: Add, Text, x212 y105  w10  h30 , .
Gui 1: Add, Edit, x222 y100  w35  h25 Limit3 gTab vIPoct3 Number , 
Gui 1: Add, Text, x262 y105  w10  h30 , .
Gui 1: Add, Edit, x272 y100  w35  h25 Limit3 gTab vIPoct4 Number , 

Gui 1: Add, Text, x12  y140  w100 h30 , Subnet mask
Gui 1: Add, Edit, x122 y140  w35  h25 Limit3 gTab vSMoct1 Number ,
Gui 1: Add, Text, x162 y145  w10  h30 , .
Gui 1: Add, Edit, x172 y140  w35  h25 Limit3 gTab vSMoct2 Number , 
Gui 1: Add, Text, x212 y145  w10  h30 , .
Gui 1: Add, Edit, x222 y140  w35  h25 Limit3 gTab vSMoct3 Number ,
Gui 1: Add, Text, x262 y145  w10  h30 , .
Gui 1: Add, Edit, x272 y140  w35  h25 Limit3 gTab vSMoct4 Number ,

Gui 1: Add, Text, x12  y180 w100 h30 , Gateway
Gui 1: Add, Edit, x122 y180 w35  h25 Limit3 gTab vGWoct1 Number ,
Gui 1: Add, Text, x162 y185 w10  h30 , .
Gui 1: Add, Edit, x172 y180 w35  h25 Limit3 gTab vGWoct2 Number , 
Gui 1: Add, Text, x212 y185 w10  h30 , .
Gui 1: Add, Edit, x222 y180 w35  h25 Limit3 gTab vGWoct3 Number , 
Gui 1: Add, Text, x262 y185 w10  h30 , .
Gui 1: Add, Edit, x272 y180 w35  h25 Limit3 gTab vGWoct4 Number ,

Gui 1: Font, s11, 
Gui 1: Add, Button,x30  y225 w80 gClearAll,Clear All
Gui 1: Add, Button,x122 y225 w80 gSubmit  ,Submit
Gui 1: Add, Button,x214 y225 w80 gExit  ,Exit
IfExist,save.txt
 {
  Gui 1: Add, Text,x87 y270,Use one of the following
  Gui 1: Add, text,x22 y300,IP Address
  Gui 1: Add, text,x122 y300,Subnet Mask
  Gui 1: Add, text,x222 y300,Gateway

  counter = 0
  breakLoop = 0
  Loop ;loop for each address set
   {
    Loop ; for each octet
     {
      FileReadLine,Value,save.txt,%a_index%
      if Errorlevel = 1
       {
        BreakLoop = 1
        break
       }
        counter++
        Octet%Counter% := Value
        if counter = 4
         {
          NextOctSet++
          counter = 0
          if NextOctSet = 1
           IPAddress := ( octet1 "." octet2 "." octet3 "." octet4 )
          if NextOctSet = 2
           SMAddress := ( octet1 "." octet2 "." octet3 "." octet4 )
          if NextOctSet = 3
           {
            NextOctSet = 0
            If CountRadioButtons = 10
             goto NoMoreRadioButtons
            Gui 1: Add, Radio,x7  y+5 -Group gRadioButton,%IPAddress%
            Gui 1: add, text,x122 yp-0 -Group , %SMAddress%
            GWAddress := ( octet1 "." octet2 "." octet3 "." octet4 )
            IfInString,GWAddress,x
             GWAddress =
            Gui 1: add, text,x222 yp-0 -Group , %GWAddress%
            CountRadioButtons++
           }
         } ;if counter = 4
     } ;Loop ;for each octet
    NoMoreRadioButtons:
    CountRadioButtons = 0
    BreakLoop = 1
    If BreakLoop = 1
     break  
   } ;loop for each address
  Gui 1: Add, Button,x12 w300 gClearSavedAddresses,Remove saved addresses
} ;IfExist,save.txt
Gui 1: Show, w323, MyIpChanger
Gui 1: color, 0x8080ff
if Type = 1
 {
  loop 12
   Control,disable,,edit%A_Index%, MyIpChanger
 }
else
 ControlFocus,edit1, MyIpChanger
Return
Type:
Gui 1: submit, NoHide
IniWrite,%type%,MyIpChanger.ini,config,Type
if Type = 1
 {
  loop 12
   Control,disable,,edit%A_Index%, MyIpChanger
 }
if Type = 2
 {
  Loop 12
   Control, enable,,edit%a_index%, MyIpChanger
  ControlFocus,edit1, MyIpChanger
 }
	

return
SubmitLanChange:
Gui 1: submit, NoHide
ControlFocus,edit1, MyIpChanger
Return
ClearSavedAddresses:
FileDelete,save.txt
goto Custom
RadioButton: ;determine static number of subnet and gateway
ControlClick,
SetControlDelay -1
ControlClick,button4,MyIpChanger
Gui 1: Submit , NoHide
MouseGetPos,,,,Control
ControlGetText,IPAddress,%Control%, MyIpChanger
StringTrimLeft,Num,Control,6 
Numm = 0
LoopNum := ( Num - 4 ) 
 Loop %LoopNum%
  Numm++               
Num := (Num + 6 + Numm)
Control := ( "Static" Num ) 
ControlGetText,SMAddress,%Control%, MyIpChanger
Num++
Control := ( "Static" Num )
ControlGetText,GWAddress,%Control%, MyIpChanger
StringSplit, Octet, IPAddress, .
ControlSetText,edit1,%octet1%, MyIpChanger
ControlSetText,edit2,%octet2%, MyIpChanger
ControlSetText,edit3,%octet3%, MyIpChanger
ControlSetText,edit4,%octet4%, MyIpChanger
StringSplit, Octet, SMAddress, .
ControlSetText,edit5,%octet1%, MyIpChanger
ControlSetText,edit6,%octet2%, MyIpChanger
ControlSetText,edit7,%octet3%, MyIpChanger
ControlSetText,edit8,%octet4%, MyIpChanger
If GWAddress = 
 {
  ControlSetText,edit9,, MyIpChanger
  ControlSetText,edit10,, MyIpChanger
  ControlSetText,edit11,, MyIpChanger
  ControlSetText,edit12,, MyIpChanger
 }
else
 {
  StringSplit, Octet, GWAddress, .
  ControlSetText,edit9,%octet1%, MyIpChanger
  ControlSetText,edit10,%octet2%, MyIpChanger
  ControlSetText,edit11,%octet3%, MyIpChanger
  ControlSetText,edit12,%octet4%, MyIpChanger
 }
GWAddress =
return
#IfWinActive,MyIpChanger
.::
GuiControlGet, WhichControl, 1:Focus
GuiControlGet,IsFieldBlank,1:,%WhichControl%
if IsFieldBlank =
 return
else
 send {tab}
return
WatchForEdit5Control:
GuiControlGet, WhichControl, 1:Focus
if WhichControl = Edit5
 {
  GuiControlGet,IPS,1:,Edit1
  if IPS between 1 and 127
   {
    GuiControl,1:, Edit5,255
    GuiControl,1:, Edit6,0
    GuiControl,1:, Edit7,0
    GuiControl,1:, Edit8,0
   }
   
  if IPS between 128 and 191
   {
    GuiControl,1:, Edit5,255
    GuiControl,1:, Edit6,255
    GuiControl,1:, Edit7,0
    GuiControl,1:, Edit8,0  
   }
   
  if (IPS>191)
   {
    GuiControl,1:, Edit5,255
    GuiControl,1:, Edit6,255
    GuiControl,1:, Edit7,255
    GuiControl,1:, Edit8,0   
   }
  GuiControlGet,Octet1,1:,Edit1
  GuiControlGet,Octet2,1:,Edit2
  GuiControlGet,Octet3,1:,Edit3
  GuiControl,1:, Edit9,%Octet1%
  GuiControl,1:, Edit10,%Octet2% 
  GuiControl,1:, Edit11,%Octet3% 
  ControlFocus,Edit12,MyIpChanger
  SetTimer,WatchForEdit5Control,Off
 }

return
Tab:
GuiControlGet, WhichControl, 1:Focus
if WhichControl = Edit4
 SetTimer,WatchForEdit5Control,100
GuiControlGet,String,,%WhichControl%
IfNotInString,String,Clear
{
if ( WhichControl = "Edit1" or WhichControl = "Edit9" )
 {
  if String > 223
   {
    MsgBox, 4112,Error,%String% is not a valid entry.  Please specify a value between 1 and 223.
	send {BS}{BS}{BS}
    return
   }
 } 
else
 {
  if String > 255
   {
    MsgBox,4112,Error,%String% is not a valid entry.  Please specify a value between 0 and 255.
	send {BS}{BS}{BS}
    return
   }
 }
Len := StrLen(String)
if Len > 2
 {
  send {tab}
  sleep 250
 }
}
return
ClearAll:
Loop 12
 GuiControl, , Edit%a_index%
ControlFocus,edit1, MyIpChanger
return
Submit:
AddressFound = 0
Gui 1: Submit , NoHide
if LanChoice =
 {
  MsgBox,4112,Error,Please select a Network Adapter
  return
 }

If Type = 1
 {
  goto Dhcp
  return
 }

Loop 4
 {
  if IPoct%a_index% =
   {
    MsgBox, 4112,Error, IP address octet %a_index% can not be blank
	ControlFocus,edit%a_index%, MyIpChanger
    return
   }
 }
Loop 4
 {
  if SMoct%a_index% =
   {
    MsgBox, 4112,Error, Subnet address octet %a_index% can not be blank
	EditControl := ( A_index + 4)
	ControlFocus,edit%EditControl%, MyIpChanger
    return
   }
 }

loop 4
 {
if GWoct%a_index% <>
 {
  Loop 4
   {
    if GWoct%a_index% =
     {
      MsgBox, 4112,Error, Gateway address octet %a_index% can not be blank
	  EditControl := ( A_index + 8)
	  ControlFocus,edit%EditControl%, MyIpChanger
      return
     }
   }
 }
 }
temp := IPoct1
gosub StripLeadingZeros
IPoct1 := temp

temp := IPoct2
gosub StripLeadingZeros
IPoct2 := temp

temp := IPoct3
gosub StripLeadingZeros
IPoct3 := temp

temp := SMoct1
gosub StripLeadingZeros
SMoct1 := temp

temp := SMoct2
gosub StripLeadingZeros
SMoct2 := temp

temp := SMoct3
gosub StripLeadingZeros
SMoct3 := temp

temp := GWoct1
gosub StripLeadingZeros
GWoct1 := temp

temp := GWoct2
gosub StripLeadingZeros
GWoct2 := temp

temp := GWoct3
gosub StripLeadingZeros
GWoct3 := temp

IPAddress := (IPoct1 "." IPoct2 "." IPoct3 "." IPoct4 )
SMAddress := (SMoct1 "." SMoct2 "." SMoct3 "." SMoct4 )
if GWoct1 <>
 GWAddress := (GWoct1 "." GWoct2 "." GWoct3 "." GWoct4 )
else
 GWAddress =
LookForDuplicateAddress := ( IPAddress SMAddress GWAddress )
counter = 0
breakLoop = 0
AddressFound = 0
Loop ;loop for each address set
 {
  Loop ; for each octet
   {
    FileReadLine,Value,save.txt,%a_index%
    if Errorlevel = 1
     {
      BreakLoop = 1
      break
     }
      counter++
      Octet%Counter% := Value
      if counter = 4
       {
        NextOctSet++
        counter = 0
        if NextOctSet = 1
         IPAddress1 := ( octet1 "." octet2 "." octet3 "." octet4 )
        if NextOctSet = 2
         SMAddress1 := ( octet1 "." octet2 "." octet3 "." octet4 )
        if NextOctSet = 3
         {
          NextOctSet = 0
          if octet1 <> x
           {
            GWAddress1 := ( octet1 "." octet2 "." octet3 "." octet4 )
            AddressSet := ( IPAddress1 SMAddress1 GWAddress1 )
            if AddressSet = %LookForDuplicateAddress%
             AddressFound = 1
           }
          else
           {
            GwAddress1 =
            AddressSet := ( IPAddress1 SMAddress1 GWAddress1 )
            if AddressSet = %LookForDuplicateAddress%
             AddressFound = 1
            }
          }
        } ;if counter = 4
   } ;Loop ;for each octet
   If BreakLoop = 1
    break  
 } ;loop for each address
if AddressFound = 0
 {
  loop
   {
    filereadline,var%a_index%,save.txt,%a_index%
    if errorlevel = 1
     {
      Lines := ( a_index - 1 )
      break
     }
   }
  filedelete,save.txt
  FileAppend,%IPoct1%`n,save.txt
  FileAppend,%IPoct2%`n,save.txt
  FileAppend,%IPoct3%`n,save.txt
  FileAppend,%IPoct4%`n,save.txt
  
  FileAppend,%SMoct1%`n,save.txt
  FileAppend,%SMoct2%`n,save.txt
  FileAppend,%SMoct3%`n,save.txt
  FileAppend,%SMoct4%`n,save.txt
  
  if GWoct1 =
   {
    loop 4
    GWoct%a_index% = x
   }
  FileAppend,%GWoct1%`n,save.txt
  FileAppend,%GWoct2%`n,save.txt
  FileAppend,%GWoct3%`n,save.txt
  FileAppend,%GWoct4%`n,save.txt
 }
if Lines > 120
 Lines = 120

loop %Lines%
 {
  var := ( "var" a_index )
  var = % %var%
  FileAppend,%var%`n,save.txt
 }
Gui 1: submit, NoHide ;get LanChoice
FileDelete %A_ScriptDir%\QCIP.bat
if GWAddress =
 {
FileAppend,
   (
rem @ECHO OFF
netsh interface ip set address "%LanChoice%" static %IPAddress% %SMAddress% 1
cls
@ECHO Setting IP Address to          :%IPAddress%
@ECHO Setting Subnet Mask Address to :%SMAddress%
TIMEOUT 5
cls
netsh interface ip show addresses "%LanChoice%"
pause
    ), %A_ScriptDir%\QCIP.bat
 }
else
 {
FileAppend,
   (
@ECHO Setting IP Address to          :%IPAddress%
@ECHO Setting Subnet Mask Address to :%SMAddress%
@ECHO Setting Gateway Address to     :%GWAddress%
@ECHO OFF
netsh interface ip set address name="%LanChoice%" source=static addr=%IPAddress% mask=%SMAddress% gateway=%GWAddress% 1 
cls
@ECHO Current Settings for "%LanChoice%"
netsh interface ip show address name="%LanChoice%"
pause
    ), %A_ScriptDir%\QCIP.bat 
 }
Runwait, %A_ScriptDir%\QCIP.bat
if AddressFound = 0 ;this means the address was added to the saved addresses and the GUI needs to be refreshed
 goto Custom
return
StripLeadingZeros:
if ( temp = 000 or temp = 00 or temp = 0 )
 return
temp := LTrim(temp, "0")
return

Dhcp:
Gui 1: submit, NoHide
if LanChoice =
 {
  MsgBox,4112,Error,Please select a Network Adapter
  return
 }
FileDelete %A_ScriptDir%\QCIP.bat
FileAppend,
   (
@ECHO OFF
netsh interface ip set address "%LanChoice%" dhcp
cls
@ECHO Setting IP to DHCP
TIMEOUT 5
cls
netsh interface ip show addresses "%LanChoice%"
pause
   ), %A_ScriptDir%\QCIP.bat

Run, %A_ScriptDir%\QCIP.bat
return
RunAsAdmin() { ;from infogulch http://www.autohotkey.com/forum/topic78902.html
    static init := A_IsAdmin ? 1 : RunAsAdmin()
    if init
        return
   
    s := (A_IsUnicode ? "" : "A") ; dllcall function suffix
    RegExMatch(DllCall("GetCommandLine" s, "Str"), "^(?|""(?<Exe>[^""]+)""|(?<Exe>[^ ]+))(?: (?<Args>.*))?$", CommandLine)
    DllCall("shell32\ShellExecute" s, "Ptr", 0, "Str", "RunAs", "Str", CommandLineExe, "Str", CommandLineArgs, "Str", A_WorkingDir, "Int", 1)
    ExitApp
}
GuiClose:
Exit:
ExitApp
return
thanks

DataLife
Last edited by DataLife on 14 Jun 2022, 23:42, edited 8 times in total.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
Sergio
Posts: 45
Joined: 29 Sep 2013, 16:36

Re: MyIpChanger ( IP Subnet and Gateway)

30 Sep 2013, 08:41

Hmm. Why would someone want to change their IP on a local level? Why would they change it regularly?
jpginc
Posts: 124
Joined: 29 Sep 2013, 22:35

Re: MyIpChanger ( IP Subnet and Gateway)

30 Sep 2013, 09:30

This would be useful for my LAN parties :)
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: MyIpChanger ( IP Subnet and Gateway)

30 Sep 2013, 14:48

Sergio wrote:Hmm. Why would someone want to change their IP on a local level? Why would they change it regularly?
I am a service technician for a national company. During the course of my job I connect my laptop via the lan on multiple pieces of electronic equipment to gain access to the configuration. Sometimes it is DHCP, sometimes it is 192.168.0.1, sometimes it is a local ip address the customer is using as in 10.xx.xx.120. Even at one location our company may have 3 pieces of equipment and I have to change my ip address several times during the service call.

MyIpChanger saves a lot of time changing my ip address back and forth many times.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
jpginc
Posts: 124
Joined: 29 Sep 2013, 22:35

Re: MyIpChanger ( IP Subnet and Gateway)

07 May 2014, 19:14

the download link isn't working for me anymore. Could you post a new link?
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: MyIpChanger ( IP Subnet and Gateway)

07 May 2014, 22:07

any script is good script regardless of immediate applications. applications will always and invariably follow, one way or another.
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: MyIpChanger ( IP Subnet and Gateway)

08 May 2014, 07:06

I fixed the link.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: MyIpChanger ( IP Subnet and Gateway)

23 Dec 2017, 09:17

I fixed the link again and added the script to the bottom on the first post.

Here is the link also.

https://sourceforge.net/projects/myipchanger/files/MyIpChanger.ahk/download
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 119 guests