Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

ifconfig.pl script for Windows

by golux (Pilgrim)
on Feb 26, 2014 at 21:11 UTC ( #1076312=CUFP: print w/ replies, xml ) Need Help??

This script came about by accident, when my Windows laptop somehow got its PATH temporarily messed up and couldn't resolve the "ipconfig" command. I'm used to typing "ifconfig" in Linux so I wrote this "ifconfig.pl" script for Windows; it's essentially a wrapper which reformats ipconfig's output in an easier-to-read format, and by default discards interfaces which are down or apply to VMs (VMware virtual machines).

It also gives useful information that the simple form of ipconfig lacks (eg. the adapter's MAC address -- to get it you have to use "ipconfig/all" which spews out even more information to wade through). Plus, you can easily spot from the "Connected" line in the header the indices of connected interfaces (when -a or -v is supplied).

Here's an output comparison, where less lines is arguably better when you're trying to get device information quickly:

ipconfig ......... 57 lines ipconfig/all ..... 121 lines (!) ifconfig.pl ...... 19 lines

Now that I'm using the script all the time I've added a feature to disable/reenable the network device by adding the argument ~N, where N is the index of the device displayed. This saves the hassle of having to open several dialog boxes to get to the network adapter settings, just to be able to cycle an interface that's down for some reason.

#!/usr/bin/perl -w # # Displays currently connected network interfaces. # Lets you DISABLE/REENABLE an interface (eg. if it's not responding) # # 2014-02-26 golux -- created. ## ############### ## Libraries ## ############### use strict; use warnings; use File::Basename; use Data::Dumper; use Getopt::Long; ################## ## User-defined ## ################## my $ipconfig = 'C:\Windows\System32\ipconfig.exe'; my $netsh = 'C:\Windows\System32\netsh.exe'; my $maxwidth = 12; my $re_addr = qr/^(\d+)[.](\d+)[.](\d+)[.](\d+)(?:)?$/; my $re_vm = qr/VMware Network Adapter/i; my $re_dots = qr/[\s.]*:\s*/; my $re_if = qr/^\s*([\sa-z0-9]+):\s*$/i; my $re_ip_addr = qr/IP Address:\s*(\d+[.]\d+[.]\d+[.]\d+)/i; # Order of tags presented my $a_items = [qw[ desc dhcp state mac addr addr6 mask gate dns ]]; # Label used for each tag my $h_label = { 'desc' => 'Desc', 'state' => 'State', 'dhcp' => 'DHCP', 'mac' => 'MAC', 'addr' => 'IP Addr', 'addr6' => 'IPv6 Addr', 'mask' => 'Mask', 'gate' => 'Gateway', 'dns' => 'DNS Suffix', }; # Regex used to capture the value for each tag my $h_regex = { 'desc' => qr/^\s*Description${re_dots}(.+)/i, 'state' => qr/^\s*Media State${re_dots}(.+)/i, 'dhcp' => qr/^\s*Dhcp Enabled${re_dots}(\S+)/i, 'mac' => qr/^\s*Physical Address${re_dots}(\S+)/i, 'addr' => qr/^\s*IP(?:v4)? Address${re_dots}(\S+)/i, 'addr6' => qr/^\s*IPv6 Address${re_dots}(\S+)/i, 'mask' => qr/^\s*Subnet Mask${re_dots}(\S+)/i, 'gate' => qr/^\s*Default Gateway${re_dots}(\S+)/i, 'dns' => qr/^\s*Connection-specific DNS Suffix${re_dots}(\S+)/i +, }; ############# ## Globals ## ############# my $b_all = 0; my $b_vms = 0; my $b_help = 0; ################## ## Command-line ## ################## Getopt::Long::Configure("bundling"); my $go = GetOptions( "a" => \$b_all, "v" => \$b_vms, "h" => \$b_help, ); ################## ## Main Program ## ################## $| = 1; my $iam = basename $0; $b_help and give_help(); my $a_args = [ @ARGV ]; my $a_info = get_config_info(); if (@$a_args > 0) { change_inteface_states($a_info, $a_args); $a_info = get_config_info(); } show_basic_info(); show_interface_info($a_info); ################# ## Subroutines ## ################# sub fatal { my ($msg) = @_; my $lnum = (caller)[2]; my $text = "($iam) FATAL[$lnum]: $msg"; die "$text\n"; } sub give_help { my $syntax = qq{ : Syntax: $iam [switches] [command ...] : : This program shows the currently connected network interfac +es. : : To cycle a network interface #N (disable, then enable it a +gain), : use the command '$iam ~N'. : : Switches : -a ... display all interfaces (even disconnected ones) : -v ... display virtual (ie. VMware) interfaces : -h ... display help message and exit }; $syntax =~ s/(^\s+:)|((?<=\n)\s+:)|(\s+$)//g; die "$syntax\n"; } sub get_config_info { chomp(my @lines = `$ipconfig /all`); my $a_info = [ ]; my $h_if = { }; HANDLE_LINE: foreach my $line (@lines) { $line =~ /^\s*$/ and next; if ($line =~ /$re_if/) { my $if = $1; (my $name = $if) =~ s/^Ethernet adapter\s*//i; $h_if = { 'if' => $if, 'name' => $name }; push @$a_info, $h_if; next; } foreach my $item (@$a_items) { if ($line =~ /$h_regex->{$item}/) { $h_if->{$item} = $1; next HANDLE_LINE; } } } return prune_results($a_info); } sub prune_results { my ($a_info) = @_; my $a_pruned = [ ]; for (my $i = 0; $i < @$a_info; $i++) { my $idx = $i + 1; my $h_if = $a_info->[$i]; my $addr = $h_if->{'addr'} || ""; my $name = $h_if->{'name'} || ""; my $b_is_vm = ($name =~ /$re_vm/)? 1: 0; my $b_conn = 0; $addr =~ s/\(Preferred\)//; if ($addr =~ /$re_addr/) { ($1 or $2 or $3 or $4) and $b_conn = 1; } $h_if->{'conn'} = $b_conn; if ($b_all or ($b_conn and ($b_vms or !$b_is_vm))) { push @$a_pruned, $h_if; } } return $a_pruned; } sub show_basic_info { my $a_conn = [ ]; for (my $i = 0; $i < @$a_info; $i++) { my $idx = $i + 1; my $h_if = $a_info->[$i]; my $b_conn = $h_if->{'conn'} || 0; $b_conn and push @$a_conn, $idx; } my $conn = (0 == @$a_conn)? "(none)": join(", ", @$a_conn); print "\n"; my $cname = $ENV{'COMPUTERNAME'} || ""; my $domain = $ENV{'ComputerDomain'} || ""; my $user = $ENV{'USERNAME'} || ""; print " ------------------------------------------------\n"; $cname and print " Computer .... $cname\n"; $domain and print " Domain ...... $domain\n"; $user and print " User ........ $user\n"; $conn and print " Connected ... $conn\n"; print " ------------------------------------------------\n"; print " Type '$iam -h' for help\n"; print "\n"; } sub show_interface_info { my ($a_info) = @_; my $space = " "; for (my $i = 0; $i < @$a_info; $i++) { my $h_if = $a_info->[$i]; my $idx = $i + 1; my $if = $h_if->{'if'}; printf " #%d [$if]\n", $idx; foreach my $item (@$a_items) { if (my $val = $h_if->{$item} || "") { ($item eq 'mac') and $val =~ s/-/:/g; my $label = $h_label->{$item} || " "; $label or fatal("No label found for item '$item'"); my $dots = "." x ($maxwidth - length($label)); printf "$space %s %s %s\n", $label, $dots, $val; } } print "\n"; } } sub change_inteface_states { my ($a_info, $a_args) = @_; my $nifs = @$a_info; foreach my $arg (@$a_args) { if ($arg !~ /^~(\d+)$/) { print "Invalid arg '$arg' ignored\n"; } else { my $idx = $1; if ($idx < 1 or $idx > $nifs) { print "Skipping interface '$idx' (not in range 1-$nifs +)\n"; } else { my $h_if = $a_info->[$idx-1]; my $name = $h_if->{'name'}; cycle_network_interface($name); } } } print "\n\n"; } sub cycle_network_interface { my ($name) = @_; print "Type [RETURN] to DISABLE, then ENABLE interface '$name':"; <STDIN>; my $netsh_dis = qq{interface set interface "$name" DISABLED}; my $netsh_en = qq{interface set interface "$name" ENABLED}; my $netsh_show = qq{interface ip show addresses "$name"}; print "Command: netsh $netsh_dis\n"; system("$netsh $netsh_dis 1>nul 2>nul"); print "Command: netsh $netsh_en\n"; system("$netsh $netsh_en 1>nul 2>nul"); my $ntries = 32; my $addr = ""; print "Renewing IP address "; while ($ntries-- > 0) { print "."; chomp(my @results = `$netsh $netsh_show`); map { /$re_ip_addr/ and $addr = $1 } @results; $addr and last; select(undef, undef, undef, 0.25); } print "\n"; $addr or fatal("Failed to reenable interface '$name'"); }

say  substr+lc crypt(qw $i3 SI$),4,5

Comment on ifconfig.pl script for Windows
Select or Download Code
Re: ifconfig.pl script for Windows
by Discipulus (Curate) on Feb 27, 2014 at 08:06 UTC
    thanks, only a pity: parse the output with regex bound this program to english only OS.
    Runs fine on english operating systems.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: ifconfig.pl script for Windows
by Anonymous Monk on Feb 27, 2014 at 14:19 UTC
      IO::Interface::Simple looks great too but seems not so good on win. The OP propose instead a win solution.
      In nay case thank you all for your contribution.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: ifconfig.pl script for Windows
by perl-diddler (Hermit) on Feb 28, 2014 at 23:41 UTC
    Worked great once I changed the path for the utils:
    /h> perl win-net.pl sh: C:WindowsSystem32ipconfig.exe: command not found ------------------------------------------------ Computer .... ATHENAE User ........ law Connected ... (none) ------------------------------------------------ Type 'win-net.pl -h' for help /h> gvim win-net.pl [ - my $ipconfig = 'C:\Windows\System32\ipconfig.exe'; - my $netsh = 'C:\Windows\System32\netsh.exe'; + my $ipconfig = 'C:/Windows/System32/ipconfig.exe'; + my $netsh = 'C:/Windows/System32/netsh.exe'; ] /h> perl win-net.pl ------------------------------------------------ Computer .... ATHENAE User ........ law Connected ... 1 ------------------------------------------------ Type 'win-net.pl -h' for help #1 [Ethernet adapter Ish2] Desc ........ Intel(R) Ethernet Converged Network Adapter X540-T2 +#2 DHCP ........ No MAC ......... A0:36:9F:15:CE:28 IP Addr ..... 192.168.4.12(Preferred) Mask ........ 255.255.255.0 Gateway ..... 192.168.4.1
    Probably don't need the absolute path. I've yet to see a windows that didn't have system32 in its path.

    FWIW
    C:/Windows/System32/<cmd> works in cmd.exe... the default shell, and in bash.exe if you run cygwin...

    My mom needed something like this a few days ago -- had to talk her through finding the right numbers (she's 85) from the normal output...

    Maybe I can use this as an excuse to put linux on her machine?? ;-)

Reaped:
by NodeReaper (Curate) on Feb 28, 2014 at 23:44 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2014-12-19 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls