| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966 |
- package Rstat::cmd;
- #
- # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
- #
- use strict;
- use English;
- use FindBin '$Bin';
- use lib "$Bin";
- use base 'Exporter';
- use vars qw(@EXPORT @ISA);
- use Data::Dumper;
- use Rstat::config;
- use Rstat::main;
- use Net::Telnet;
- use Net::SSH::Expect;
- @ISA = qw(Exporter);
- @EXPORT = qw(
- log_cmd
- log_cmd2
- log_cmd3
- log_cmd4
- flush_telnet
- run_command
- netdev_login
- netdev_cmd
- netdev_backup
- netdev_set_port_descr
- netdev_set_hostname
- netdev_set_enable
- netdev_wr_mem
- );
- BEGIN
- {
- #---------------------------------------------------------------------------------
- # Execute command and wait answer from device
- # Args: t - telnet session, $command - command string, $sleep - pause after execute command (enabled by default)
- #
- sub log_cmd {
- my $t = shift;
- my $command = shift;
- my $sleep = shift || 1;
- if (!$t) { die "Telnet session not exists!"; }
- $t->binmode(0);
- $t->errmode('return');
- log_session('Send:'.$command);
- my @ret=$t->cmd(String => $command);
- my @a=();
- foreach my $row (@ret) {
- next if (!$row);
- #zyxel patch
- $row=~ s/\x1b\x37//g;
- #mikrotik patch
- $row=~ s/\x0d\x0d\x0d\x1b\x5b\x39\x39\x39\x39\x42//g;
- #new line
- $row=~ s/\n//g;
- $row=trim($row);
- if ($row) {
- my @tmp=split("\n",$row);
- foreach my $line (@tmp) {
- next if (!$line);
- $line=trim($line);
- next if (!$line);
- push(@a,$line);
- log_session('Get:'.$line);
- }
- }
- }
- select(undef, undef, undef, 0.15) if ($sleep);
- if (scalar(@a)) { return @a; }
- $t->cmd(String => "\n");
- my @tmp=flush_telnet($t);
- foreach my $line (@tmp) {
- next if (!$line);
- push(@a,$line);
- }
- return @a;
- }
- #---------------------------------------------------------------------------------
- # Execute command list array without confirmation from device
- # Args: t - telnet session, $command - array of command string, $sleep - pause after execute command (enabled by default)
- #
- sub log_cmd2 {
- my $t = shift;
- my $command = shift;
- my $sleep = shift || 1;
- if (!$t) { die "Telnet session not exists!"; }
- $t->binmode(0);
- $t->errmode("return");
- $t->cmd(String => "\n");
- $t->buffer_empty;
- my @a;
- foreach my $out (split("\n",$command)){
- if ($out =~ /SLEEP/) {
- if ($out =~ /SLEEP\s+(\d+)/) { sleep($1); } else { sleep(5); };
- next;
- }
- chomp($out);
- log_session('Send:'.$out);
- $t->print($out);
- #sleep 250 ms
- select(undef, undef, undef, 0.25) if ($sleep);
- foreach my $str ($t->waitfor($t->prompt)) {
- $str=trim($str);
- if ($str) {
- my @tmp=split("\n",$str);
- foreach my $line (@tmp) {
- next if (!$line);
- $line=trim($line);
- next if (!$line);
- push(@a,$line);
- log_session('Get:'.$line);
- }
- }
- }
- }
- chomp(@a);
- return @a;
- }
- #---------------------------------------------------------------------------------
- # Execute command list array without confirmation from device and press any key by device prompt
- # Args: t - telnet session, $command - array of command string, $sleep - pause after execute command (enabled by default)
- #
- sub log_cmd3 {
- my $t = shift;
- my $lines = shift;
- my $sleep = shift || 1;
- if (!$t) { die "Telnet session not exists!"; }
- $t->errmode("return");
- $t->buffer_empty;
- $t->binmode(0);
- my @result=();
- foreach my $out (split("\n",$lines)) {
- if ($out =~ /SLEEP/i) {
- if ($out =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
- next;
- }
- chomp($out);
- log_session('Send:'.$out);
- $t->print($out);
- #sleep 250 ms
- select(undef, undef, undef, 0.25) if ($sleep);
- my $end = 0;
- my $get;
- while ($end == 0) {
- foreach my $str ($t->waitfor('/[(#)(\>)(\:)(press)(sure)(Please input)(next page)(continue)(quit)(-- more --)(Confirm)(ESC)(^C$)]/')) {
- $t->print("\n") if $str =~ /ENTER/i;
- $t->print(" ") if $str =~ /ESC/i;
- $t->print(" ") if $str =~ /^C$/i;
- $t->print(" ") if $str =~ /(-- more --)/i;
- $t->print(" ") if $str =~ /SPACE/i;
- $t->print("y\n") if $str =~ /sure/i;
- $t->print("\n") if $str =~ /continue/i;
- $t->print("y\n") if $str =~ /Please input/i;
- $t->print("Y\n") if $str =~ /Confirm/i;
- $t->print("Y\n") if $str =~ /\:/;
- #last line!!!
- $end = 1 if $str =~ /\>/;
- $get .= $str;
- }
- }
- log_debug('Get:'.$get) if ($get);
- push(@result,split(/\n/,$get));
- }
- log_session('Get:'.Dumper(\@result));
- return @result;
- }
- #---------------------------------------------------------------------------------
- # Execute command list array without confirmation from device and press any key by device prompt
- # Args: t - telnet session, $command - array of command string
- #
- sub log_cmd4 {
- my $t = shift;
- my $lines = shift;
- if (!$t) { die "Telnet session not exists!"; }
- $t->errmode("return");
- $t->buffer_empty;
- $t->binmode(0);
- my @result=();
- log_session('Send:'.$lines);
- $t->print($lines);
- #sleep 250 ms
- select(undef, undef, undef, 0.25);
- my ($prematch, $match)=$t->waitfor('/\[.*\] >/');
- log_debug("Get: $prematch, $match");
- push(@result,split(/\n/,$prematch));
- log_session('Get:'.Dumper(\@result));
- return @result;
- }
- #---------------------------------------------------------------------------------
- sub flush_telnet {
- my $t = shift;
- return if (!$t);
- my @a=();
- $t->buffer_empty;
- $t->print("\n");
- foreach my $str ($t->waitfor($t->prompt)) {
- next if (!$str);
- my @tmp=split("\n",$str);
- foreach my $row (@tmp) {
- $row=trim($row);
- next if (!$row);
- log_session('Flush:'.$row);
- push(@a,$row);
- }
- }
- $t->buffer_empty;
- return(@a);
- }
- #---------------------------------------------------------------------------------
- sub run_command {
- my $t=shift;
- my $cmd=shift;
- my $cmd_id = shift || 1;
- my @tmp=();
- if (ref($cmd) eq 'ARRAY') {
- @tmp = @{$cmd};
- } else {
- push(@tmp,$cmd);
- }
- eval {
- foreach my $run_cmd (@tmp) {
- next if (!$run_cmd);
- log_cmd($t,$run_cmd) if ($cmd_id == 1);
- log_cmd2($t,$run_cmd) if ($cmd_id == 2);
- log_cmd3($t,$run_cmd) if ($cmd_id == 3);
- log_cmd4($t,$run_cmd) if ($cmd_id == 4);
- }
- };
- if ($@) { log_error("Abort: $@"); return 0; };
- return 1;
- }
- #---------------------------------------------------------------------------------
- sub netdev_login {
- my $device = shift;
- #skip unknown vendor
- if (!$switch_auth{$device->{vendor_id}}) { return; }
- if (!$switch_auth{$device->{vendor_id}}{proto}) { $switch_auth{$device->{vendor_id}}{proto} = 'telnet'; }
- my $t;
- if ($switch_auth{$device->{vendor_id}}{proto} eq 'telnet') {
- if (!$device->{port}) { $device->{port} = '23'; }
- log_info("Try login to $device->{device_name} ip: $device->{ip} by telnet...");
- #zyxel patch
- if ($device->{vendor_id} eq '4') {
- eval {
- my $t1 = new Net::Telnet (Timeout => 30, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
- $t1->open($device->{ip}) or return;
- if (exists $switch_auth{$device->{vendor_id}}{login}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
- $t1->print($device->{login});
- if (exists $switch_auth{$device->{vendor_id}}{password}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
- $t1->print($device->{password});
- $t1->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
- $t1->cmd("exit");
- $t1->close;
- };
- }
- eval {
- # $t = new Net::Telnet (Timeout => 30, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/", Dump_Log=>'/tmp/1');
- $t = new Net::Telnet (Timeout => 30, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
- $t->open($device->{ip}) or return;
- if (exists $switch_auth{$device->{vendor_id}}{login}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
- if ($device->{vendor_id} eq '9') { $t->print($device->{login}.'+ct400w'); } else { $t->print($device->{login}); }
- if (exists $switch_auth{$device->{vendor_id}}{password}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
- $t->print($device->{password});
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
- if (exists $switch_auth{$device->{vendor_id}}{enable}) {
- $t->print($switch_auth{$device->{vendor_id}}{enable});
- $t->print($device->{enable_password});
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
- }
- if ($device->{vendor_id} eq '2') {
- log_cmd($t,"terminal datadump");
- log_cmd($t,"no logging console");
- }
- if ($device->{vendor_id} eq '5') { log_cmd($t,"terminal page-break disable"); }
- if ($device->{vendor_id} eq '6') { log_cmd($t,"terminal length 0"); }
- if ($device->{vendor_id} eq '9') { log_cmd($t,"/system note set show-at-login=no"); }
- if ($device->{vendor_id} eq '16') { log_cmd($t,"terminal width 0"); }
- if ($device->{vendor_id} eq '17') {
- log_cmd($t,"more displine 50");
- log_cmd($t,"more off");
- }
- if ($device->{vendor_id} eq '38') {
- log_cmd($t,"disable cli prompting");
- log_cmd($t,"disable clipaging");
- }
- };
- if ($@) { log_error("Login to $device->{device_name} ip: $device->{ip} by telnet aborted: $@"); } else { log_info("Login to $device->{device_name} ip: $device->{ip} by telnet success!"); }
- }
- if ($switch_auth{$device->{vendor_id}}{proto} eq 'ssh') {
- if (!$device->{port}) { $device->{port} = '22'; }
- log_info("Try login to $device->{device_name} ip: $device->{ip} by ssh...");
- eval {
- $t = Net::SSH::Expect->new (
- host=>$device->{ip},
- port=>$device->{port},
- user=>$device->{login},
- password=>$device->{password},
- timeout=>1,
- raw_pty=>1,
- ssh_option=>'-o PubkeyAcceptedKeyTypes=+ssh-dss \
- -o KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1 \
- -o HostKeyAlgorithms=+ssh-dss \
- -o LogLevel=quiet \
- -o UserKnownHostsFile=/dev/null \
- -o StrictHostKeyChecking=no'
- );
- $t->run_ssh() or die "SSH process couldn't start: $!";
- my $retry_count = 0;
- my $max_retry_count = 30;
- my $rc;
- while(1){
- $rc = eval{$t->login($switch_auth{$device->{vendor_id}}{login},$switch_auth{$device->{vendor_id}}{password},0);};
- last if defined $rc;
- return if $retry_count >= $max_retry_count;
- $retry_count++;
- sleep 1;
- }
- if ($rc !~ /$switch_auth{$device->{vendor_id}}{prompt}/) { return; }
- if (exists $switch_auth{$device->{vendor_id}}{enable}) {
- $t->send($switch_auth{$device->{vendor_id}}{enable}."\n");
- # $t->print($device->{enable_password});
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '2') {
- $t->send("terminal datadump");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- $t->send("no logging console");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '5') {
- $t->send("terminal page-break disable");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '6') {
- $t->send("terminal length 0");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '9') {
- $t->send("/system note set show-at-login=no");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '16') {
- $t->send("terminal width 0");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '17') {
- $t->send("more displine 50");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- $t->send("more off");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '38') {
- $t->send("disable cli prompting");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- $t->send("disable clipaging");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- };
- if ($@) { log_error("Login to $device->{device_name} ip: $device->{ip} by telnet aborted: $@"); } else { log_info("Login to $device->{device_name} ip: $device->{ip} by ssh success!"); }
- }
- return $t;
- }
- #---------------------------------------------------------------------------------
- sub netdev_set_enable {
- my $session = shift;
- my $device = shift;
- return if (!exists $switch_auth{$device->{vendor_id}}{enable});
- my $cmd = "$switch_auth{$device->{vendor_id}}{enable}
- SLEEP 5
- $device->{enable_password}
- ";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
- }
- #---------------------------------------------------------------------------------
- sub netdev_cmd {
- my ($device,$session,$proto,$cmd,$telnet_version)=@_;
- my @result=();
- if ($proto eq 'ssh') {
- my @tmp=();
- if (ref($cmd) eq 'ARRAY') { @tmp = @{$cmd}; } else { push(@tmp,$cmd); }
- eval {
- foreach my $run_cmd (@tmp) {
- next if (!$run_cmd);
- if ($run_cmd =~ /SLEEP/i) {
- if ($run_cmd =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
- next;
- }
- log_session('Send:'.$cmd);
- $session->send($cmd."\n");
- my $line;
- while ( defined ($line = $session->read_line()) ) { push(@result,$line); }
- select(undef, undef, undef, 0.25);
- $session->waitfor($switch_auth{$device->{vendor_id}}{prompt}, 1);
- }
- log_session('Get:'.Dumper(\@result));
- };
- if ($@) { log_error("Abort: $@"); return 0; };
- }
- if ($proto eq 'telnet') {
- my @tmp=();
- if (!$telnet_version) { $telnet_version = 1; }
- if (ref($cmd) eq 'ARRAY') { @tmp = @{$cmd}; } else { push(@tmp,$cmd); }
- eval {
- foreach my $run_cmd (@tmp) {
- next if (!$run_cmd);
- my @ret=();
- @ret=log_cmd($session,$run_cmd) if ($telnet_version == 1);
- @ret=log_cmd2($session,$run_cmd) if ($telnet_version == 2);
- @ret=log_cmd3($session,$run_cmd) if ($telnet_version == 3);
- @ret=log_cmd4($session,$run_cmd) if ($telnet_version == 4);
- if (scalar @ret) { push(@result,@ret); }
- select(undef, undef, undef, 0.25);
- }
- };
- if ($@) { log_error("Abort: $@"); return 0; };
- }
- return @result;
- }
- #---------------------------------------------------------------------------------
- sub netdev_backup {
- my $device = shift;
- my $tftp_ip = shift;
- #eltex
- if ($device->{vendor_id} eq '2') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "upload startup-config tftp $tftp_ip $device->{device_name}.cfg";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
- };
- }
- #huawei
- if ($device->{vendor_id} eq '3') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "tftp $tftp_ip put vrpcfg.zip $device->{device_name}.zip";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
- };
- }
- #zyxel
- if ($device->{vendor_id} eq '4') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "copy running-config tftp $tftp_ip $device->{device_name}.cfg";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
- };
- }
- #raisecom
- if ($device->{vendor_id} eq '5') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "upload startup-config tftp $tftp_ip $device->{device_name}.cfg";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
- };
- }
- #SNR
- if ($device->{vendor_id} eq '6') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "copy running-config tftp://$tftp_ip/$device->{device_name}.cfg
- Y
- ";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
- };
- }
- #Dlink
- if ($device->{vendor_id} eq '7') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "upload cfg_toTFTP $tftp_ip dest_file $device->{device_name}.cfg src_file config.cfg";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
- };
- }
- #allied telesys x210,x610
- if ($device->{device_model_id} ~~ [50..53]) {
- eval {
- my $session = netdev_login($device);
- my $cmd = "copy running-config tftp
- SLEEP 2
- $tftp_ip
- SLEEP 2
- $device->{device_name}.cfg
- SLEEP 5
- ";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
- };
- }
- #allied telesys 8000
- if ($device->{device_model_id} eq '3') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "copy running-config tftp://$tftp_ip/$device->{device_name}.cfg";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
- };
- }
- #allied telesys 8100
- if ($device->{device_model_id} eq '4') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "copy flash tftp $tftp_ip boot.cfg";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
- rename $tftp_dir."/boot.cfg",$tftp_dir."/$device->{device_name}".".cfg";
- };
- }
- #mikrotik
- if ($device->{vendor_id} eq '9') {
- eval {
- my $session = netdev_login($device);
- log_cmd($session,"/system note set show-at-login=no",1,$session->prompt);
- my $cmd = "/export";
- my @netdev_cfg = netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,4);
- write_to_file($tftp_dir."/$device->{device_name}.cfg","Config for $device->{device_name}",0);
- foreach my $row (@netdev_cfg) { write_to_file($tftp_dir."/$device->{device_name}.cfg",$row,1); }
- };
- }
- #cisco
- if ($device->{vendor_id} eq '16') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "
- copy system:/running-config tftp:
- SLEEP 2
- $tftp_ip
- SLEEP 2
- $device->{device_name}.cfg
- SLEEP 5
- ";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
- };
- }
- #maipu
- if ($device->{vendor_id} eq '17') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "
- filesystem
- copy running-config tftp $tftp_ip $device->{device_name}.cfg
- SLEEP 5
- exit
- ";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
- };
- }
- #Qtech
- if ($device->{vendor_id} eq '38') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "upload configuration tftp $tftp_ip $device->{device_name}.cfg";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
- };
- }
- #Extreme
- if ($device->{vendor_id} eq '39') {
- eval {
- my $session = netdev_login($device);
- my $cmd = "upload configuration $tftp_ip $device->{device_name}.cfg vr \"VR-Default\"";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
- };
- }
- }
- #---------------------------------------------------------------------------------
- sub netdev_set_port_descr {
- my $session = shift;
- my $device = shift;
- my $port = shift;
- my $port_num = shift;
- my $descr = shift;
- my $cmd;
- my $telnet_cmd_mode = 4;
- #eltex
- if ($device->{vendor_id} eq '2') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $cmd = "
- conf t
- interface $port
- $descr
- exit
- exit";
- }
- #huawei
- if ($device->{vendor_id} eq '3') {
- if (!$descr) { $descr = "undo description"; } else { $descr = "description $descr"; }
- $cmd = "
- interface $port
- $descr
- quit";
- }
- #zyxel
- if ($device->{vendor_id} eq '4') {
- if (!$descr) { $descr = "name "; } else { $descr = "name $descr"; }
- $cmd = "
- conf t
- interface port-channel $port_num
- $descr
- exit
- exit";
- }
- #raisecom
- if ($device->{vendor_id} eq '5') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $cmd = "
- conf t
- interface $port_num
- $descr
- exit
- exit";
- }
- #SNR
- if ($device->{vendor_id} eq '6') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $cmd = "
- conf t
- interface $port
- $descr
- exit
- exit";
- }
- #Dlink
- if ($device->{vendor_id} eq '7') {
- if (!$descr) { $descr = "clear_description"; } else { $descr = "description $descr"; }
- $cmd = "config ports $port $descr";
- }
- #allied telesys x210,x610
- if ($device->{vendor_id} eq '8') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $telnet_cmd_mode = 2;
- $cmd = "
- conf t
- interface $port
- $descr
- exit
- exit";
- }
- #allied telesys 8000
- if ($device->{device_model_id} eq '3') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $telnet_cmd_mode = 2;
- $cmd = "
- conf
- interface ethernet $port
- $descr
- exit
- exit";
- }
- #allied telesys 8100
- if ($device->{device_model_id} eq '4') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $telnet_cmd_mode = 2;
- $cmd = "
- conf t
- interface $port
- $descr
- exit
- exit";
- }
- #mikrotik
- if ($device->{vendor_id} eq '9') {
- $cmd = "/interface ethernet
- set [ find default-name=$port ] comment=$descr
- ";
- }
- #cisco
- if ($device->{vendor_id} eq '16') {
- if (!$descr) { $descr = 'description ""'; } else { $descr = "description $descr"; }
- $cmd = "
- conf t
- interface $port
- $descr
- exit
- exit";
- }
- #maipu
- if ($device->{vendor_id} eq '17') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $cmd = "
- conf t
- port $port
- $descr
- exit
- exit";
- }
- #Qtech
- if ($device->{vendor_id} eq '38') {
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $cmd = "
- conf t
- interface $port
- $descr
- exit
- exit";
- }
- #Extreme
- if ($device->{vendor_id} eq '39') {
- if ($descr) {
- $cmd = "configure port $port_num display $descr";
- } else {
- $cmd = "unconfigure port $port_num display";
- }
- }
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
- }
- #---------------------------------------------------------------------------------
- sub netdev_set_hostname {
- my $session = shift;
- my $device = shift;
- my $cmd;
- my $telnet_cmd_mode = 4;
- #eltex
- if ($device->{vendor_id} eq '2') {
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #huawei
- if ($device->{vendor_id} eq '3') {
- $cmd = "sysname $device->{device_name}";
- }
- #zyxel
- if ($device->{vendor_id} eq '4') {
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #raisecom
- if ($device->{vendor_id} eq '5') {
- $cmd = "hostname $device->{device_name}";
- }
- #SNR
- if ($device->{vendor_id} eq '6') {
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #Dlink
- if ($device->{vendor_id} eq '7') {
- $cmd = "config hostname $device->{device_name}";
- }
- #allied telesys x210,x610 - default
- if ($device->{vendor_id} eq '8') {
- $telnet_cmd_mode = 2;
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #allied telesys 8000
- if ($device->{device_model_id} eq '3') {
- $telnet_cmd_mode = 2;
- $cmd = "
- conf
- hostname $device->{device_name}
- exit";
- }
- #allied telesys 8100
- if ($device->{device_model_id} eq '4') {
- $telnet_cmd_mode = 2;
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #mikrotik
- if ($device->{vendor_id} eq '9') {
- $cmd = "/system identity
- set name=$device->{device_name}
- ";
- }
- #cisco
- if ($device->{vendor_id} eq '16') {
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #maipu
- if ($device->{vendor_id} eq '17') {
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #Qtech
- if ($device->{vendor_id} eq '38') {
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #Extreme
- if ($device->{vendor_id} eq '39') {
- $cmd = "configure snmp sysName $device->{device_name}";
- }
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
- }
- #---------------------------------------------------------------------------------
- sub netdev_wr_mem {
- my $session = shift;
- my $device = shift;
- my $cmd;
- my $telnet_cmd_mode = 4;
- #eltex
- if ($device->{vendor_id} eq '2') {
- $cmd = "wr
- Y";
- }
- #huawei
- if ($device->{vendor_id} eq '3') {
- $cmd = "quit
- save
- Y
- ";
- }
- #zyxel
- if ($device->{vendor_id} eq '4') { $cmd = "wr mem"; }
- #raisecom
- if ($device->{vendor_id} eq '5') { $cmd = "wr"; }
- #SNR
- if ($device->{vendor_id} eq '6') {
- $cmd="copy running-config startup-config
- Y";
- }
- #Dlink
- if ($device->{vendor_id} eq '7') { $cmd="save"; }
- #allied telesys x210,x610
- if ($device->{vendor_id} eq '8') {
- $cmd = "wr
- Y";
- }
- #allied telesys 8000
- if ($device->{device_model_id} eq '3') {
- $telnet_cmd_mode=2;
- $cmd = "copy running-config startup-config
- Y
- ";
- }
- #allied telesys 8100
- if ($device->{device_model_id} eq '4') {
- $cmd = "wr
- Y";
- }
- #cisco
- if ($device->{vendor_id} eq '16') { $cmd="wr_mem"; }
- #maipu
- if ($device->{vendor_id} eq '17') {
- $cmd = "wr
- Yes
- ";
- }
- #Qtech
- if ($device->{vendor_id} eq '38') {
- $cmd = "copy running-config startup-config
- Y
- ";
- }
- #Extreme
- if ($device->{vendor_id} eq '39') { $cmd="save configuration primary"; }
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
- }
- #---------------------------------------------------------------------------------
- 1;
- }
|