#!/usr/bin/perl $| = 1; use strict; #################################################################### # usage: # # netconfig-win32.pl ip/net ip/net ... # # in the network connections control panel, name your interfaces eth0, # eth1, etc. netconfig will assign the first ip/net to eth0, the next # ip/net to eth1 and so on. you can use 'dhcp' instead of the ip/net # notation to make an interface use dhcp. the ip/net notation is of # the form 10.0.0.5/27, where the /27 refers to how many bits are set # in the netmask. /24 is a netmask of 255.255.255.0, /27 is a netmask # of 255.255.255.224 and so on. the router is cacluated as the subnet # address (bitwise and of the ip address and netmask) plus 1, except # for a /24, where the router is the subnet address plus 100. ##################################################################### sub iptext { return sprintf("%u.%u.%u.%u", ($_[0] >> 24) & 0xFF, ($_[0] >> 16) & 0xFF, ($_[0] >> 8) & 0xFF, $_[0] & 0xFF); } # create netsh script from command line args my $netsh = "interface ip\n"; my $id = 0; for my $arg (@ARGV) { if($arg eq 'dhcp') { $netsh .= "set address eth$id dhcp\n"; $netsh .= "set dns eth$id dhcp none\n"; } elsif($arg =~ m|^(\d+)\.(\d+)\.(\d+)\.(\d+)/([0-9]+)$|s) { my $ip = ($1 << 24) | ($2 << 16) | ($3 << 8) | $4; my $netmask = 0; for(my $i = 32 - $5; $i < 32; $i++) { $netmask |= (1 << $i); } my $router = ($ip & $netmask) + (($netmask == 0xFFFFFF00) ? 100 : 1); $netsh .= "set address eth$id static ".iptext($ip)." ". iptext($netmask)." ".iptext($router)." 1\n"; $netsh .= "set dns eth$id static 128.95.112.1 none\n"; $netsh .= "add dns eth$id 128.95.120.1\n"; } $id++; } print "netsh script:\n".$netsh; # run netsh my $netshfile = "netconfig-temp-$$"; open(NETSH, '>', $netshfile) || die "$!, $netshfile, open failed\n"; print NETSH $netsh; close(NETSH); my $rv = system('netsh', '-f', $netshfile); ($rv == -1) && die "$!, netsh failed\n"; ($rv >> 8) && die "non-zero exit code, netsh failed\n"; unlink($netshfile); # edit registry for DNS suffix search order # there should be a better way to do this that doesn't require a reboot my $rv = system('reg', 'add', 'HKLM\System\CurrentControlSet\Services\TCPIP\Parameters', '/v', 'SearchList', '/t', 'REG_SZ', '/f', '/d', 'cac.washington.edu u.washington.edu washington.edu'); ($rv == -1) && die "$!, reg failed\n"; ($rv >> 8) && die "non-zero exit code, reg failed\n"; exit 0;