| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004 |
- package Rstat::cmd;
- #
- # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
- #
- use utf8;
- 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_set_auth
- 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_set_auth {
- my $device = shift;
- #router
- if ($device->{device_type} eq '2') {
- #mikrotik
- if ($device->{vendor_id} eq '9') { $device->{port}=$config_ref{router_port}; }
- $device->{login}=$config_ref{router_login};
- $device->{password}=$config_ref{router_password};
- }
- #switch
- if ($device->{device_type} eq '1' or $device->{vendor_id} eq '3') {
- #mikrotik
- # if ($device->{vendor_id} eq '9') { $device->{port}=$config_ref{router_port}; }
- $device->{login}=$sw_login;
- $device->{password}=$sw_password;
- }
- return $device;
- }
- #---------------------------------------------------------------------------------
- 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'; }
- if (!$device->{port} and $switch_auth{$device->{vendor_id}}{port}) { $device->{port} = $switch_auth{$device->{vendor_id}}{port}; }
- 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} $device->{ip}:$device->{port} by telnet...");
- #zyxel patch
- if ($device->{vendor_id} eq '4') {
- eval {
- my $t1 = new Net::Telnet (Timeout => 5, 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 => 10, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/", Dump_Log=>'/tmp/1');
- $t = new Net::Telnet (Timeout => 10, 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_cmd4($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} $device->{ip}:$device->{port} 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\r");
- # $t->print($device->{enable_password});
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '2') {
- $t->send("terminal datadump\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- $t->send("no logging console\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '5') {
- $t->send("terminal page-break disable\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '6') {
- $t->send("terminal length 0\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '9') {
- $t->send("/system note set show-at-login=no\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '16') {
- $t->send("terminal width 0\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '17') {
- $t->send("more displine 50\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- $t->send("more off\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- }
- if ($device->{vendor_id} eq '38') {
- $t->send("disable cli prompting\n\r");
- $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
- $t->send("disable clipaging\n\r");
- $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:'.$run_cmd);
- $session->send($run_cmd."\n\r");
- my $chunk;
- while ($chunk = $session->peek(1)) {
- my $ret =$session->eat($chunk);
- if (ref($ret) eq 'ARRAY') {
- push(@result,@{$ret});
- } else {
- my @norm_text = split(/\n/,$ret);
- foreach my $row (@norm_text) { push(@result,trim($row)); }
- }
- }
- select(undef, undef, undef, 0.25);
- }
- # 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 = "quit";
- netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
- $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') {
- $telnet_cmd_mode = 1;
- 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') {
- $telnet_cmd_mode = 1;
- if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
- $cmd = "
- conf t
- interface $port
- $descr
- exit
- exit";
- }
- #Dlink
- if ($device->{vendor_id} eq '7') {
- $telnet_cmd_mode = 1;
- if (!$descr) { $descr = "clear_description"; } else { $descr = "description $descr"; }
- $cmd = "config ports $port_num $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') {
- $telnet_cmd_mode = 4;
- if (!$descr) { $descr='""'; } else { $descr='"'.$descr.'"'; }
- $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') {
- $telnet_cmd_mode = 1;
- $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') {
- $telnet_cmd_mode = 1;
- $cmd = "
- conf t
- hostname $device->{device_name}
- exit";
- }
- #Dlink
- if ($device->{vendor_id} eq '7') {
- $telnet_cmd_mode = 1;
- $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') {
- $telnet_cmd_mode = 4;
- $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;
- }
|