#!/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 interfaces. : : To cycle a network interface #N (disable, then enable it again), : 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':"; ; 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'"); }