📄 netsh
字号:
#!/usr/bin/perluse Getopt::Std;use DBI;use Term::ReadLine;use SNMP;use AutoLoader;use IO::File;BEGIN { $opts{'t'} = ! eval { require Text::FormatTable; }; $ansicolor = eval { require Term::ANSIColor; };}# override some of the functions to force our colorized semantics.# This is really ugly, but if you pass colorized strings directly to# Text::FormatTable then it calculates the string lengths incorrectly.# Children: don't try this at home. We're trained professionals.if ($colorize) { eval { # # colorized strings. # package color_string; require Term::ANSIColor; use overload '""' => \&string_it ; sub string_it { if ($_[0][3]) { if ($_[0][3] == 1) { return color_it(); } else { $_[0][3] -= 1; return $_[0][1]; } } return $_[0][1]; } sub colorize_next { $_[0][3] = 2; } sub color_it { my $str = $_[1] || $_[0][1]; return $_[0][0] . $str . $_[0][2]; } sub new { my $this = [Term::ANSIColor::color($_[2]), $_[1], Term::ANSIColor::color('reset')]; return bless($this, $_[0]); } }}if ($opts{'t'} == 0 && $colorize) { eval { package Text::FormatTable; sub _l_box($$) { my ($width, $text) = @_; my $lines = _wrap($width, $text); map { if (ref($text) eq "color_string") { $_ .= $text->color_it($_) . ' 'x($width-length($_)); } else { $_ .= ' 'x($width-length($_)); } } @$lines; return $lines; } sub _r_box($$) { my ($width, $text) = @_; my $lines = _wrap($width, $text); map { if (ref($text) eq "color_string") { $_ = ' 'x($width-length($_)) . $text->color_it($_); } else { $_ = ' 'x($width-length($_)) . $_; } } @$lines; return $lines; } }}if (!$ansicolor) { $begin = $end = "*";}$SNMP::use_enums=1;#defaults$opts{'d'} = "\t";$opts{'v'} = 1;$opts{'l'} = 'authNoPriv';$params = "";getopts('hd:tR:u:l:v:a:A:x:X:p:c:t:r:',\%opts);usage() if ($#ARGV == -1 || $opts{'h'});my %parammap = { 'v' => 'Version', 'u' => 'SecName', 'a' => 'AuthProto', 'A' => 'AuthPass', 'x' => 'PrivProto', 'X' => 'PrivPass', 'p' => 'RemotePort', 't' => 'Timeout', 'r' => 'Retries', 'c' => 'Community', 'l' => 'SecLevel' };foreach my $x (keys(%opts)) { if ($parammap{$x}) { $params .= ";ad_SNMP_$parammap{$x}=$x"; } push @sessparams,$parammap{$x},$x;}my $host = shift @ARGV;$params .= ";ad_SNMP_DestHost=" . $host;push @sessparms,'DestHost', $host;# connect to the DBI interface$AnyData::Storage::SNMP::debugre = $opts{'R'} if ($opts{'R'});($dbh = DBI->connect("dbi:AnyData:ad_default=SNMP$params")) || die "\tConnection problem: $DBI::errstr\n";$AnyData::Storage::SNMP::debugre = $opts{'R'} if ($opts{'R'});$prompt = "netsh> ";load_rcs();# setup terminal prompter$ENV{'PERL_RL'}='o=0' if (!exists($ENV{'PERL_RL'}));# the ornaments are too ugly$term = new Term::ReadLine 'netsh';if ($#ARGV >= 0) { # command line command netsh(join(" ",@ARGV));} else { # interactive shell while($cmd = $term->readline($prompt)) { last if ($cmd eq "exit" || $cmd eq "quit" || $cmd eq "q"); netsh($cmd, \%conf); }}# load all configuration files we can find.sub load_rcs { if (-f "$ENV{'HOME'}/.snmp/netshrc") { source_file("$ENV{'HOME'}/.snmp/netshrc"); } if (-d "$ENV{'HOME'}/.snmp/netsh") { foreach my $i (glob("$ENV{'HOME'}/.snmp/netsh/*")) { if (-f "$i" && "$i" !~ /.*(~|.bak)$/) { source_file("$i"); } } }}# command definition for sourcing a particular filesub source_file { my $fh = new IO::File; if ($fh->open("<$_[0]")) { while(<$fh>) { if (s/<<\s*(\w+)$//) { my $stopat = $1; my $lines = $_; while(<$fh>) { last if (/$stopat/); $lines .= $_; } $_ = $lines; } netsh($_); } } else { print STDERR "no such file: $_[0]\n"; }}# export data into an external filesub my_export { shift; if (!$insh) { my $cmd = "create table exporttable (" . join (" varchar(255), ",@{$sth->{NAME}}) . " varchar(255))"; $exporth->do($cmd); $cmd = "insert into exporttable values(" . ('?, ' x ($#_)) . "?)"; $insh = $exporth->prepare($cmd); } $insh->execute(@_);}# the main processing function.sub netsh { my $stmt = shift; chomp($stmt); # remove trailing white space $stmt =~ s/^\s+//; # remove leading white space $stmt =~ s/;*$//; # get rid of trailing semicolons return if ($stmt =~ /^\s*$/); return if ($stmt =~ /^\s*\#/); my ($name, $args) = ($stmt =~ /^(\w+)\s*(.*)$/); #define alias# print "doing [$multi_alias]: $stmt\n"; if ($name eq "alias" || $multi_alias) { if ($multi_alias) { if ($stmt =~ /^prompt\s+(\d+)\s+[\"\'](.+)[\"\']/) { $aliases{$current_alias}{'prompts'}[$1] = $2; return; } elsif ($stmt =~ /^prompt\s+(\d+)\s+(.+)/) { my $x = $2; my $spot = $1; $x =~ s/\s+$//; $aliases{$current_alias}{'prompts'}[$spot] = "$x "; return; } elsif ($stmt =~ /^\s*\}\s*$/) { $prompt = $oprompt; $multi_alias = 0; return; } push @{$aliases{$current_alias}{'definition'}},$stmt; return; } $stmt =~ s/^alias\s+//; if ($args eq "") { foreach $i (sort keys(%aliases)) { display_alias($i); } return; } ($name, $args) = ($stmt =~ /^(\w+)\s*(.*)$/); if ($args eq "") { display_alias($name); return; }# print "alias: $name $args\n"; if ($args eq "{") { $oprompt = $prompt; $prompt = "define $name> "; $current_alias = $name; $multi_alias = 1; $aliases{$name}{'definition'} = []; return; } $aliases{$name}{'definition'} = $args; return; } #eval if ($name eq "eval") { eval $args; return; } #eval just vars if ($name eq "evalvars") {# print "args1:",$args,"\n"; $args =~ s/\$(\w+)/$$1/eg;# print "args2:",$args,"\n"; netsh($args); return; } #eval aliases while (exists $aliases{$name}) {# print "modified: $stmt -> "; my @ARGS = split(/\s+/,$args); my $statements; if (ref($aliases{$name}{'definition'}) eq "ARRAY") { $statements = $aliases{$name}{'definition'}; # maybe prompt for values if ($#{$aliases{$name}{'prompts'}} > -1) { my $i; for($i = 1; $i <= $#{$aliases{$name}{'prompts'}}; $i++) { if (!$ARGS[$i-1] && $term) { $ARGS[$i-1] = $term->readline($aliases{$name}{'prompts'}[$i]); } } } } else { $statements = [$aliases{$name}{'definition'}]; } foreach my $stmt (@$statements) { #print "$stmt -> "; $stmt =~ s/\\(\d+)/$ARGS[$1-1]/g;# print "running $stmt\n"; ($name, $args) = ($stmt =~ /^(\w+)\s*(.*)$/); netsh($stmt); } return; } if ($stmt =~ /^rehash$/) { load_rcs(); return; } my $subfn; if ($stmt =~ /^eval (.*)/) { eval $1; } if ($stmt =~ s/^printf\s+(\".*\")\s*(.*)/$2/) { if ($2 eq "") { print eval $1; return; } $subfn = \&my_printf; $stmt = $2; $printfmt = $1; } # special show columns statement if ($stmt =~ /^show columns from (\w+)$/) { my $mibnode = $SNMP::MIB{$1}; my $entrynode = $mibnode->{children}[0]; if (!defined($mibnode) || !defined($entrynode)) { print STDERR "no such table: $1\n"; return; } if ($opts{'t'}) { map { print $_->{label},"\n"; } sort { $a->{subID} <=> $b->{subID}} @{$entrynode->{children}}; } else { $table = Text::FormatTable->new('|r|'); $table->rule('-'); $table->head('Column'); $table->rule('-'); map { $table->row($_->{label}); } sort { $a->{subID} <=> $b->{subID}} @{$entrynode->{children}}; $table->rule('-'); print $table->render(); } return; } if ($stmt =~ /^source\s+(.*)/) { source_file($1); return; } if ($stmt =~ s/^export\s+(\S+)\s+(.*)/$2/) { $insh = undef; unlink($1); $exporth = DBI->connect('dbi:AnyData:'); $exporth->func('exporttable','CSV',$1,'ad_catalog'); $subfn = \&my_export; } if ($stmt =~ /^import\s+(\S+)/) { my $exporth = DBI->connect('dbi:AnyData:'); $exporth->func('exporttable','CSV',$1,'ad_catalog'); my $selh = $exporth->prepare("select * from exporttable"); $selh->execute(); $old_data = []; while(my $row = $selh->fetchrow_arrayref) { push @$old_data, @$row; } $selh->finish(); $exporth->disconnect(); return; } if ($stmt =~ /^diff\s+(.*)/) { $running_watch = 2; netsh($1); $running_watch = 0; return; } if ($stmt =~ /^watch\s+(.*)/) { $running_watch = 1; my $cmd = $1; my $delay = 1; my $clear = `clear`; if ($cmd =~ s/^(\d+)\s+(.*)/$2/) { $delay = $1; $cmd = $2; } $SIG{'TERM'} = sub { $running_watch = 0; }; $SIG{'INT'} = sub { $running_watch = 0; }; while($running_watch) { print $clear; netsh($cmd); sleep($delay); } $SIG{'TERM'} = \&exit; $SIG{'INT'} = \&exit; return; } # we have an SQL statement. process it. if ($stmt =~ /^(select|insert|update|delete)/) { $sth = $dbh->prepare($stmt); $sth->execute(); if ($stmt =~ /^select/) { my $table; my $older_data = $old_data; if ($running_watch == 1) { $old_data = [];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -