Jump to content

IP Changing Tool


Recommended Posts

Hello All,

Let me explain what I am trying to do:

Our company has about 15 ships all on there own unique IP range, and they dont want to implement DHCP. This means whenever an employee from the office visits the ships we need to change there IP address, subnet, DNS etc.

I am trying to create a tool so that the users can just call us when they board and we can give them an available IP address, the user can run the app and it will change the address accordingly.

So far I have got to a point where if all the info is entered into the inputbox's and the user clicks Change my IP using the NETSH command it works fine as well as when clicking Reset to DHCP

:"> Now my problems start: :whistle:

1. To make it easier, I would like to give them the option to select a Ship from a dropdown, once the ship is selected I want the Subnet Mask, Default Gateway and DNS fields to automatically be populated with data.

I guess I need to use the IF THEN ELSEIF but I cant figure out how.

Example:

Ship1 might be: Subnet Mask - 255.255.255.0

Default Gateway - 192.166.10.254

DNS 192.166.10.1

Ship2 might be: Subnet Mask - 255.255.254.0

Default Gateway - 192.166.11.254

DNS 192.166.11.3

Ship3 might be: Subnet Mask - 255.255.0.0

Default Gateway - 192.166.22.254

DNS 192.166.22.1

2. I also would like for then to select which NIC to set, Hard Wire or Wireless for this I am using Radio buttons, but also cant figure out the IF THEN ELSEIF statement.

3. For all this to work the user must have admin permissions on the local machine, not all do so I think I can use the Runas command here but is there anyway to change the username and password in the code once it is converted to a .exe?

Can I store the username and password encrypted in another file that, say I put a menu item on the top of the form, an administrator can select Change Password another form will open where the new username and password will be entered, when running the app it will runas with the new username and password?

I guess this is asking a hell of a lot but any help will be greatly appreciated.

Below is what I have.

Thanks in advance,

Craig

---CODE BELOW----

#include <GuiConstants.au3>

#include <Array.au3>

#include <Date.au3>

AutoItSetOption ( "TrayIconHide", 1 )

Opt ("GUIOnEventMode", 1)

Global $pb_progress1, $pb_plbl1

; PROMPT USER FOR YES/NO

; ======================

AutoItSetOption ( "TrayIconHide", 1 )

$answer = MsgBox(4, "IP Changer", "Have you contacted the IT Officer for a valid IP Address?")

; IF NO THEN EXIT APPLICATION

; ===========================

If $answer = 7 Then

MsgBox(496, "IP Changer", "Please first contact the IT-Officer to supply you with a falid IP Address then rerun this App.")

Exit

EndIf

; GUI

; ===

GUICreate("Your current IP is " & @IPAddress1, 380, 520,-1,-1,"","")

GUISetIcon("Icons\AppIcon.ico")

; PROGRESS

; ========

GUICtrlCreateLabel("Progress:", 120, 95)

_Progress_Create(5,70,360,20)

; INPUT

; =====

GUICtrlCreateGroup("Network settings", 5, 170, 360, 130)

$IPAddress = GUICtrlCreateInput("", 110, 190, 130, 20)

GUICtrlCreateLabel("IP-Address:", 15, 195)

$SubNet = GUICtrlCreateInput("", 110, 215, 130, 20)

GUICtrlCreateLabel("Subnet Mask:", 15, 220)

$Gatway = GUICtrlCreateInput("", 110, 240, 130, 20)

GUICtrlCreateLabel("Default Gatway:", 15, 245)

$DNS1 = GUICtrlCreateInput("", 110, 265, 130, 20)

GUICtrlCreateLabel("Preferred DNS:", 15, 270)

; SHIP NAMES

;===========

GUICtrlCreateGroup("Select Ship", 210, 120, 120, 40)

$ComboShip = GUICtrlCreateCombo ("", 220,135,90)

$ShipName = GUICtrlSetData(-1,"Ship1|Ship2|Ship3","Select a ship")

$menutext = GUICtrlRead($ShipName)

GUICtrlCreateGroup("", -99, -99, 1, 1)

; RADIALS

; =======

GUICtrlCreateGroup("Select the card to configure", 5, 120, 200, 40)

$LANCard = GUICtrlCreateRadio ("Network Card", 10, 135, 90, 20)

$WiFiCard = GUICtrlCreateRadio ("Wireless Card", 110, 135, 90, 20)

GUICtrlSetState ("", $GUI_CHECKED)

GUICtrlCreateGroup("", -99, -99, 1, 1)

; BUTTON

; ======

$buttonchangeIP = GUICtrlCreateButton("Change my IP", 10, 330, 100, 30, "")

$buttonresetIP = GUICtrlCreateButton("Reset to DHCP", 120, 330, 100, 30, "")

$buttonDone = GUICtrlCreateButton("Exit Application", 220, 330, 100, 30, "")

GUICtrlSetOnEvent($buttonchangeIP, "buttonchangeIP")

GUISetState()

GUICtrlSetOnEvent($buttonDone, "buttonDone")

GUICtrlSetOnEvent($buttonresetIP, "buttonSetDHCP")

GUICtrlSetBkColor(-1, 0x00ff00)

WinSetOnTop("Date", "", 1)

GUICtrlCreateGroup("", -99, -99, 1, 1)

; GUI MESSAGE LOOP

; ================

GUISetState()

While 1

; GUI Set Ctrl Read

; =================

$IPAddressMessage = GUICtrlRead($IPAddress)

$SubNetMessage = GUICtrlRead($SubNet)

$GatwayMessage = GUICtrlRead($Gatway)

$DNS1Message = GUICtrlRead($DNS1)

WEnd

; START OF SCRIPT

; ===============

; FUNCTION EXIT APPLICATION

; =========================

Func buttonDone()

GUISetState ()

Exit

EndFunc

; FUNCTION START TO SET DHCP

; ==========================

Func buttonSetDHCP()

Local $percent = 0

$percent = 10

_Progress_Update($percent)

$percent = 20

_Progress_Update($percent)

; RUN NETSH AND SET DHCP

; ======================

RunWait(@ComSpec & " /c " & 'netsh interface ip set address "Local Area Connection" dhcp', "", @SW_HIDE)

$percent = 30

_Progress_Update($percent)

$percent = 40

_Progress_Update($percent)

$percent = 50

_Progress_Update($percent)

$percent = 60

_Progress_Update($percent)

$percent = 70

_Progress_Update($percent)

; RUN NETSH AND SET DNS TO AUTO OBTAIN

; ====================================

RunWait(@ComSpec & " /c " & 'netsh interface ip set dns "Local Area Connection" dhcp', "", @SW_HIDE)

$percent = 80

_Progress_Update($percent)

$percent = 90

_Progress_Update($percent)

$percent = 100

_Progress_Update($percent)

; MESSAGE CONFIRM IP IS DHCP

; =========================

MsgBox(496, "IP Changer", "Your TCP/IP settings have succesfully been changed to DHCP.")

Exit

EndFunc

Func buttonchangeIP()

; RUN NETSH AND SET IP INFO SUBMITTED

; ===================================

Local $percent = 0

$percent = 10

_Progress_Update($percent)

$percent = 20

_Progress_Update($percent)

$percent = 30

_Progress_Update($percent)

RunWait(@ComSpec & " /c " & 'netsh interface ip set address name="Local Area Connection" static '& $IPAddressMessage &' '& $SubNetMessage &' '& $GatwayMessage &' 1' , "", @SW_HIDE)

$percent = 40

_Progress_Update($percent)

$percent = 50

_Progress_Update($percent)

$percent = 95

_Progress_Update($percent)

RunWait(@ComSpec & " /c " & 'netsh interface ip set dns "Local Area Connection" static '& $DNS1Message &'', "", @SW_HIDE)

$percent = 100

_Progress_Update($percent)

MsgBox(496, "IP Changer", "Your IP has sucsesfully been changed to "&@IPAddress1&".")

Exit

EndFunc

Func _Progress_Create($i_x, $i_y, $i_width, $i_height)

$pb_progress1 = GUICtrlCreateProgress($i_x, $i_y, $i_width, $i_height, $PBS_SMOOTH)

$pb_plbl1 = GUICtrlCreateLabel("0%", ($i_width / 2) + ($i_x / 2), $i_y + 25, 35, $i_height - 4, -1, $WS_EX_TRANSPARENT)

GUICtrlSetFont(-1, -1, 800)

EndFunc

Func _Progress_Update($i_percent)

GUICtrlSetData($pb_progress1, $i_percent)

GUICtrlSetData($pb_plbl1, $i_percent & "%")

EndFunc

Link to comment
Share on other sites

Have a look at GUICtrlRead. Use it to get the ship you selected.

Then do sth like

$oldship=""

; Put this into the While-loop to get changes and put the correct data
$newship=GuiCtrlRead($Shipname) 
If not $oldship=$newship then
  If $newship = 'Ship1' then
    GuiCtrlsetData ($Gateway,...)
  ElseIf $newship = 'Ship2' then
    GuiCtrlsetData ($Gateway, ...)
  EndIf
$oldship=$newship
EndIf

Have a look at Hex, Chr, Asc and Encrypt (sorry, can't find it in the online-functions ?). This way you can put a password in an ini-file without giving the password directly (readable by humans in its true form).

I'd also use IniReadSection to read the current ship-data and IniReadSectionNames to get the ship-entries. This way, you don't have to recompile your script if you have to add a new ship/modify an ip. Maybe add a new button that enables changes to the inifile by Gui.

You could make the inifile write-protected to avoid changes by other users than Admin.

--- If you like, I can post my version that does some of the things you request.

Edited by dabus
Link to comment
Share on other sites

Have a look at GUICtrlRead. Use it to get the ship you selected.

Then do sth like

$oldship=""

; Put this into the While-loop to get changes and put the correct data
$newship=GuiCtrlRead($Shipname) 
If not $oldship=$newship then
  If $newship = 'Ship1' then
    GuiCtrlsetData ($Gateway,...)
  ElseIf $newship = 'Ship2' then
    GuiCtrlsetData ($Gateway, ...)
  EndIf
$oldship=$newship
EndIf

Have a look at Hex, Chr, Asc and Encrypt (sorry, can't find it in the online-functions ?). This way you can put a password in an ini-file without giving the password directly (readable by humans in its true form).

I'd also use IniReadSection to read the current ship-data and IniReadSectionNames to get the ship-entries. This way, you don't have to recompile your script if you have to add a new ship/modify an ip. Maybe add a new button that enables changes to the inifile by Gui.

You could make the inifile write-protected to avoid changes by other users than Admin.

--- If you like, I can post my version that does some of the things you request.

Hi dabus,

Thanks for the help, I will give it a go.

I will also look into the inifile thing you mensioned, I like the idea about putting ship names and ip info in there to, it makes total sence to do it that way.

Now if I can only figure out how to do all that it would be cool.

If you could post your version it would be great, I would really like to see it.

Maybe I can see in your version how you do the ini file thing.

Once again thanks a milion,

Craig

Link to comment
Share on other sites

I posted mine tuned and qip, scripts, look at my signature

Attention, they were made for vers 3.1. Two scripts one for winnt_xp and another one for win98, they work fine. tested.

if you do not want to update to the 3.2 look for the older compiler, thanks.

Autoit.es - Foro Autoit en Español Word visitors Image Clustrmap image: - Football Spanish team - Spanish team: Casillas, Iniesta, Villa, Xavi, Puyol, Campdevilla, etc..Programando en Autoit+Html - Coding Autoit-Html - Arranca programas desde Internet - Preprocesador de Autoit a http
Link to comment
Share on other sites

I posted mine tuned and qip, scripts, look at my signature

Attention, they were made for vers 3.1. Two scripts one for winnt_xp and another one for win98, they work fine. tested.

if you do not want to update to the 3.2 look for the older compiler, thanks.

Thanks BasicOs,

But whats up with the mouse going crazy after running the script?

Cheers,

Craig

Link to comment
Share on other sites

Thanks BasicOs,

But whats up with the mouse going crazy after running the script?

Cheers,

Craig

you also might want to consider using a Select/Case statement rather than a whole bunch of IF/Then commands for the ship details, would most likely make your script a bit easier to read.

Select
    Case $ship = "Ship1"
        ;do stuff here for ship 1
    Case $ship = "Ship2"
        ;do stuff here for ship 2
    Case $ship = "Ship3"
        ;do stuff here for ship 3
    Case Else
    ;do stuff here for matches that were not made
    EndSelect
Link to comment
Share on other sites

you also might want to consider using a Select/Case statement rather than a whole bunch of IF/Then commands for the ship details, would most likely make your script a bit easier to read.

Select
    Case $ship = "Ship1"
        ;do stuff here for ship 1
    Case $ship = "Ship2"
        ;do stuff here for ship 2
    Case $ship = "Ship3"
        ;do stuff here for ship 3
    Case Else
    ;do stuff here for matches that were not made
    EndSelect

Thank you fu2m8,

I will give that a try too, as it is I am still strugling with the if then else, but I must say I am really keen on trying to get it all written to and INI file and then accessing it from there.

Any ideas/help on the INI file thing would be cool.

Thanks again,

Craig

Link to comment
Share on other sites

As for the user selecting the network adaptor in which to change the ip address, you may find this way of listing the adaptors helpful

http://www.autoitscript.com/forum/index.ph...c=34772&hl=

Wow, Thanks creeping,

I might have to go and re do the mess I have, your code look so much neeter, easier to read and shorter.

Thanks for the input,

Craig

Link to comment
Share on other sites

Wow, Thanks creeping,

I might have to go and re do the mess I have, your code look so much neeter, easier to read and shorter.

Thanks for the input,

Craig

hmm im still new to the gui myself but you might be able to try something like this for reading info from the ini file into your gui

#include <GUIConstants.au3>
#include <Array.au3>

$iniFile = @ScriptDir & "\ships.ini" ;ini file in the same directory as the script

$iniSectionNamesArray = IniReadSectionNames($iniFile) ;array of the section names in the ini file (one for each of your ships?)

$iniSectionNamesString = _ArrayToString($iniSectionNamesArray, "|", 1) ;turns the array into a string to use in our list menu thingy ($mylist), each entry seperated by a '|'   

GUICreate("Ship List", 400, 300, -1, -1) ;creates the gui

GUICtrlCreateLabel("Highlight an entry click refresh to show the info from the .ini file.", 25, 15)
$mylist=GUICtrlCreateList ("", 25, 40, 130, 100) ;creates our list (blank atm)
GUICtrlSetLimit(-1,200) ; to limit horizontal scrolling
GUICtrlSetData(-1, $iniSectionNamesString);populates our list with the entries from our INI file (taken by the section name)
$refresh=GUICtrlCreateButton ("Refresh", 25,130,130,25) ;creates a refresh button
GUICtrlCreateLabel ("IP Address:",  25, 180, 100) ;label
$iplabel = GUICtrlCreateLabel("", 25, 200, 200) ;label that will be used to show the IP info taken from the INI file
GUICtrlCreateLabel ("DNS Address:",  25, 220, 100) ;label
$dnslabel = GUICtrlCreateLabel("", 25, 240, 200) ;label that will be used to show DNS IP taken from the INI file

GUISetState () ;actually displays our GUI

$msg = 0 ;var used in our while loop
While $msg <> $GUI_EVENT_CLOSE ;waits for msg to be equal to $GUI_EVENT_CLOSE before exiting the loop
    $msg = GUIGetMsg() ;waits for something to happen on the gui
    Select ;select statement to wait for some to push our $refresh button
        Case $msg = $refresh ;once pushed -->
            $ipAdd = IniRead($iniFile, GUICtrlRead($mylist), "IP", "") ;looks in the ini file to the section name that was highlighted in the GUI, once there it looks at the "IP" value and stores that
            $dnsAdd = IniRead($iniFile, GUICtrlRead($mylist), "DNS", "") ;looks in the ini file to the section name that was highlighted in the GUI, once there it looks at the "DNS" value and stores that
            GUICtrlSetData($iplabel, $ipAdd) ;sets our label with IP info
            GUICtrlSetData($dnslabel, $dnsAdd) ;sets our label with DNS info
    EndSelect
Wend

Exit

ships.ini file:

[Ship1]
IP=192.168.150.20
DNS=192.168.150.56

[Ship2]
IP=10.11.12.13
DNS=10.11.12.20

[Ship3]
IP=192.168.0.55
DNS=192.168.0.2

[Ship4]
IP=18.18.18.17
DNS=18.18.18.18

[Ship5]
IP=201.72.15.101
DNS=201.72.15.125

[Ship6]
IP=222.45.13.100
DNS=222.45.13.201

[Ship7]
IP=197.50.26.18
DNS=197.50.26.75

[Ship8]
IP=201.73.41.100
DNS=201.73.41.150

[Ship9]
IP=107.3.6.1
DNS=107.3.6.200

this example will basically read the .ini file (named ships.ini) look at each section name then build the list of ships from that. When you click refresh it will use the name you highlighted and just display some info (IP & DNS addresses as an example) in the gui for that particular ship. Obviously in your script you would rather have an input of some form for the IP address of the local machine rather than pull it from a file, like in this example though.

:whistle:

Link to comment
Share on other sites

hmm im still new to the gui myself but you might be able to try something like this for reading info from the ini file into your gui

#include <GUIConstants.au3>
#include <Array.au3>

$iniFile = @ScriptDir & "\ships.ini" ;ini file in the same directory as the script

$iniSectionNamesArray = IniReadSectionNames($iniFile) ;array of the section names in the ini file (one for each of your ships?)

$iniSectionNamesString = _ArrayToString($iniSectionNamesArray, "|", 1) ;turns the array into a string to use in our list menu thingy ($mylist), each entry seperated by a '|'   

GUICreate("Ship List", 400, 300, -1, -1) ;creates the gui

GUICtrlCreateLabel("Highlight an entry click refresh to show the info from the .ini file.", 25, 15)
$mylist=GUICtrlCreateList ("", 25, 40, 130, 100) ;creates our list (blank atm)
GUICtrlSetLimit(-1,200) ; to limit horizontal scrolling
GUICtrlSetData(-1, $iniSectionNamesString);populates our list with the entries from our INI file (taken by the section name)
$refresh=GUICtrlCreateButton ("Refresh", 25,130,130,25) ;creates a refresh button
GUICtrlCreateLabel ("IP Address:",  25, 180, 100) ;label
$iplabel = GUICtrlCreateLabel("", 25, 200, 200) ;label that will be used to show the IP info taken from the INI file
GUICtrlCreateLabel ("DNS Address:",  25, 220, 100) ;label
$dnslabel = GUICtrlCreateLabel("", 25, 240, 200) ;label that will be used to show DNS IP taken from the INI file

GUISetState () ;actually displays our GUI

$msg = 0 ;var used in our while loop
While $msg <> $GUI_EVENT_CLOSE ;waits for msg to be equal to $GUI_EVENT_CLOSE before exiting the loop
    $msg = GUIGetMsg() ;waits for something to happen on the gui
    Select ;select statement to wait for some to push our $refresh button
        Case $msg = $refresh ;once pushed -->
            $ipAdd = IniRead($iniFile, GUICtrlRead($mylist), "IP", "") ;looks in the ini file to the section name that was highlighted in the GUI, once there it looks at the "IP" value and stores that
            $dnsAdd = IniRead($iniFile, GUICtrlRead($mylist), "DNS", "") ;looks in the ini file to the section name that was highlighted in the GUI, once there it looks at the "DNS" value and stores that
            GUICtrlSetData($iplabel, $ipAdd) ;sets our label with IP info
            GUICtrlSetData($dnslabel, $dnsAdd) ;sets our label with DNS info
    EndSelect
Wend

Exit

ships.ini file:

[Ship1]
IP=192.168.150.20
DNS=192.168.150.56

[Ship2]
IP=10.11.12.13
DNS=10.11.12.20

[Ship3]
IP=192.168.0.55
DNS=192.168.0.2

[Ship4]
IP=18.18.18.17
DNS=18.18.18.18

[Ship5]
IP=201.72.15.101
DNS=201.72.15.125

[Ship6]
IP=222.45.13.100
DNS=222.45.13.201

[Ship7]
IP=197.50.26.18
DNS=197.50.26.75

[Ship8]
IP=201.73.41.100
DNS=201.73.41.150

[Ship9]
IP=107.3.6.1
DNS=107.3.6.200

this example will basically read the .ini file (named ships.ini) look at each section name then build the list of ships from that. When you click refresh it will use the name you highlighted and just display some info (IP & DNS addresses as an example) in the gui for that particular ship. Obviously in your script you would rather have an input of some form for the IP address of the local machine rather than pull it from a file, like in this example though.

:whistle:

This is really Cool, thanks fu2m8.

I to am also new to this whole thing, actually I have no programing experiance at all but like to play with it.

This is only really my second tool I am trying out.

So I really appreciate the imput, makes it so much easier to learn, this forum is really great and the help is unbelievable.

Cheers,

Craig

Link to comment
Share on other sites

Really late, but I post it anyway since I promised to do...

Translated on the fly, you'll also have to adjust net-sh & feedback, since this was build for a German version...

#cs
    Author: dabus
    Script function: Configure IP-Adress
        
    Changes:
    0.0.0 - Got my TeleSec-script
    0.0.1 - Got the netsh-command
    0.0.2 - Added gui
    0.0.3 - Save and load configuration
    0.0.4 - added some response
    0.0.5 - added dns-configuration
    
    ToDo: Routing
    
#ce 
#Region Compiler directives section
#Compiler_Prompt=y                                              ;y=show compile menu
#Compiler_Icon=E:\Scripts\Icons\Plug.ico                        ;Filename of the Ico file to use
#Compiler_Compression=4                                         ;Compression parameter 0-4  0=Low 2=normal 4=High
#Compiler_Allow_Decompile=n                                     ;y= allow decompile
#Compiler_Res_Comment=Configure your Ethernet                   ;Comment field
#Compiler_Res_Description=Created with Scite4AutoIt & AutoIt3   ;Description field
#Compiler_Res_Fileversion=0.0.5                                 ;File Version
#Compiler_Res_LegalCopyright=© dabus                           ;Copyright field
#Compiler_Run_AU3Check=y                                        ;Run au3check before compilation
#EndRegion

AutoItSetOption('RunErrorsFatal', 0) 
AutoItSetOption('TrayIconHide', 1) 

#include <Constants.au3>
#include <GUIConstants.au3>

Global $Output
Global $Name='NetSwitch'
Global $IniFile=@AppDataDir&'\NetSwitch.ini'
Global $Input1, $Input2, $Input3, $Input4, $Input5, $Input6, $Input6, $Input7, $Input8, $Input9, $Combo1
Global $ConfigName, $LocalIP, $DNS, $GateWay, $NetWork, $RouteGate, $RouteTarget

;Check for power
If @UserName <> 'Administrator' Then
    #Region --- CodeWizard generated code Start ---
    ;InputBox features: Title=Yes, Prompt=Yes, Default Text=No
    $AdminPass = InputBox($Name, "Let's do a restart as Aministrator", '', '*', '280', '160', '-1', '-1')
    Select
        Case @error = 0 ;OK - The string returned is valid
            RunAsSet('Administrator', @ComputerName, $AdminPass)
            If StringInStr(@ScriptFullPath, '.au3') Then
                Run('"' & @AutoItExe & '" "' & @ScriptFullPath & '" ' & $AdminPass)
            Else
                Run('"' & @ScriptFullPath & '" ' & $AdminPass)
            EndIf
            If @error Then MsgBox(4096 + 32, 'Error', 'Error starting under admin mode')
            Exit
        Case @error = 1 ;The Cancel button was pushed
            Exit
        Case @error = 3 ;The InputBox failed to open
            Exit
    EndSelect
    #EndRegion --- CodeWizard generated code End ---
Else
    ; == GUI generated with Koda ==
    $Form1 = GUICreate("NetSwitch", 230, 270)
    GUICtrlCreateLabel("Adress", 10, 50, 50, 20)
    GUICtrlCreateLabel("GateWay", 10, 80, 50, 20)
    GUICtrlCreateLabel("DNS", 10, 110, 50, 20)
    GUICtrlCreateLabel("Route", 10, 150, 50, 20)
    $Label1 = GUICtrlCreateLabel("Please insert the configuration or load one.", 10, 10, 213, 37)
    $Combo1 = GUICtrlCreateCombo("", 10, 190, 95, 20)
    $Input1 = GUICtrlCreateInput("", 70, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input2 = GUICtrlCreateInput("", 110, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input3 = GUICtrlCreateInput("", 150, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input4 = GUICtrlCreateInput("", 190, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input5 = GUICtrlCreateInput("", 190, 80, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input6 = GUICtrlCreateInput("", 70, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input7 = GUICtrlCreateInput("", 110, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input8 = GUICtrlCreateInput("", 150, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input9 = GUICtrlCreateInput("", 190, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input10= GUICtrlCreateInput("", 70, 150, 70, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    GUICtrlSetTip(-1, 'Insert target.')
    $Input11= GUICtrlCreateInput("", 150, 150, 70, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    GUICtrlSetTip(-1, 'Insert gateway.')
    $Button1 = GUICtrlCreateButton("OK", 10, 230, 100, 25)
    $Button2 = GUICtrlCreateButton("Load", 110, 190, 50, 20)
    $Button3 = GUICtrlCreateButton("Save", 170, 190, 50, 20)
    $Button4 = GUICtrlCreateButton("Exit", 120, 230, 100, 25)
    ReadEntries()
    GUICtrlSetState($Button1, $GUI_DEFBUTTON)
    GUICtrlSetState($Combo1, $GUI_FOCUS)
    GUISetState(@SW_SHOW)
    While 1
        $msg = GuiGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Button1
            ; Set
            ReadGui()
            _PipeCMD('netsh interface ip set address "LAN-Verbindung" static '&$NetWork&$LocalIP&' 255.255.255.0 '&$NetWork&$GateWay&' 1', $Output)
            If StringInStr ($Output, 'OK') Then
                _PipeCMD('netsh interface ip set dns "LAN-Verbindung" static '&$DNS, $Output)
                If StringInStr ($Output, 'OK') Then
                    _PipeCMD('route DELETE '&$RouteTarget, $Output)
                    If Not StringInStr ($Output, 'fehlgeschlagen') Then
                        _PipeCMD('route ADD '&$RouteTarget & ' mask 255.255.255.0 '&$RouteGate, $Output)
                        If Not StringInStr ($Output, 'fehlgeschlagen') Then
                            GUICtrlSetData($Label1, 'Command was done properly.')
                            GUICtrlSetColor($Label1, 0xffffff)
                            Sleep (1000)
                            GUICtrlSetColor($Label1, 0x000000)
                        Else; route add
                            GUICtrlSetData($Label1, 'Error while setting the new route.')
                            GUICtrlSetColor($Label1, 0xffffff)
                            Sleep (1000)
                            GUICtrlSetColor($Label1, 0xff0000)
                        EndIf
                    Else; route delete
                        GUICtrlSetData($Label1, 'Error while deleting old route.')
                        GUICtrlSetColor($Label1, 0xffffff)
                        Sleep (1000)
                        GUICtrlSetColor($Label1, 0xff0000)
                    EndIf
                Else; dns
                    GUICtrlSetData($Label1, 'Error while setting dns.')
                    GUICtrlSetColor($Label1, 0xffffff)
                    Sleep (1000)
                    GUICtrlSetColor($Label1, 0xff0000)
                EndIf   
            Else; ip+gateway
                GUICtrlSetData($Label1, 'Error while setting gateway/ip.')
                GUICtrlSetColor($Label1, 0xffffff)
                Sleep (1000)
                GUICtrlSetColor($Label1, 0xff0000)
            EndIf   
        Case $msg = $Button2
            ; Load
            SetGui(GUICtrlRead($Combo1))
            ; read vars again
            ReadGui()
        Case $msg = $Button3
            ; Save
            ReadGui()
            IniWrite($IniFile, $ConfigName, 'LocalIP', $NetWork&$LocalIP) 
            IniWrite($IniFile, $ConfigName, 'GateWay', $NetWork&$GateWay)
            IniWrite($IniFile, $ConfigName, 'DNS', $DNS)
            IniWrite($IniFile, $ConfigName, 'RouteTarget', $RouteTarget)
            IniWrite($IniFile, $ConfigName, 'RouteGate', $RouteGate)
            ReadEntries()
        Case $msg = $Button4
            Exit
        EndSelect
    WEnd
    Exit
EndIf   
            
Func Info($a, $b, $c)
    ToolTip($b, MouseGetPos(0), MouseGetPos(1), $Name & ': ' & $a, $c)
    If $c=3 Then
        MsgBox(16,$Name,'Error was:' & @CRLF & @CRLF & $Output)
        Exit
    EndIf
    Sleep ( 1000 )
EndFunc 

Func _PipeCMD ( $a, ByRef $b , $c=0, $d=0, $e=0 ) ; $a=cmd, $b=$var or Filename, $c=WinMode, $d=show $e=Write to file
    Local $file, $log, $output
    If $c=0 Then
        If $d=0 Then
            $log=Run ( @ComSpec & ' /c ' & $a , '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD )
        Else
            $log=Run ( @ComSpec & ' /c ' & $a , '', @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD )
        EndIf
    Else
        If $d=0 Then
            $log=Run ( $a , '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD )
        Else
            $log=Run ( $a , '', @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD )
        EndIf
    EndIf
    While 1
        $line = StdoutRead($log)
        If @error Then ExitLoop
        $output = $output & @CR & $line
    Wend
    
    While 1
        $line = StderrRead($log)
        If @error Then ExitLoop
        $output = $output & @CR & $line
    Wend
    If $e=0 Then
        $b=$output
    Else
        $file=FileOpen ( $b, 10 )
        FileWrite ( $file, $output)
        FileClose ( $file )
    EndIf   
EndFunc

Func ReadEntries()
    $Sections=IniReadSectionNames($IniFile)
    $a=''
    If Not @error Then
        For $i=1 to $Sections[0]
            If $i=1 Then 
                $a=$Sections[$i]
            Else
                $a=$a&'|'&$Sections[$i]
            EndIf
        Next
        If GUICtrlRead($Combo1) = '' Then 
            GUICtrlSetData($Combo1, '')
            GUICtrlSetData($Combo1, $a, $Sections[1])
            SetGui($Sections[1])
        Else
            $ConfigName=GUICtrlRead($Combo1)
            GUICtrlSetData($Combo1, '')
            GUICtrlSetData($Combo1, $a, $ConfigName)
        EndIf
    EndIf   
EndFunc 

Func ReadGui()
    $ConfigName=GUICtrlRead($Combo1)
    $NetWork=GUICtrlRead($Input1)&'.'&GUICtrlRead($Input2)&'.'&GUICtrlRead($Input3)&'.'
    $LocalIP=GUICtrlRead($Input4)
    $GateWay=GUICtrlRead($Input5)
    $DNS=GUICtrlRead($Input6)&'.'&GUICtrlRead($Input7)&'.'&GUICtrlRead($Input8)&'.'&GUICtrlRead($Input9)
    $RouteTarget=GUICtrlRead($Input10)
    $RouteGate=GUICtrlRead($Input11)
EndFunc
    
Func SetGui($a)
    $LocalIP=IniRead($IniFile, $a, 'LocalIP', '') 
    $GateWay=IniRead($IniFile, $a, 'GateWay', '')
    $DNS=IniRead($IniFile, $a, 'DNS', '')
    $RouteGate=IniRead($IniFile, $a, 'RouteGate', '')
    $RouteTarget=IniRead($IniFile, $a, 'RouteTarget', '')
    $LocalIP=StringSplit($LocalIP, '.')
    GUICtrlSetData($Input1, $LocalIP[1])
    GUICtrlSetData($Input2, $LocalIP[2])
    GUICtrlSetData($Input3, $LocalIP[3])
    GUICtrlSetData($Input4, $LocalIP[4])
    $GateWay=StringSplit($GateWay, '.')
    GUICtrlSetData($Input5, $GateWay[4])
    $DNS=StringSplit($DNS, '.')
    GUICtrlSetData($Input6, $DNS[1])
    GUICtrlSetData($Input7, $DNS[2])
    GUICtrlSetData($Input8, $DNS[3])
    GUICtrlSetData($Input9, $DNS[4])
    GUICtrlSetData($Input10, $RouteTarget)
    GUICtrlSetData($Input11, $RouteGate)
EndFunc
Edited by dabus
Link to comment
Share on other sites

Really late, but I post it anyway since I promised to do...

Translated on the fly, you'll also have to adjust net-sh & feedback, since this was build for a German version...

#cs
    Author: dabus
    Script function: Configure IP-Adress
        
    Changes:
    0.0.0 - Got my TeleSec-script
    0.0.1 - Got the netsh-command
    0.0.2 - Added gui
    0.0.3 - Save and load configuration
    0.0.4 - added some response
    0.0.5 - added dns-configuration
    
    ToDo: Routing
    
#ce 
#Region Compiler directives section
#Compiler_Prompt=y                                              ;y=show compile menu
#Compiler_Icon=E:\Scripts\Icons\Plug.ico                        ;Filename of the Ico file to use
#Compiler_Compression=4                                         ;Compression parameter 0-4  0=Low 2=normal 4=High
#Compiler_Allow_Decompile=n                                     ;y= allow decompile
#Compiler_Res_Comment=Configure your Ethernet                   ;Comment field
#Compiler_Res_Description=Created with Scite4AutoIt & AutoIt3   ;Description field
#Compiler_Res_Fileversion=0.0.5                                 ;File Version
#Compiler_Res_LegalCopyright=© dabus                           ;Copyright field
#Compiler_Run_AU3Check=y                                        ;Run au3check before compilation
#EndRegion

AutoItSetOption('RunErrorsFatal', 0) 
AutoItSetOption('TrayIconHide', 1) 

#include <Constants.au3>
#include <GUIConstants.au3>

Global $Output
Global $Name='NetSwitch'
Global $IniFile=@AppDataDir&'\NetSwitch.ini'
Global $Input1, $Input2, $Input3, $Input4, $Input5, $Input6, $Input6, $Input7, $Input8, $Input9, $Combo1
Global $ConfigName, $LocalIP, $DNS, $GateWay, $NetWork, $RouteGate, $RouteTarget

;Check for power
If @UserName <> 'Administrator' Then
    #Region --- CodeWizard generated code Start ---
    ;InputBox features: Title=Yes, Prompt=Yes, Default Text=No
    $AdminPass = InputBox($Name, "Let's do a restart as Aministrator", '', '*', '280', '160', '-1', '-1')
    Select
        Case @error = 0 ;OK - The string returned is valid
            RunAsSet('Administrator', @ComputerName, $AdminPass)
            If StringInStr(@ScriptFullPath, '.au3') Then
                Run('"' & @AutoItExe & '" "' & @ScriptFullPath & '" ' & $AdminPass)
            Else
                Run('"' & @ScriptFullPath & '" ' & $AdminPass)
            EndIf
            If @error Then MsgBox(4096 + 32, 'Error', 'Error starting under admin mode')
            Exit
        Case @error = 1 ;The Cancel button was pushed
            Exit
        Case @error = 3 ;The InputBox failed to open
            Exit
    EndSelect
    #EndRegion --- CodeWizard generated code End ---
Else
    ; == GUI generated with Koda ==
    $Form1 = GUICreate("NetSwitch", 230, 270)
    GUICtrlCreateLabel("Adress", 10, 50, 50, 20)
    GUICtrlCreateLabel("GateWay", 10, 80, 50, 20)
    GUICtrlCreateLabel("DNS", 10, 110, 50, 20)
    GUICtrlCreateLabel("Route", 10, 150, 50, 20)
    $Label1 = GUICtrlCreateLabel("Please insert the configuration or load one.", 10, 10, 213, 37)
    $Combo1 = GUICtrlCreateCombo("", 10, 190, 95, 20)
    $Input1 = GUICtrlCreateInput("", 70, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input2 = GUICtrlCreateInput("", 110, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input3 = GUICtrlCreateInput("", 150, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input4 = GUICtrlCreateInput("", 190, 50, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input5 = GUICtrlCreateInput("", 190, 80, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input6 = GUICtrlCreateInput("", 70, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input7 = GUICtrlCreateInput("", 110, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input8 = GUICtrlCreateInput("", 150, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input9 = GUICtrlCreateInput("", 190, 110, 30, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    $Input10= GUICtrlCreateInput("", 70, 150, 70, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    GUICtrlSetTip(-1, 'Insert target.')
    $Input11= GUICtrlCreateInput("", 150, 150, 70, 20, $ES_CENTER, $WS_EX_CLIENTEDGE)
    GUICtrlSetTip(-1, 'Insert gateway.')
    $Button1 = GUICtrlCreateButton("OK", 10, 230, 100, 25)
    $Button2 = GUICtrlCreateButton("Load", 110, 190, 50, 20)
    $Button3 = GUICtrlCreateButton("Save", 170, 190, 50, 20)
    $Button4 = GUICtrlCreateButton("Exit", 120, 230, 100, 25)
    ReadEntries()
    GUICtrlSetState($Button1, $GUI_DEFBUTTON)
    GUICtrlSetState($Combo1, $GUI_FOCUS)
    GUISetState(@SW_SHOW)
    While 1
        $msg = GuiGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Button1
            ; Set
            ReadGui()
            _PipeCMD('netsh interface ip set address "LAN-Verbindung" static '&$NetWork&$LocalIP&' 255.255.255.0 '&$NetWork&$GateWay&' 1', $Output)
            If StringInStr ($Output, 'OK') Then
                _PipeCMD('netsh interface ip set dns "LAN-Verbindung" static '&$DNS, $Output)
                If StringInStr ($Output, 'OK') Then
                    _PipeCMD('route DELETE '&$RouteTarget, $Output)
                    If Not StringInStr ($Output, 'fehlgeschlagen') Then
                        _PipeCMD('route ADD '&$RouteTarget & ' mask 255.255.255.0 '&$RouteGate, $Output)
                        If Not StringInStr ($Output, 'fehlgeschlagen') Then
                            GUICtrlSetData($Label1, 'Command was done properly.')
                            GUICtrlSetColor($Label1, 0xffffff)
                            Sleep (1000)
                            GUICtrlSetColor($Label1, 0x000000)
                        Else; route add
                            GUICtrlSetData($Label1, 'Error while setting the new route.')
                            GUICtrlSetColor($Label1, 0xffffff)
                            Sleep (1000)
                            GUICtrlSetColor($Label1, 0xff0000)
                        EndIf
                    Else; route delete
                        GUICtrlSetData($Label1, 'Error while deleting old route.')
                        GUICtrlSetColor($Label1, 0xffffff)
                        Sleep (1000)
                        GUICtrlSetColor($Label1, 0xff0000)
                    EndIf
                Else; dns
                    GUICtrlSetData($Label1, 'Error while setting dns.')
                    GUICtrlSetColor($Label1, 0xffffff)
                    Sleep (1000)
                    GUICtrlSetColor($Label1, 0xff0000)
                EndIf   
            Else; ip+gateway
                GUICtrlSetData($Label1, 'Error while setting gateway/ip.')
                GUICtrlSetColor($Label1, 0xffffff)
                Sleep (1000)
                GUICtrlSetColor($Label1, 0xff0000)
            EndIf   
        Case $msg = $Button2
            ; Load
            SetGui(GUICtrlRead($Combo1))
            ; read vars again
            ReadGui()
        Case $msg = $Button3
            ; Save
            ReadGui()
            IniWrite($IniFile, $ConfigName, 'LocalIP', $NetWork&$LocalIP) 
            IniWrite($IniFile, $ConfigName, 'GateWay', $NetWork&$GateWay)
            IniWrite($IniFile, $ConfigName, 'DNS', $DNS)
            IniWrite($IniFile, $ConfigName, 'RouteTarget', $RouteTarget)
            IniWrite($IniFile, $ConfigName, 'RouteGate', $RouteGate)
            ReadEntries()
        Case $msg = $Button4
            Exit
        EndSelect
    WEnd
    Exit
EndIf   
            
Func Info($a, $b, $c)
    ToolTip($b, MouseGetPos(0), MouseGetPos(1), $Name & ': ' & $a, $c)
    If $c=3 Then
        MsgBox(16,$Name,'Error was:' & @CRLF & @CRLF & $Output)
        Exit
    EndIf
    Sleep ( 1000 )
EndFunc 

Func _PipeCMD ( $a, ByRef $b , $c=0, $d=0, $e=0 ) ; $a=cmd, $b=$var or Filename, $c=WinMode, $d=show $e=Write to file
    Local $file, $log, $output
    If $c=0 Then
        If $d=0 Then
            $log=Run ( @ComSpec & ' /c ' & $a , '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD )
        Else
            $log=Run ( @ComSpec & ' /c ' & $a , '', @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD )
        EndIf
    Else
        If $d=0 Then
            $log=Run ( $a , '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD )
        Else
            $log=Run ( $a , '', @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD )
        EndIf
    EndIf
    While 1
        $line = StdoutRead($log)
        If @error Then ExitLoop
        $output = $output & @CR & $line
    Wend
    
    While 1
        $line = StderrRead($log)
        If @error Then ExitLoop
        $output = $output & @CR & $line
    Wend
    If $e=0 Then
        $b=$output
    Else
        $file=FileOpen ( $b, 10 )
        FileWrite ( $file, $output)
        FileClose ( $file )
    EndIf   
EndFunc

Func ReadEntries()
    $Sections=IniReadSectionNames($IniFile)
    $a=''
    If Not @error Then
        For $i=1 to $Sections[0]
            If $i=1 Then 
                $a=$Sections[$i]
            Else
                $a=$a&'|'&$Sections[$i]
            EndIf
        Next
        If GUICtrlRead($Combo1) = '' Then 
            GUICtrlSetData($Combo1, '')
            GUICtrlSetData($Combo1, $a, $Sections[1])
            SetGui($Sections[1])
        Else
            $ConfigName=GUICtrlRead($Combo1)
            GUICtrlSetData($Combo1, '')
            GUICtrlSetData($Combo1, $a, $ConfigName)
        EndIf
    EndIf   
EndFunc 

Func ReadGui()
    $ConfigName=GUICtrlRead($Combo1)
    $NetWork=GUICtrlRead($Input1)&'.'&GUICtrlRead($Input2)&'.'&GUICtrlRead($Input3)&'.'
    $LocalIP=GUICtrlRead($Input4)
    $GateWay=GUICtrlRead($Input5)
    $DNS=GUICtrlRead($Input6)&'.'&GUICtrlRead($Input7)&'.'&GUICtrlRead($Input8)&'.'&GUICtrlRead($Input9)
    $RouteTarget=GUICtrlRead($Input10)
    $RouteGate=GUICtrlRead($Input11)
EndFunc
    
Func SetGui($a)
    $LocalIP=IniRead($IniFile, $a, 'LocalIP', '') 
    $GateWay=IniRead($IniFile, $a, 'GateWay', '')
    $DNS=IniRead($IniFile, $a, 'DNS', '')
    $RouteGate=IniRead($IniFile, $a, 'RouteGate', '')
    $RouteTarget=IniRead($IniFile, $a, 'RouteTarget', '')
    $LocalIP=StringSplit($LocalIP, '.')
    GUICtrlSetData($Input1, $LocalIP[1])
    GUICtrlSetData($Input2, $LocalIP[2])
    GUICtrlSetData($Input3, $LocalIP[3])
    GUICtrlSetData($Input4, $LocalIP[4])
    $GateWay=StringSplit($GateWay, '.')
    GUICtrlSetData($Input5, $GateWay[4])
    $DNS=StringSplit($DNS, '.')
    GUICtrlSetData($Input6, $DNS[1])
    GUICtrlSetData($Input7, $DNS[2])
    GUICtrlSetData($Input8, $DNS[3])
    GUICtrlSetData($Input9, $DNS[4])
    GUICtrlSetData($Input10, $RouteTarget)
    GUICtrlSetData($Input11, $RouteGate)
EndFunc

This is great, thanks for posting, dabus.

Have you taken a look at psandu.ro's version its really cool too.

I cant believe I have spent soooooo much time trying to build somthing that is out there already :"> .

Thanks for all the help guys I really appreciate it.

Cheers,

Craig

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...