Yet another IP changer

Post your working scripts, libraries and tools
TheEnemy
Posts: 94
Joined: 28 Jan 2016, 09:54

Yet another IP changer

19 Nov 2019, 08:48

It's been done. I know. And my version isn't without it's flaws. But I built it specifically for me to use in my situation.
So, in the interest of sharing information, I made a generic template of my work, so that you guys can change/improve as you see fit.
Have fun.

Code: Select all

#NoEnv
#SingleInstance force
SendMode Input
SetWorkingDir %A_ScriptDir%

Gui, font, s8, Arial
Gui, Add, Text,, Select Preset
Gui, Add, DropDownList, w152 vConn Gdlist, Preset-1|Preset-2|Preset-3|Preset-4|Preset-5|Preset-6|Preset-7
Gui, Add, Text,, IP Address:
Gui, Add, Edit, vUnqIP w152
Gui, Add, Text,, Subnet Mask:
Gui, Add, Edit, vUnqSubnt w152
Gui, Add, Text,, Gateway:
Gui, Add, Edit, vUnqGW w152
Gui, Add, Text,, DNS Server 1:
Gui, Add, Edit, vUnqDNS1 w152
Gui, Add, Text,, DNS Server 2:
Gui, Add, Edit, vUnqDNS2 w152
Gui, Add, Button,,Set IP
Gui, Add, Text, x190 y5, Select Adapter
Gui, Add, Edit, vUnqAdptr x190 y266 h22 w210
Gui, Add, Button,,DHCP
Gui, Add, Text, x190 y246, Selected Adapter:
RunWait PowerShell.exe Get-NetAdapter | Format-Table -Property Name | Out-File -FilePath C:\NetInfo.txt -Width 300,,hide
LineText := []
Loop, Read, C:\NetInfo.txt
LineText.Insert(A_LoopReadLine)
Line1 = % Trim(LineText.4)
Line2 = % Trim(LineText.5)
Line3 = % Trim(LineText.6)
Line4 = % Trim(LineText.7)
Line5 = % Trim(LineText.8)
Line6 = % Trim(LineText.9)
Line7 = % Trim(LineText.10)
Line8 = % Trim(LineText.11)
Gui, Add, DropDownList, x190 y25 h215 w210 vAdptrs Galist, %Line1%|%Line2%|%Line3%|%Line4%|%Line5%|%Line6%|%Line7%|%Line8%
Gui, Show, x396 y245 h365 w412, IP CHanger
FileDelete, C:\NetInfo.txt
return
;=========================================================================
dlist:
Gui, Submit, NoHide
subntID = %Conn%
if (Conn = "Preset-1") {
GuiControl,,UnqIP, 192.168.1.10
GuiControl,,UnqSubnt, 255.255.255.0
GuiControl,,UnqGW, 192.168.1.1
GuiControl,,UnqDNS1, 8.8.8.8
GuiControl,,UnqDNS2, 8.8.4.4
return
}
if (Conn = "Preset-2") {
GuiControl,,UnqIP, 192.168.2.10
GuiControl,,UnqSubnt, 255.255.255.0
GuiControl,,UnqGW, 192.168.2.1
GuiControl,,UnqDNS1, 8.8.8.8
GuiControl,,UnqDNS2, 8.8.4.4
return
}
if (Conn = "Preset-3") {
GuiControl,,UnqIP, 192.168.3.10
GuiControl,,UnqSubnt, 255.255.255.0
GuiControl,,UnqGW, 192.168.3.1
GuiControl,,UnqDNS1, 8.8.8.8
GuiControl,,UnqDNS2, 8.8.4.4
return
}
if (Conn = "Preset-4") {
GuiControl,,UnqIP, 192.168.4.10
GuiControl,,UnqSubnt, 255.255.255.0
GuiControl,,UnqGW, 192.168.4.1
GuiControl,,UnqDNS1, 8.8.8.8
GuiControl,,UnqDNS2, 8.8.4.4
return
}
if (Conn = "Preset-5") {
GuiControl,,UnqIP, 192.168.5.10
GuiControl,,UnqSubnt, 255.255.255.0
GuiControl,,UnqGW, 192.168.5.1
GuiControl,,UnqDNS1, 8.8.8.8
GuiControl,,UnqDNS2, 8.8.4.4
return
}
if (Conn = "Preset-6") {
GuiControl,,UnqIP, 192.168.6.10
GuiControl,,UnqSubnt, 255.255.255.0
GuiControl,,UnqGW, 192.168.6.1
GuiControl,,UnqDNS1, 8.8.8.8
GuiControl,,UnqDNS2, 8.8.4.4
return
}
if (Conn = "Preset-7") {
GuiControl,,UnqIP, 192.168.7.10
GuiControl,,UnqSubnt, 255.255.255.0
GuiControl,,UnqGW, 192.168.7.1
GuiControl,,UnqDNS1, 8.8.8.8
GuiControl,,UnqDNS2, 8.8.4.4
return
}
;=========================================================================
alist:
Gui, Submit, NoHide
GuiControl,,UnqAdptr, %Adptrs%
return
;=========================================================================
ButtonSetIP:
GuiControlGet, UnqAdptr
GuiControlGet, UnqIP
GuiControlGet, UnqSubnt
GuiControlGet, UnqGW
GuiControlGet, UnqDNS1
GuiControlGet, UnqDNS2
run, %comspec% /c netsh interface ipv4 set address name="%UnqAdptr%" static %UnqIP% %UnqSubnt% %UnqGW%,,hide
run, %comspec% /c netsh interface ipv4 set dns name="%UnqAdptr%" static %UnqDNS1%,,hide
if (UnqDNS2 = "") 
    {    
    run, %comspec% /c netsh delete dnsserver "%UnqAdptr%" index=2,,hide
    return
    }
Else
run, %comspec% /c  netsh interface ip add dns "%UnqAdptr%" %UnqDNS2% index=2,,hide
return
;=========================================================================
ButtonDHCP:
run, %comspec% /c netsh interface ip set address "%UnqAdptr%" dhcp,,hide
return
;=========================================================================
GuiClose:
 ExitApp
User avatar
TheDewd
Posts: 1391
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Yet another IP changer

19 Nov 2019, 15:23

As a fun exercise, I attempted to make a few improvements to the Gui.

1. Sort the adapters alphabetically.
2. Make the fields ReadOnly unless "Custom" is selected from the preset DropDownList.
3. Show the Gui before getting the adapter names.
4. Add "Status" text.
5. Add preset data to array.

I didn't add the other commands and functionality. I was only focusing on the Gui itself.

Code: Select all

; Script Information ===========================================================
; Name:        New AutoHotkey Script
; Description: New Script Description
; AHK Version: 1.1.31.01 (Unicode 32-bit)
; OS Version:  Windows 2000+
; Language:    English (United States)
; Author:      FirstName LastName <[email protected]>
; Filename:    New AutoHotkey Script.ahk
; ==============================================================================

; Revision History =============================================================
; Revision 1 (YYYY-MM-DD)
; * Initial release
; ==============================================================================

; Auto-Execute =================================================================
#SingleInstance, Force ; Allow only one running instance of script
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
#Warn ; Enable warnings to assist with detecting common errors
;#NoTrayIcon ; Disable the tray icon of the script
;#KeyHistory, 0 ; Keystroke and mouse click history
;ListLines, Off ; The script lines most recently executed
SetWorkingDir, % A_ScriptDir ; Set the working directory of the script
SetBatchLines, -1 ; The speed at which the lines of the script are executed
SendMode, Input ; The method for sending keystrokes and mouse clicks
;DetectHiddenWindows, On ; The visibility of hidden windows by the script
;SetWinDelay, 0 ; The delay to occur after modifying a window
;SetControlDelay, 0 ; The delay to occur after modifying a control
OnExit("OnUnload") ; Run a subroutine or function when exiting the script

return ; End automatic execution
; ==============================================================================

; Labels =======================================================================
; TBD
; ==============================================================================

; Functions ====================================================================
OnLoad() {
	Global ; Assume-global mode
	Static Init := OnLoad() ; Call function

	Menu, Tray, Tip, IP Changer

	Presets := []
	Presets[1] := {"IP": "192.168.1.10", "SM": "255.255.255.0", "GW": "192.168.1.1", "DNS1": "8.8.8.8", "DNS2": "8.8.4.4"}
	Presets[2] := {"IP": "192.168.2.10", "SM": "255.255.255.0", "GW": "192.168.2.1", "DNS1": "8.8.8.8", "DNS2": "8.8.4.4"}
	Presets[3] := {"IP": "192.168.3.10", "SM": "255.255.255.0", "GW": "192.168.3.1", "DNS1": "8.8.8.8", "DNS2": "8.8.4.4"}
	Presets[4] := {"IP": "192.168.4.10", "SM": "255.255.255.0", "GW": "192.168.4.1", "DNS1": "8.8.8.8", "DNS2": "8.8.4.4"}
	Presets[5] := {"IP": "192.168.5.10", "SM": "255.255.255.0", "GW": "192.168.5.1", "DNS1": "8.8.8.8", "DNS2": "8.8.4.4"}
	Presets[6] := {"IP": "192.168.6.10", "SM": "255.255.255.0", "GW": "192.168.6.1", "DNS1": "8.8.8.8", "DNS2": "8.8.4.4"}
	Presets[7] := {"IP": "192.168.7.10", "SM": "255.255.255.0", "GW": "192.168.7.1", "DNS1": "8.8.8.8", "DNS2": "8.8.4.4"}

	If (FileExist(A_Temp "\NetInfo.txt")) {
		FileDelete, %A_Temp%\NetInfo.txt
	}
}

OnUnload(ExitReason, ExitCode) {
	Global ; Assume-global mode
}

GuiCreate() {
	Global ; Assume-global mode
	Static Init := GuiCreate() ; Call function

	Gui, +LastFound -Resize +HWNDhGui
	Gui, Margin, 10, 10

	Gui, Add, Text, xm ym w160, Select Preset:
	Gui, Add, DropDownList, w160 vPresetsDDL gPresetsDDL +AltSubmit
	Gui, Add, Text, x+20 ym w160 Section, Select Adapter:
	Gui, Add, DropDownList, w160 vAdaptersDDL gAdaptersDDL
	Gui, Add, Text, xm w160, IP Address:
	Gui, Add, Edit, w160 r1 HWNDhIPAddress +ReadOnly
	Gui, Add, Text, w160, Subnet Mask:
	Gui, Add, Edit, w160 r1 HWNDhSubnetMask +ReadOnly
	Gui, Add, Text, w160, Gateway:
	Gui, Add, Edit, w160 r1 HWNDhGateway +ReadOnly
	Gui, Add, Text, w160, DNS Server 1:
	Gui, Add, Edit, w160 r1 HWNDhDNSServer1 +ReadOnly
	Gui, Add, Text, w160, DNS Server 2:
	Gui, Add, Text, x+20 yp w160, Selected Adapter:
	Gui, Add, Edit, xm w160 r1 HWNDhDNSServer2 +ReadOnly
	Gui, Add, Edit, x+20 w160 r1 HWNDhSelectedAdapter +ReadOnly
	Gui, Add, Button, xm w80 h23, Set IP
	Gui, Add, Button, xs yp w80 h23, DHCP

	Gui, Add, Text, xm y+20 w160 HWNDhStatus,

	Gui, Font, c666666
	GuiControl, Font, % hStatus

	Gui, Show, AutoSize, IP Changer

	For Index, Value In Presets {
		GuiControl,, PresetsDDL, % "Preset " Index

		If (Index = Presets.MaxIndex()) {
			GuiControl,, PresetsDDL, Custom
		}
	}

	GuiControl,, % hStatus, Getting Adapters...
	GuiControl, +Disabled, AdaptersDDL
	RunWait, PowerShell.exe Get-NetAdapter | Format-Table -Property Name | Out-File -FilePath %A_Temp%\NetInfo.txt -Width 300,, Hide

	Adapters := ""

	Loop, Read, %A_Temp%\NetInfo.txt
	{
		If (A_Index < 4 || A_LoopReadLine = "") {
			Continue
		}

		Adapters .= RegexReplace(A_LoopReadLine, "^\s+|\s+$") "|"
	}

	If (FileExist(A_Temp "\NetInfo.txt")) {
		FileDelete, %A_Temp%\NetInfo.txt
	}

	Sort, Adapters, UD|
	GuiControl,, AdaptersDDL, % Adapters
	GuiControl, -Disabled, AdaptersDDL
	GuiControl,, % hStatus, Ready
}

AdaptersDDL:
	Gui, Submit, NoHide

	GuiControl,, % hSelectedAdapter, % AdaptersDDL
return

PresetsDDL:
	Gui, Submit, NoHide

	If (PresetsDDL <> Presets.MaxIndex() + 1) {
		GuiControl,, % hIPAddress, % Presets[PresetsDDL].IP
		GuiControl,, % hSubnetMask, % Presets[PresetsDDL].SM
		GuiControl,, % hGateway, % Presets[PresetsDDL].GW
		GuiControl,, % hDNSServer1, % Presets[PresetsDDL].DNS1
		GuiControl,, % hDNSServer2, % Presets[PresetsDDL].DNS2
	}

	GuiControl, % (PresetsDDL = Presets.MaxIndex() + 1  ? "-ReadOnly" : "+ReadOnly"), % hIPAddress
	GuiControl, % (PresetsDDL = Presets.MaxIndex() + 1  ? "-ReadOnly" : "+ReadOnly"), % hSubnetMask
	GuiControl, % (PresetsDDL = Presets.MaxIndex() + 1  ? "-ReadOnly" : "+ReadOnly"), % hGateway
	GuiControl, % (PresetsDDL = Presets.MaxIndex() + 1  ? "-ReadOnly" : "+ReadOnly"), % hDNSServer1
	GuiControl, % (PresetsDDL = Presets.MaxIndex() + 1  ? "-ReadOnly" : "+ReadOnly"), % hDNSServer2
return

GuiClose(GuiHwnd) {
	ExitApp ; Terminate the script unconditionally
}

GuiEscape(GuiHwnd) {
	ExitApp ; Terminate the script unconditionally
}
; ==============================================================================
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: songdg and 101 guests