Jump to content
Sign in to follow this  
jquonce

runas to change network settings

Recommended Posts

jquonce

First off I am very new to AutoIt and still learning it.

I am looking to make a script that changes a laptop users TCP/IP from a static IP to DHCP so when they are offsite they can connect to other networks.

I was hoping to a runas command and run the built in windows command but I am obviously doing something wrong.

Any suggestions or help are greatly appreciated.

Global $adminuser, $adminpass, $admindomain 
 
;=========================================== 
;Hard code the user and password here. 
;=========================================== 
$adminuser = "admin" 
$adminpass = "admin" 
$admindomain = "" 
 
Func _RunAsAdmin($cmd) 
RunAs($adminuser, $admindomain, $adminpass, 1, $cmd) 
EndFunc;==>_RunAsAdmin 
$var = "netsh interface ip set address name="Local Area Connection" dhcp"
$var2 = "netsh interface ip set dns name="Local Area Connection" dhcp"
_RunAsAdmin($var)
_RunAsAdmin($var2)

Share this post


Link to post
Share on other sites
erik7426

First off I am very new to AutoIt and still learning it.

I am looking to make a script that changes a laptop users TCP/IP from a static IP to DHCP so when they are offsite they can connect to other networks.

I was hoping to a runas command and run the built in windows command but I am obviously doing something wrong.

Any suggestions or help are greatly appreciated.

Global $adminuser, $adminpass, $admindomain 
 
;=========================================== 
;Hard code the user and password here. 
;=========================================== 
$adminuser = "admin" 
$adminpass = "admin" 
$admindomain = "" 
 
Func _RunAsAdmin($cmd) 
RunAs($adminuser, $admindomain, $adminpass, 1, $cmd) 
EndFunc;==>_RunAsAdmin 
$var = "netsh interface ip set address name="Local Area Connection" dhcp"
$var2 = "netsh interface ip set dns name="Local Area Connection" dhcp"
_RunAsAdmin($var)
_RunAsAdmin($var2)
Is DHCP available onsite? If not, the solution is very simple. I know this works in XP but not sure about other operating systems. Set the laptop to DHCP. After doing so an option becomes available for an Alternate Configuration. Go to it and supply the static IP connection information.

With this configuration while onsite the laptop will attempt to connect using DHCP. When this fails it will fall back to the Alternate Configuration which is what you want it to use while onsite. The only issue I can think with this method is if while onsite the laptop is in range of other wireless networks that might have DHCP enabled.

Or if you are really committed to using your script the problem is that the quotes are required as part of the command line around Local Area Connection. As is Autoit is processing the quotes as part of the programming structure. This should work for you with no problem as long as nobody has renamed the network connection to something other than Local Area Connection...

Global $adminuser, $adminpass, $admindomain 
 
;=========================================== 
;Hard code the user and password here. 
;=========================================== 
$adminuser = "admin" 
$adminpass = "admin" 
$admindomain = ""
 
$q = Chr(34)

Func _RunAsAdmin($cmd) 
      RunAs($adminuser, $admindomain, $adminpass, 1, $cmd) 
EndFunc;==>_RunAsAdmin

$var = "netsh interface ip set address " & $q & "Local Area Connection" & $q & " dhcp"

_RunAsAdmin($var)
Edited by erik7426

Share this post


Link to post
Share on other sites
jquonce

Great that worked but one more quick question. I want it to actually run these commands one at a time, like let the first one finish then start the next one. How would I do that in the same file?

$varip = "netsh interface ip set address " & $q & "Local Area Connection" & $q & " dhcp"
$vardns = "netsh interface ip set dns " & $q & "Local Area Connection" & $q & " dhcp"
_RunAsAdmin($varip)
_RunAsAdmin($vardns)
Edited by jquonce

Share this post


Link to post
Share on other sites
jquonce

Anyone know how I would run these commands one at a time, like let the first one finish then start the next one?

Share this post


Link to post
Share on other sites
erik7426

Anyone know how I would run these commands one at a time, like let the first one finish then start the next one?

Change the RunAs to RunAsWait in your RunAsAdmin function should work.

Global $adminuser, $adminpass, $admindomain 

;=========================================== 
;Hard code the user and password here. 
;=========================================== 
$adminuser = "admin" 
$adminpass = "admin" 
$admindomain = ""

$q = Chr(34)

Func _RunAsAdmin($cmd) 
    RunAsWait($adminuser, $admindomain, $adminpass, 1, $cmd) 
EndFunc;==>_RunAsAdmin

$varip = "netsh interface ip set address " & $q & "Local Area Connection" & $q & " dhcp"
$vardns = "netsh interface ip set dns " & $q & "Local Area Connection" & $q & " dhcp"

_RunAsAdmin($varip)
_RunAsAdmin($vardns)

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  

×