Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Change IP-address and computer name on Win32

by primus (Scribe)
on May 19, 2003 at 19:45 UTC ( [id://259274]=perlquestion: print w/replies, xml ) Need Help??

primus has asked for the wisdom of the Perl Monks concerning the following question:


Greetings Monks,

I was wondering if it was possible to alter the ip address of a computer, along with its name?

I know that after these are done (if at all possible) I must reset the computer. I only know of this MFC call
ExitWindowsEx(EWX_REBOOT, 0);
which will reset the computer.

If anyone has any ideas on how something like this would work in perl, i would really appreciate it. I know you can do inline C with perl, but TIMTOWTDI :)

Thanks

update (broquaint): title change (was change IP and computer name)

Replies are listed 'Best First'.
Re: Change IP-address and computer name on Win32
by Abigail-II (Bishop) on May 19, 2003 at 20:37 UTC
    First of all, computers don't have IP addresses. Interfaces have them. A computer typically has more than one interface, and an interface can have more than one IP address. Furthermore, more than one interface can have the same IP address (for instance, most computers with a TCP/IP stack will have a loopback interface, with 127.0.0.1 as IP address).

    And yes, you can change the IP address of an interface. After all, when your machine comes up, the interfaces won't have an IP address. It's often set during, or shortly after the boot sequence.

    The same with the name of a computer. It's something that's set during the boot process. On Unix computers, you'd use the hostname command for that.

    There's no standard way of setting hostnames or IP addresses in Perl. The easiest way of doing so would be to call an external program. On Unix, you'd call ifconfig (and check your systems manual for its options, it varies from system to system) to set an IP address, and hostname to set a hostname.

    On Windows, you call something else.

    Abigail

Re: Change IP-address and computer name on Win32
by Mr. Muskrat (Canon) on May 19, 2003 at 20:51 UTC

    I'm running Windows 98 on this machine and a quick search of the registry for my ip address shows that it is stored in a string value called "IPAddress" in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\NetTrans\xxxx branch. (xxxx will vary.) So I'd start by looking at all of the appropriate values using regedit. Then I would use Win32::TieRegistry to change those string values before rebooting. (Different version of Windows will probably have different registry keys.)

    Another option would be to automate the task using Win32::GUITest or the like...

Re: Change IP-address and computer name on Win32
by JamesNC (Chaplain) on May 19, 2003 at 21:41 UTC
    You can set the network settings with netsh via a Net::Telnet session. I use this script along side a form that feeds it the required info. You could try the registry with Win32::TieRegistry. Hope you can pull some insight out of this example where I set the wins/dns via telnet... then we login via Win32 Remote Term and just run the bat file that executes the netsh static ip line and cleans the .bat off the desktop. I suppose you could execute it the netsh static if you were on the same subnet... but we are not, and telnet session closes before the command executes.
    #!/perl/bin/perl use Net::Telnet; use CGI qw(:all); #my @rv=`serverconfig.pl $host $newip $password $hostname $netmask + # $gateway $dbdns1 $dbdns2 $dbwins1 $dbwins2`; my $username = "administrator"; my $host = "$ARGV[0]"; my $newip = "$ARGV[1]"; my $password = "$ARGV[2]"; my $servername = "$ARGV[3]"; my $netmask = "$ARGV[4]"; my $gateway = "$ARGV[5]"; my $dns1 = "$ARGV[6]"; my $dns2 = "$ARGV[7]"; my $wins1 = "$ARGV[8]"; my $wins2 = "$ARGV[9]"; my $t = Net::Telnet->new(Timeout =>5, Prompt=>'/\>/'); my $ok; $ok = $t->open($host); $ok = $t->login($username, $password); $ok = $t->cmd("netsh interface ip add dns \"Production LAN\" $dns1 in +dex=1"); sleep 2; $ok = $t->cmd("netsh interface ip add dns \"Production LAN\" $dns2 in +dex=2"); sleep 2; $ok = $t->cmd("netsh interface ip delete wins \"Production LAN\" all" +); sleep 2; $ok = $t->cmd("netsh interface ip add wins \"Production LAN\" $wins1 +index=1"); sleep 2; $ok = $t->cmd("netsh interface ip add wins \"Production LAN\" $wins2 +index=2"); sleep 2; $ok = $t->cmd("echo netsh interface ip set address \"Production LAN\" +static $newip $netmask $gateway 1 > c:\\\"Documents and Settings\"\\A +dministrator\\Desktop\\set_ip.bat"); sleep 2; $ok = $t->cmd("echo erase set_ip.bat >> c:\\\"Documents and Settings\" +\\Administrator\\Desktop\\set_ip.bat"); sleep 2; $ok = $t->cmd("exit"); sleep 1; $t->close; print redirect("admin.cgi?query=$args");
    JamesNC Update... FYI, You don't ever need to reboot a machine when you change it's IP address... You will only have to do that if you change the hostname.
Re: Change IP-address and computer name on Win32
by Jenda (Abbot) on May 19, 2003 at 22:17 UTC

    You did not specify what's your OS. Anyway did you look whether Win32::AdminMisc or Win32::Lanman support this?

    To restart the computer you might

    use Win32; Win32::InitiateSystemShutdown(Win32::NodeName(), "", 0, 0, 1);
    (See perldoc Win32) or (if you insist on using that call):
    use Win32::API; sub EWX_LOGOFF () {0} sub EWX_SHUTDOWN () {0x00000001} sub EWX_REBOOT () {0x00000002} sub EWX_FORCE () { 0x00000004} sub EWX_POWEROFF () {0x00000008} sub EWX_FORCEIFHUNG () {0x00000010} $ExitWindows = new Win32::API ('kernel32','ExitWindowsEx', ['L','L'], +'L'); $ExitWindows->Call(EWX_REBOOT,0);
    (The code's untested, but it should work.)

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

Re: Change IP-address and computer name on Win32
by krujos (Curate) on May 19, 2003 at 21:54 UTC
    This should give you information on setting the ip address. I have no idea if it works on anything other than XP and 2000, but its worth a shot. The syntax is something like
    netsh interface ip add address [name=]InterfaceName [addr=]IPAddress [mask=]SubnetMask [[ +gateway=] DefaultGateway [gwmetric=]GatewayMetric]
    This will give you a little more comprehensive information on using netsh.
    Could not find anything on setting the hostname, sorry.
    Good Luck
    Josh
    Update: ++JamesNC for a much better answer.
      You should note in my example above.. that the standard connection is Local Area Connection -vs- Production LAN which is what our segment is called... it makes a difference, you will have to plug your correct connection name in... caps don't matter, but spelling does! :) ++ to Josh too :)
Re: Change IP-address and computer name on Win32
by primus (Scribe) on May 19, 2003 at 23:36 UTC

    thanks alot monks, i will put all of this together and give it a shot. I am confident that the methods will work, and i appologize for being vague.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://259274]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-03-28 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found