Skip to content

Instantly share code, notes, and snippets.

@somian
Created December 8, 2014 05:48
Show Gist options
  • Save somian/ebfb527d3d113315985e to your computer and use it in GitHub Desktop.
Save somian/ebfb527d3d113315985e to your computer and use it in GitHub Desktop.
windows netwrk adapters
#!perl
use PerlIO;
use strict;
use warnings;
my $PSF;
my $networkshellcmd = q[netsh];
my @networkshellpar = qw[-c lan show interfaces];
my @startprocess = ($networkshellcmd, @networkshellpar);
my $CPID = open($PSF, "-|:raw", @startprocess);
die "Failed attempt to open $networkshellcmd" unless $CPID;
# scope the following operations.
{
my $fwjo = qr'There are (.) interfaces on the system:';
my $pthrough;
my $wandat;
ONI:
while
( defined($pthrough = readline($PSF))) {
if ($pthrough =~ $fwjo) {
# reset input record sep for the DOS program output coming in from subprocess.
$/ = "\cM\cJ\cM\cJ";
next ONI
}
if ($pthrough =~/\s*State \s*: Connected\./)
{
$wandat .= $pthrough;
}
}
no strict;
no warnings qw(once);
my (%intof, %dats);
my $hit=1;
my $mflen = '-' . length 'Physical Address';
while ($hit and $wandat =~/\s*([^:]+)\s*[:]\s([^:]+)\cM?\cJ/gsm) {
($hit = $1) || last ;
my $w = $2;
die unless $w;
( $stn = $1 )=~ s/\s+ \z//x;
next if $stn eq 'State';
{ $intof {$stn} = $w; next }
}
# Wanted "Name", "Physical Address", "Description".
while (my($t_,$u_) = each %intof) {
$t_ =~ s/\s+ \z//x;
$u_ =~ s/\s+ \z//x;
$dats{$t_} = $u_;
}
printf STDERR qq[%${mflen}s => %s\n], $_, $dats{$_} for keys %dats;
} # // end scoping //
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment