Sign in to follow this  
Followers 0
Chinasmoker

How to set IP and computer name

14 posts in this topic

#1 ·  Posted (edited)

R there some functions that they can set IP and computer name?

Thanks any way

Edited by Chinasmoker

One is never too old to learn

Share this post


Link to post
Share on other sites



Not that i can see after a quick skim over the forums and the help file..

why not track down a VB script that does it, and then convert it to an autoIT script?

Share this post


Link to post
Share on other sites

The computer name can be changed in a registry setting, so the answer is yes, would just be a 1 liner. The IP address, which are you speaking of?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Not that i can see after a quick skim over the forums and the help file..

why not track down a VB script that does it, and then convert it to an autoIT script?

I touch this class script first time,much knowledge to be learned for me ,bit by bit to hard study,


One is never too old to learn

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

eg. I want to set ip of the first adapter :192.168.0.1 ,mask 255.255.255.0,gateway 192.168.0.254,DNS server1: 218.XXX.XXX.XXX,DNS server2: 202.XXX.XXX.XXX,

The computer name can be changed in a registry setting, so the answer is yes, would just be a 1 liner. The IP address, which are you speaking of?

CODE
;----------------------------修改计算机名,IP地址,导入CS-CDkey

#include <GUIConstants.au3>

; == GUI generated with Koda ==

$main = GUICreate("修改计算机名IP地址导CDKEY", 450, 256, 192, 125)

GUICtrlCreateLabel("请输入本机计算机名:", 24, 40, 115, 20)

GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

$cname = GUICtrlCreateInput("", 160, 40, 73, 21)

GUICtrlCreateLabel("请输入本机IP地址:", 24, 104, 103, 20)

GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

GUICtrlCreateLabel("192.168.0", 136, 104, 59, 20)

GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

$IP = GUICtrlCreateInput("", 200, 104, 33, 21)

GUICtrlCreateLabel("导入CDKEY:", 32, 160, 76, 20)

GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

$yes = GUICtrlCreateRadio("是", 152, 160, 49, 17)

$no = GUICtrlCreateRadio("否", 216, 160, 57, 17)

$ok = GUICtrlCreateButton("OK", 88, 208, 75, 25, BitOR($BS_PUSHLIKE,$BS_MULTILINE,$WS_BORDER,$WS_CLIPSIBLINGS))

$cancel = GUICtrlCreateButton("Cancel", 224, 208, 75, 25, BitOR($WS_BORDER,$WS_CLIPSIBLINGS))

GUISetState(@SW_SHOW)

;处理计算机名

While 1

$msg = GuiGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case Else

;;;;;;;

EndSelect

WEnd

Edited by Chinasmoker

One is never too old to learn

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Not sure if AutoIT can do it... but here is how to do it in a batch file:

c:\windows\system32\netsh.exe interface ip set address name="Local Area Connection" static 192.168.0.1 255.255.255.0

Change the name="" to whatever adapter you wish to change.

everything else should hopefully be straight forward.

you could use it in an @comsec style command...

Netsh.exe is included with Windows XP (Pro defineately, check others first)

if you need more help on netsh, open a CMD and run netsh /?

hope this helps you out.

Edited by tAKTelapis

Share this post


Link to post
Share on other sites

Not sure if AutoIT can do it... but here is how to do it in a batch file:

c:\windows\system32\netsh.exe interface ip set address name="Local Area Connection" static 192.168.0.1 255.255.255.0

Change the name="" to whatever adapter you wish to change.

everything else should hopefully be straight forward.

you could use it in an @comsec style command...

Netsh.exe is included with Windows XP (Pro defineately, check others first)

if you need more help on netsh, open a CMD and run netsh /?

hope this helps you out.

thanks any way ,I try it,and I will report test result soon


One is never too old to learn

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

thanks any way ,I try it,and I will report test result soon

;這是我利用WMI轉成AU3的 請試試看吧

#include <array.au3>
#include <date.au3>
    Const $OpenFileForAppending = 8

;****Main Code***************
    $strLocationNumber = InputBox("IPCONFIG","請注意!!!!! 輸入你的IP最後一碼")
    IPCONFIG()
;****End Code****************



func IPCONFIG()
    $strFirstOctet  = "10"
    $strSecondOctet     = "240"
    $strThirdOctet  = "173"
    $strFourthOctet     = $strLocationNumber
    $bolEnableDHCP = 0 
    $strPrimaryWINSServer = "10.240.173.166"
    $strSecondaryWINSServer = "172.31.4.192"
    $strDNSDomain = ""
    $arrIPAddress = _ArrayCreate($strFirstOctet & "." & $strSecondOctet & "." & $strThirdOctet & "." & $strFourthOctet)
    $arrSubnetMask = _ArrayCreate("255.255.255.0")
    $arrGateway = _ArrayCreate($strFirstOctet & "." & $strSecondOctet & "." & $strThirdOctet & ".254")
    $arrGatewayMetric = _ArrayCreate(1)
    $arrDNSServers = _ArrayCreate("10.240.173.166", "172.31.4.192", "168.95.1.1")

    
   ;;*** Set name of computer to connect to "." means this computer
    $strComputer = "."

   ;;*** Connect to WMI service on computer
     $objWMIService = ObjGet("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

   ;;*** Get collection of all Network Adapters in computer with IP enabled
     $colNetCards = $objWMIService.ExecQuery    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
        
    ;;*** Loop for all NIC in computer
    For $objNetCard in $colNetCards

       ;;*** Set static IP address and Subnet Mask
        $intErrorEnableStatic = $objNetCard.EnableStatic($arrIPAddress, $arrSubnetMask)
        
       ;;*** Set gateways and metrics
        $intErrorGateways = $objNetCard.SetGateways($arrGateway, $arrGatewayMetric)
        
    ;;*** Set DNS search order
        $intErrorSetDNSServerSearchOrder = $objNetCard.SetDNSServerSearchOrder($arrDNSServers)
        
       ;;*** Set DNS suffix 
        $intErrorSetDNSDomain = $objNetCard.SetDNSDomain($strDNSDomain )
                
       ;;*** Set WINS
        $objNetCard.SetWINSServer ($strPrimaryWINSServer, $strSecondaryWINSServer)

       ;;*** Enable DHCP and release and renew lease
        If $bolEnableDHCP Then
            $intErrorEnableDHCP = $objNetCard.EnableDHCP()
            $intErrorReleaseDHCPLease = $objNetCard.ReleaseDHCPLease()
            $intErrorRenewDHCPLease = $objNetCard.RenewDHCPLease()
         EndIf

     Next
MsgBox(48,"看 做完了,測試一下喔","關心您")
EndFunc
Exit
Edited by log54321_tw

Share this post


Link to post
Share on other sites

;這是我利用WMI轉成AU3的 請試試看吧

#include <array.au3>
#include <date.au3>
    Const $OpenFileForAppending = 8

;****Main Code***************
    $strLocationNumber = InputBox("IPCONFIG","請注意!!!!! 輸入你的IP最後一碼")
    IPCONFIG()
;****End Code****************
func IPCONFIG()
    $strFirstOctet  = "10"
    $strSecondOctet     = "240"
    $strThirdOctet  = "173"
    $strFourthOctet     = $strLocationNumber
    $bolEnableDHCP = 0 
    $strPrimaryWINSServer = "10.240.173.166"
    $strSecondaryWINSServer = "172.31.4.192"
    $strDNSDomain = ""
    $arrIPAddress = _ArrayCreate($strFirstOctet & "." & $strSecondOctet & "." & $strThirdOctet & "." & $strFourthOctet)
    $arrSubnetMask = _ArrayCreate("255.255.255.0")
    $arrGateway = _ArrayCreate($strFirstOctet & "." & $strSecondOctet & "." & $strThirdOctet & ".254")
    $arrGatewayMetric = _ArrayCreate(1)
    $arrDNSServers = _ArrayCreate("10.240.173.166", "172.31.4.192", "168.95.1.1")

    
  ;;*** Set name of computer to connect to "." means this computer
    $strComputer = "."

  ;;*** Connect to WMI service on computer
     $objWMIService = ObjGet("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

  ;;*** Get collection of all Network Adapters in computer with IP enabled
     $colNetCards = $objWMIService.ExecQuery    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
        
;;*** Loop for all NIC in computer
    For $objNetCard in $colNetCards

      ;;*** Set static IP address and Subnet Mask
        $intErrorEnableStatic = $objNetCard.EnableStatic($arrIPAddress, $arrSubnetMask)
        
      ;;*** Set gateways and metrics
        $intErrorGateways = $objNetCard.SetGateways($arrGateway, $arrGatewayMetric)
        
;;*** Set DNS search order
        $intErrorSetDNSServerSearchOrder = $objNetCard.SetDNSServerSearchOrder($arrDNSServers)
        
      ;;*** Set DNS suffix 
        $intErrorSetDNSDomain = $objNetCard.SetDNSDomain($strDNSDomain )
                
      ;;*** Set WINS
        $objNetCard.SetWINSServer ($strPrimaryWINSServer, $strSecondaryWINSServer)

      ;;*** Enable DHCP and release and renew lease
        If $bolEnableDHCP Then
            $intErrorEnableDHCP = $objNetCard.EnableDHCP()
            $intErrorReleaseDHCPLease = $objNetCard.ReleaseDHCPLease()
            $intErrorRenewDHCPLease = $objNetCard.RenewDHCPLease()
         EndIf

     Next
MsgBox(48,"看 做完了,測試一下喔","關心您")
EndFunc
Exit
I've looked at this before but the issue I saw with this is if you have more than One network card you can't specify which card to change, with the netsh option above you specify the connection name.

I'm sure there must be a way to specify which network adapter to change with WMI though

Share this post


Link to post
Share on other sites

The NETSH command line works fine for me. However, now I want to change the DNS on a remote computer on the network. The only way I could think of doing it was to put the NETSH command in a .BAT file and copy it to the remote computer's startup folder. However this would only take effect on the next boot. Is there any way of doing it immediately?

Share this post


Link to post
Share on other sites

This is a combination of two separate scripts I posted a few months back. You need to know the name of the adapter you want to change. In my example below (my laptop) it was "Wireless Network Connection". This way if you have more than one adapter enabled it will go by name.

Dim $IP[1]
Dim $DNS[2]
Dim $Subnet[1]
Dim $Gateway[1]

$strComputer = "."

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$objClass = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")

For $obj in $objClass
    If $obj.NetConnectionID = "Wireless Network Connection" Then $iIndex = $obj.Index
Next

$collection = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

$IP[0] = "10.0.0.5"
$Subnet[0] = "255.0.0.0"
$Gateway[0] = "10.0.0.1"
$DNS[0] = "4.2.2.1"
$DNS[1] = "4.2.2.2"

For $obj in $collection

    If $iIndex = $obj.Index Then
        $err = $obj.EnableStatic($IP,$Subnet)
        $err2 = $obj.SetGateways($Gateway)
        $err3 = $obj.SetDNSServerSearchOrder($DNS)
   
        If $err = 1 Then
            MsgBox(1,"","Unable to set static IP until reboot")
        ElseIf $err > 1 Then
            MsgBox(1,"","Unable to set IP")
        EndIf
   
        ;etc err2, err3..
    EndIf
Next

Share this post


Link to post
Share on other sites

You could use RegEnumKey() here:

"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\"

to get your nic names and service names.

Dim $key="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\"
    For $i= 1 to 10
        $nic = RegEnumKey($key,$i)
        $Description=RegRead ( $key & $nic, "Description" )
        $ServiceName=RegRead ( $key & $nic, "ServiceName" )
        If $Description <> "" And $ServiceName <> "" Then MsgBox(0,"", $Description & @CRLF & $ServiceName)
    Next

Use ServiceName here:

"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" & $ServiceName & "\Parameters\Tcpip"

to read\ write the adapter IP, DHCP status, gateway, etc.

Dim $key="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\"
    $key2="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"
    $key3="\Parameters\Tcpip"
    For $i= 1 to 10
        $nic = RegEnumKey($key,$i)
        $Description=RegRead ( $key & $nic, "Description" )
        $ServiceName=RegRead ( $key & $nic, "ServiceName" )
        If $Description <> "" And $ServiceName <> "" Then
            $AdapterInfo = $key2 & $ServiceName & $key3
            $DHCPStatus=RegRead ( $AdapterInfo, "EnableDHCP" )
                If $DHCPStatus = "1" Then 
                    $DHCPStatus = "DHCP Enabled"
                Else 
                    $DHCPStatus = "Static IP"
                EndIf
            $DHCPIPAddress=RegRead ( $AdapterInfo, "DhcpIPAddress" )
            $DHCPSubnetMask=RegRead ( $AdapterInfo, "DhcpSubnetMask" )
            $IPAddress=RegRead ( $AdapterInfo, "IPAddress" )
            $SubnetMask=RegRead ( $AdapterInfo, "SubnetMask" )
            $DHCPServer=RegRead ( $AdapterInfo, "DhcpServer" )
            
            $out=$Description & @CRLF 
            $out&=$ServiceName & @CRLF
            $out&="DHCP Status  "&$DHCPStatus & @CRLF 
            $out&="DHCP IP  "&$DHCPIPAddress & @CRLF
            $out&="DHCP SubNet Mask  "&$DHCPSubnetMask & @CRLF 
            $out&="Adapter IP  "&$IPAddress & @CRLF
            $out&="Adapter SubNet Mask  "&$SubnetMask & @CRLF 
            $out&="DHCP Server  "&$DHCPServer & @CRLF
            MsgBox(0,"", $out)
        EndIf
    Next

Share this post


Link to post
Share on other sites

You could use RegEnumKey() here:

"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\"

to get your nic names and service names.

Dim $key="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\"
    For $i= 1 to 10
        $nic = RegEnumKey($key,$i)
        $Description=RegRead ( $key & $nic, "Description" )
        $ServiceName=RegRead ( $key & $nic, "ServiceName" )
        If $Description <> "" And $ServiceName <> "" Then MsgBox(0,"", $Description & @CRLF & $ServiceName)
    Next

Use ServiceName here:

"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" & $ServiceName & "\Parameters\Tcpip"

to read\ write the adapter IP, DHCP status, gateway, etc.

Dim $key="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\"
    $key2="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"
    $key3="\Parameters\Tcpip"
    For $i= 1 to 10
        $nic = RegEnumKey($key,$i)
        $Description=RegRead ( $key & $nic, "Description" )
        $ServiceName=RegRead ( $key & $nic, "ServiceName" )
        If $Description <> "" And $ServiceName <> "" Then
            $AdapterInfo = $key2 & $ServiceName & $key3
            $DHCPStatus=RegRead ( $AdapterInfo, "EnableDHCP" )
                If $DHCPStatus = "1" Then 
                    $DHCPStatus = "DHCP Enabled"
                Else 
                    $DHCPStatus = "Static IP"
                EndIf
            $DHCPIPAddress=RegRead ( $AdapterInfo, "DhcpIPAddress" )
            $DHCPSubnetMask=RegRead ( $AdapterInfo, "DhcpSubnetMask" )
            $IPAddress=RegRead ( $AdapterInfo, "IPAddress" )
            $SubnetMask=RegRead ( $AdapterInfo, "SubnetMask" )
            $DHCPServer=RegRead ( $AdapterInfo, "DhcpServer" )
            
            $out=$Description & @CRLF 
            $out&=$ServiceName & @CRLF
            $out&="DHCP Status  "&$DHCPStatus & @CRLF 
            $out&="DHCP IP  "&$DHCPIPAddress & @CRLF
            $out&="DHCP SubNet Mask  "&$DHCPSubnetMask & @CRLF 
            $out&="Adapter IP  "&$IPAddress & @CRLF
            $out&="Adapter SubNet Mask  "&$SubnetMask & @CRLF 
            $out&="DHCP Server  "&$DHCPServer & @CRLF
            MsgBox(0,"", $out)
        EndIf
    Next
You can use the registry functions in AutoIt, however, they still aren't a guarantee to make the IP address effective unless the computer is rebooted or system is refreshed to use the new settings. I think using NetSh in some ways is a good way to get the IP information and to change it because it does it dynamically.

I have messed around with using registry settings and the Network Cards section isn't full proof either. In some cases there may or may not be a network card indicated by the numbers there or the numbers can be higher than 10.

Also, someone posted this way to get the syntax/script for Netsh (if you want another way to understand netsh which is a pretty powerful command)...

Net Switcher

If there was a way to get the NetSh output to avoid writing to disk and output information directly to AutoIt, that would be a nice thing.

Log54321_tw's post does show another way to do it using WMI...I am not that familiar with using WMI but it would be nice because it avoids the 'writing to disk portion'.


Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Share this post


Link to post
Share on other sites

If there was a way to get the NetSh output to avoid writing to disk and output information directly to AutoIt, that would be a nice thing.

see this post:

#505433

Following the example from the post above:

MsgBox(64, 'DOS', _getDOSOutput('netsh -c interface dump'))

Func _getDOSOutput($command)
    Local $text = '', $Pid = Run(@ComSpec & ' /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return $text
EndFunc  ;==>_getDOSOutput

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