cmd.pm 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. package Rstat::cmd;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use strict;
  7. use English;
  8. use FindBin '$Bin';
  9. use lib "$Bin";
  10. use base 'Exporter';
  11. use vars qw(@EXPORT @ISA);
  12. use Data::Dumper;
  13. use Rstat::config;
  14. use Rstat::main;
  15. use Net::Telnet;
  16. use Net::OpenSSH;
  17. @ISA = qw(Exporter);
  18. @EXPORT = qw(
  19. log_cmd
  20. log_cmd2
  21. log_cmd3
  22. log_cmd4
  23. flush_telnet
  24. run_command
  25. netdev_set_auth
  26. netdev_login
  27. netdev_cmd
  28. netdev_backup
  29. netdev_set_port_descr
  30. netdev_set_hostname
  31. netdev_set_enable
  32. netdev_wr_mem
  33. );
  34. BEGIN
  35. {
  36. #---------------------------------------------------------------------------------
  37. # Execute command and wait answer from device
  38. # Args: t - telnet session, $command - command string, $sleep - pause after execute command (enabled by default)
  39. #
  40. sub log_cmd {
  41. my $t = shift;
  42. my $command = shift;
  43. my $sleep = shift || 1;
  44. if (!$t) { die "Telnet session not exists!"; }
  45. $t->binmode(0);
  46. $t->errmode('return');
  47. log_session('Send:'.$command);
  48. my @ret=$t->cmd(String => $command);
  49. my @a=();
  50. foreach my $row (@ret) {
  51. next if (!$row);
  52. #zyxel patch
  53. $row=~ s/\x1b\x37//g;
  54. #mikrotik patch
  55. $row=~ s/\x0d\x0d\x0d\x1b\x5b\x39\x39\x39\x39\x42//g;
  56. #new line
  57. $row=~ s/\n//g;
  58. $row=trim($row);
  59. if ($row) {
  60. my @tmp=split("\n",$row);
  61. foreach my $line (@tmp) {
  62. next if (!$line);
  63. $line=trim($line);
  64. next if (!$line);
  65. push(@a,$line);
  66. log_session('Get:'.$line);
  67. }
  68. }
  69. }
  70. select(undef, undef, undef, 0.15) if ($sleep);
  71. if (scalar(@a)) { return @a; }
  72. $t->cmd(String => "\n");
  73. my @tmp=flush_telnet($t);
  74. foreach my $line (@tmp) {
  75. next if (!$line);
  76. push(@a,$line);
  77. }
  78. return @a;
  79. }
  80. #---------------------------------------------------------------------------------
  81. # Execute command list array without confirmation from device
  82. # Args: t - telnet session, $command - array of command string, $sleep - pause after execute command (enabled by default)
  83. #
  84. sub log_cmd2 {
  85. my $t = shift;
  86. my $command = shift;
  87. my $sleep = shift || 1;
  88. if (!$t) { die "Telnet session not exists!"; }
  89. $t->binmode(0);
  90. $t->errmode("return");
  91. $t->cmd(String => "\n");
  92. $t->buffer_empty;
  93. my @a;
  94. foreach my $out (split("\n",$command)){
  95. if ($out =~ /SLEEP/) {
  96. if ($out =~ /SLEEP\s+(\d+)/) { sleep($1); } else { sleep(5); };
  97. next;
  98. }
  99. chomp($out);
  100. log_session('Send:'.$out);
  101. $t->print($out);
  102. #sleep 250 ms
  103. select(undef, undef, undef, 0.25) if ($sleep);
  104. foreach my $str ($t->waitfor($t->prompt)) {
  105. $str=trim($str);
  106. if ($str) {
  107. my @tmp=split("\n",$str);
  108. foreach my $line (@tmp) {
  109. next if (!$line);
  110. $line=trim($line);
  111. next if (!$line);
  112. push(@a,$line);
  113. log_session('Get:'.$line);
  114. }
  115. }
  116. }
  117. }
  118. chomp(@a);
  119. return @a;
  120. }
  121. #---------------------------------------------------------------------------------
  122. # Execute command list array without confirmation from device and press any key by device prompt
  123. # Args: t - telnet session, $command - array of command string, $sleep - pause after execute command (enabled by default)
  124. #
  125. sub log_cmd3 {
  126. my $t = shift;
  127. my $lines = shift;
  128. my $sleep = shift || 1;
  129. if (!$t) { die "Telnet session not exists!"; }
  130. $t->errmode("return");
  131. $t->buffer_empty;
  132. $t->binmode(0);
  133. my @result=();
  134. foreach my $out (split("\n",$lines)) {
  135. if ($out =~ /SLEEP/i) {
  136. if ($out =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
  137. next;
  138. }
  139. chomp($out);
  140. log_session('Send:'.$out);
  141. $t->print($out);
  142. #sleep 250 ms
  143. select(undef, undef, undef, 0.25) if ($sleep);
  144. my $end = 0;
  145. my $get;
  146. while ($end == 0) {
  147. foreach my $str ($t->waitfor('/[(#)(\>)(\:)(press)(sure)(Please input)(next page)(continue)(quit)(-- more --)(Confirm)(ESC)(^C$)]/')) {
  148. $t->print("\n") if $str =~ /ENTER/i;
  149. $t->print(" ") if $str =~ /ESC/i;
  150. $t->print(" ") if $str =~ /^C$/i;
  151. $t->print(" ") if $str =~ /(-- more --)/i;
  152. $t->print(" ") if $str =~ /SPACE/i;
  153. $t->print("y\n") if $str =~ /sure/i;
  154. $t->print("\n") if $str =~ /continue/i;
  155. $t->print("y\n") if $str =~ /Please input/i;
  156. $t->print("Y\n") if $str =~ /Confirm/i;
  157. $t->print("Y\n") if $str =~ /\:/;
  158. #last line!!!
  159. $end = 1 if $str =~ /\>/;
  160. $get .= $str;
  161. }
  162. }
  163. log_debug('Get:'.$get) if ($get);
  164. push(@result,split(/\n/,$get));
  165. }
  166. log_session('Get:'.Dumper(\@result));
  167. return @result;
  168. }
  169. #---------------------------------------------------------------------------------
  170. # Execute command list array without confirmation from device and press any key by device prompt
  171. # Args: t - telnet session, $command - array of command string
  172. #
  173. sub log_cmd4 {
  174. my $t = shift;
  175. my $lines = shift;
  176. if (!$t) { die "Telnet session not exists!"; }
  177. $t->errmode("return");
  178. $t->buffer_empty;
  179. $t->binmode(0);
  180. my @result=();
  181. log_session('Send:'.$lines);
  182. $t->print($lines);
  183. #sleep 250 ms
  184. select(undef, undef, undef, 0.25);
  185. my ($prematch, $match)=$t->waitfor('/\[.*\] >/');
  186. log_debug("Get: $prematch, $match");
  187. push(@result,split(/\n/,$prematch));
  188. log_session('Get:'.Dumper(\@result));
  189. return @result;
  190. }
  191. #---------------------------------------------------------------------------------
  192. sub flush_telnet {
  193. my $t = shift;
  194. return if (!$t);
  195. my @a=();
  196. $t->buffer_empty;
  197. $t->print("\n");
  198. foreach my $str ($t->waitfor($t->prompt)) {
  199. next if (!$str);
  200. my @tmp=split("\n",$str);
  201. foreach my $row (@tmp) {
  202. $row=trim($row);
  203. next if (!$row);
  204. log_session('Flush:'.$row);
  205. push(@a,$row);
  206. }
  207. }
  208. $t->buffer_empty;
  209. return(@a);
  210. }
  211. #---------------------------------------------------------------------------------
  212. sub run_command {
  213. my $t=shift;
  214. my $cmd=shift;
  215. my $cmd_id = shift || 1;
  216. my @tmp=();
  217. if (ref($cmd) eq 'ARRAY') {
  218. @tmp = @{$cmd};
  219. } else {
  220. push(@tmp,$cmd);
  221. }
  222. eval {
  223. foreach my $run_cmd (@tmp) {
  224. next if (!$run_cmd);
  225. log_cmd($t,$run_cmd) if ($cmd_id == 1);
  226. log_cmd2($t,$run_cmd) if ($cmd_id == 2);
  227. log_cmd3($t,$run_cmd) if ($cmd_id == 3);
  228. log_cmd4($t,$run_cmd) if ($cmd_id == 4);
  229. }
  230. };
  231. if ($@) { log_error("Abort: $@"); return 0; };
  232. return 1;
  233. }
  234. #---------------------------------------------------------------------------------
  235. sub netdev_set_auth {
  236. my $device = shift;
  237. #router
  238. if ($device->{device_type} eq '2') {
  239. #mikrotik
  240. if ($device->{vendor_id} eq '9') { $device->{port}=$config_ref{router_port}; }
  241. $device->{login}=$config_ref{router_login};
  242. $device->{password}=$config_ref{router_password};
  243. }
  244. #switch
  245. if ($device->{device_type} eq '1' or $device->{vendor_id} eq '3') {
  246. #mikrotik
  247. # if ($device->{vendor_id} eq '9') { $device->{port}=$config_ref{router_port}; }
  248. $device->{login}=$sw_login;
  249. $device->{password}=$sw_password;
  250. }
  251. return $device;
  252. }
  253. #---------------------------------------------------------------------------------
  254. sub netdev_login {
  255. my $device = shift;
  256. #skip unknown vendor
  257. if (!$switch_auth{$device->{vendor_id}}) { return; }
  258. if (!$switch_auth{$device->{vendor_id}}{proto}) { $switch_auth{$device->{vendor_id}}{proto} = 'telnet'; }
  259. if (!$device->{port} and $switch_auth{$device->{vendor_id}}{port}) { $device->{port} = $switch_auth{$device->{vendor_id}}{port}; }
  260. my $t;
  261. #open my $out, '>', "/tmp/debug-$device->{ip}.txt" or warn $!;
  262. #$Net::OpenSSH::debug_fh = $out;
  263. #$Net::OpenSSH::debug = -1;
  264. if ($switch_auth{$device->{vendor_id}}{proto} eq 'telnet') {
  265. if (!$device->{port}) { $device->{port} = '23'; }
  266. log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by telnet...");
  267. #zyxel patch
  268. if ($device->{vendor_id} eq '4') {
  269. eval {
  270. my $t1 = new Net::Telnet (Timeout => 5, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
  271. $t1->open($device->{ip}) or return;
  272. if (exists $switch_auth{$device->{vendor_id}}{login}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
  273. $t1->print($device->{login});
  274. if (exists $switch_auth{$device->{vendor_id}}{password}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
  275. $t1->print($device->{password});
  276. $t1->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  277. $t1->cmd("exit");
  278. $t1->close;
  279. };
  280. }
  281. eval {
  282. # $t = new Net::Telnet (Timeout => 10, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/", Dump_Log=>'/tmp/1');
  283. $t = new Net::Telnet (Timeout => 30, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
  284. $t->open($device->{ip}) or return;
  285. if (exists $switch_auth{$device->{vendor_id}}{login}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
  286. if ($device->{vendor_id} eq '9') { $t->print($device->{login}.'+ct400w'); } else { $t->print($device->{login}); }
  287. if (exists $switch_auth{$device->{vendor_id}}{password}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
  288. $t->print($device->{password});
  289. $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  290. if (exists $switch_auth{$device->{vendor_id}}{enable}) {
  291. $t->print($switch_auth{$device->{vendor_id}}{enable});
  292. $t->print($device->{enable_password});
  293. $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  294. }
  295. if ($device->{vendor_id} eq '2') {
  296. log_cmd($t,"terminal datadump");
  297. log_cmd($t,"no logging console");
  298. }
  299. if ($device->{vendor_id} eq '5') { log_cmd($t,"terminal page-break disable"); }
  300. if ($device->{vendor_id} eq '6') { log_cmd($t,"terminal length 0"); }
  301. if ($device->{vendor_id} eq '9') { log_cmd4($t,"/system note set show-at-login=no"); }
  302. if ($device->{vendor_id} eq '16') { log_cmd($t,"terminal width 0"); }
  303. if ($device->{vendor_id} eq '17') {
  304. log_cmd($t,"more displine 50");
  305. log_cmd($t,"more off");
  306. }
  307. if ($device->{vendor_id} eq '38') {
  308. log_cmd($t,"disable cli prompting");
  309. log_cmd($t,"disable clipaging");
  310. }
  311. };
  312. 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!"); }
  313. }
  314. if ($switch_auth{$device->{vendor_id}}{proto} eq 'ssh') {
  315. if (!$device->{port}) { $device->{port} = '22'; }
  316. log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by ssh...");
  317. $t = Net::OpenSSH->new($device->{ip},
  318. user=>$device->{login},
  319. password=>$device->{password},
  320. port=>$device->{port},
  321. timeout=>30,
  322. master_opts => [
  323. -o => "StrictHostKeyChecking=no",
  324. -o => "PubkeyAcceptedKeyTypes=+ssh-dss",
  325. -o => "KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1",
  326. -o => "HostKeyAlgorithms=+ssh-dss",
  327. -o => "LogLevel=quiet",
  328. -o => "UserKnownHostsFile=/dev/null"
  329. ]
  330. );
  331. if ($t->error) { log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: ".$t->error); }
  332. netdev_set_enable($t,$device);
  333. if ($device->{vendor_id} eq '2') {
  334. $t->capture("terminal datadump");
  335. $t->capture("no logging console");
  336. }
  337. if ($device->{vendor_id} eq '5') {
  338. $t->capture("terminal page-break disable");
  339. }
  340. if ($device->{vendor_id} eq '6') {
  341. $t->capture("terminal length 0");
  342. }
  343. if ($device->{vendor_id} eq '9') {
  344. $t->capture("/system note set show-at-login=no");
  345. }
  346. if ($device->{vendor_id} eq '16') {
  347. $t->capture("terminal width 0");
  348. }
  349. if ($device->{vendor_id} eq '17') {
  350. $t->capture("more displine 50");
  351. $t->capture("more off");
  352. }
  353. if ($device->{vendor_id} eq '38') {
  354. $t->capture("disable cli prompting");
  355. $t->capture("disable clipaging");
  356. }
  357. log_info("Login to $device->{device_name} ip: $device->{ip} by ssh success!");
  358. }
  359. return $t;
  360. }
  361. #---------------------------------------------------------------------------------
  362. sub netdev_set_enable {
  363. my $session = shift;
  364. my $device = shift;
  365. return if (!exists $switch_auth{$device->{vendor_id}}{enable});
  366. my $cmd = $switch_auth{$device->{vendor_id}}{enable};
  367. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
  368. if ($device->{enable_password}) { netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$device->{enable_password},3); }
  369. }
  370. #---------------------------------------------------------------------------------
  371. sub netdev_cmd {
  372. my ($device,$session,$proto,$cmd,$telnet_version)=@_;
  373. my @result=();
  374. my @tmp=();
  375. if (ref($cmd) eq 'ARRAY') { @tmp = @{$cmd}; } else { @tmp = split(/\n/,$cmd); }
  376. if ($proto eq 'ssh') {
  377. eval {
  378. foreach my $run_cmd (@tmp) {
  379. next if (!$run_cmd);
  380. if ($run_cmd =~ /SLEEP/i) {
  381. if ($run_cmd =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
  382. next;
  383. }
  384. log_session('Send:'.$run_cmd);
  385. my @row = $session->capture($run_cmd);
  386. chomp(@row);
  387. push(@result,@row);
  388. select(undef, undef, undef, 0.25);
  389. }
  390. log_session('Get:'.Dumper(\@result));
  391. };
  392. if ($@) { log_error("Abort: $@"); return 0; };
  393. }
  394. if ($proto eq 'tssh') {
  395. my $t = Net::OpenSSH->new($device->{ip},
  396. user=>$device->{login},
  397. password=>$device->{password},
  398. port=>$device->{port},
  399. timeout=>30,
  400. master_opts => [
  401. -o => "StrictHostKeyChecking=no",
  402. -o => "PubkeyAcceptedKeyTypes=+ssh-dss",
  403. -o => "KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1",
  404. -o => "HostKeyAlgorithms=+ssh-dss",
  405. -o => "LogLevel=quiet",
  406. -o => "UserKnownHostsFile=/dev/null"
  407. ]
  408. );
  409. if ($t->error) { log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: ".$t->error); }
  410. my ($pty, $pid) = $t->open2pty({stderr_to_stdout => 1}) or die "unable to start remote shell: " . $t->error;
  411. my $telnet = Net::Telnet->new(-fhopen => $pty, -prompt => "/$switch_auth{$device->{vendor_id}}{prompt}/", -telnetmode => 0,-cmd_remove_mode => 1,-output_record_separator => "\r");
  412. $telnet->waitfor(-match => $telnet->prompt, -errmode => "return") or die "login failed: " . $telnet->lastline;
  413. if (exists $switch_auth{$device->{vendor_id}}{enable}) {
  414. $telnet->print($switch_auth{$device->{vendor_id}}{enable});
  415. $telnet->print($device->{enable_password});
  416. $telnet->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  417. }
  418. if (!$telnet_version) { $telnet_version = 1; }
  419. eval {
  420. foreach my $run_cmd (@tmp) {
  421. next if (!$run_cmd);
  422. my @ret=();
  423. @ret=log_cmd($telnet,$run_cmd) if ($telnet_version == 1);
  424. @ret=log_cmd2($telnet,$run_cmd) if ($telnet_version == 2);
  425. @ret=log_cmd3($telnet,$run_cmd) if ($telnet_version == 3);
  426. @ret=log_cmd4($telnet,$run_cmd) if ($telnet_version == 4);
  427. if (scalar @ret) { push(@result,@ret); }
  428. select(undef, undef, undef, 0.25);
  429. }
  430. };
  431. $telnet->close;
  432. waitpid($pid, 0);
  433. if ($@) { log_error("Abort: $@"); return 0; };
  434. }
  435. if ($proto eq 'telnet') {
  436. if (!$telnet_version) { $telnet_version = 1; }
  437. eval {
  438. foreach my $run_cmd (@tmp) {
  439. next if (!$run_cmd);
  440. my @ret=();
  441. @ret=log_cmd($session,$run_cmd) if ($telnet_version == 1);
  442. @ret=log_cmd2($session,$run_cmd) if ($telnet_version == 2);
  443. @ret=log_cmd3($session,$run_cmd) if ($telnet_version == 3);
  444. @ret=log_cmd4($session,$run_cmd) if ($telnet_version == 4);
  445. if (scalar @ret) { push(@result,@ret); }
  446. select(undef, undef, undef, 0.25);
  447. }
  448. };
  449. if ($@) { log_error("Abort: $@"); return 0; };
  450. }
  451. return @result;
  452. }
  453. #---------------------------------------------------------------------------------
  454. sub netdev_backup {
  455. my $device = shift;
  456. my $tftp_ip = shift;
  457. #eltex
  458. if ($device->{vendor_id} eq '2') {
  459. eval {
  460. my $session = netdev_login($device);
  461. my $cmd = "upload startup-config tftp $tftp_ip $device->{device_name}.cfg";
  462. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  463. };
  464. }
  465. #huawei
  466. if ($device->{vendor_id} eq '3') {
  467. eval {
  468. my $cmd = "quit\ntftp $tftp_ip put vrpcfg.zip $device->{device_name}.zip\nSLEEP 5\n";
  469. netdev_cmd($device,undef,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
  470. };
  471. }
  472. #zyxel
  473. if ($device->{vendor_id} eq '4') {
  474. eval {
  475. my $session = netdev_login($device);
  476. my $cmd = "copy running-config tftp $tftp_ip $device->{device_name}.cfg";
  477. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  478. };
  479. }
  480. #raisecom
  481. if ($device->{vendor_id} eq '5') {
  482. eval {
  483. my $session = netdev_login($device);
  484. my $cmd = "upload startup-config tftp $tftp_ip $device->{device_name}.cfg";
  485. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  486. };
  487. }
  488. #SNR
  489. if ($device->{vendor_id} eq '6') {
  490. eval {
  491. my $session = netdev_login($device);
  492. my $cmd = "copy running-config tftp://$tftp_ip/$device->{device_name}.cfg
  493. Y
  494. ";
  495. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
  496. };
  497. }
  498. #Dlink
  499. if ($device->{vendor_id} eq '7') {
  500. eval {
  501. my $session = netdev_login($device);
  502. my $cmd = "upload cfg_toTFTP $tftp_ip dest_file $device->{device_name}.cfg src_file config.cfg";
  503. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  504. };
  505. }
  506. #allied telesys x210,x610
  507. if ($device->{device_model_id} ~~ [50..53]) {
  508. eval {
  509. my $session = netdev_login($device);
  510. my $cmd = "copy running-config tftp
  511. SLEEP 2
  512. $tftp_ip
  513. SLEEP 2
  514. $device->{device_name}.cfg
  515. SLEEP 5
  516. ";
  517. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  518. };
  519. }
  520. #allied telesys 8000
  521. if ($device->{device_model_id} eq '3') {
  522. eval {
  523. my $session = netdev_login($device);
  524. my $cmd = "copy running-config tftp://$tftp_ip/$device->{device_name}.cfg";
  525. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  526. };
  527. }
  528. #allied telesys 8100
  529. if ($device->{device_model_id} eq '4') {
  530. eval {
  531. my $session = netdev_login($device);
  532. my $cmd = "copy flash tftp $tftp_ip boot.cfg";
  533. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  534. rename $tftp_dir."/boot.cfg",$tftp_dir."/$device->{device_name}".".cfg";
  535. };
  536. }
  537. #mikrotik
  538. if ($device->{vendor_id} eq '9') {
  539. eval {
  540. my $session = netdev_login($device);
  541. log_cmd($session,"/system note set show-at-login=no",1,$session->prompt);
  542. my $cmd = "/export";
  543. my @netdev_cfg = netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,4);
  544. write_to_file($tftp_dir."/$device->{device_name}.cfg","Config for $device->{device_name}",0);
  545. foreach my $row (@netdev_cfg) { write_to_file($tftp_dir."/$device->{device_name}.cfg",$row,1); }
  546. };
  547. }
  548. #cisco
  549. if ($device->{vendor_id} eq '16') {
  550. eval {
  551. my $session = netdev_login($device);
  552. my $cmd = "
  553. copy system:/running-config tftp:
  554. SLEEP 2
  555. $tftp_ip
  556. SLEEP 2
  557. $device->{device_name}.cfg
  558. SLEEP 5
  559. ";
  560. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  561. };
  562. }
  563. #maipu
  564. if ($device->{vendor_id} eq '17') {
  565. eval {
  566. my $session = netdev_login($device);
  567. my $cmd = "
  568. filesystem
  569. copy running-config tftp $tftp_ip $device->{device_name}.cfg
  570. SLEEP 5
  571. exit
  572. ";
  573. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  574. };
  575. }
  576. #Qtech
  577. if ($device->{vendor_id} eq '38') {
  578. eval {
  579. my $session = netdev_login($device);
  580. my $cmd = "upload configuration tftp $tftp_ip $device->{device_name}.cfg";
  581. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  582. };
  583. }
  584. #Extreme
  585. if ($device->{vendor_id} eq '39') {
  586. eval {
  587. my $session = netdev_login($device);
  588. my $cmd = "upload configuration $tftp_ip $device->{device_name}.cfg vr \"VR-Default\"";
  589. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  590. };
  591. }
  592. }
  593. #---------------------------------------------------------------------------------
  594. sub netdev_set_port_descr {
  595. my $session = shift;
  596. my $device = shift;
  597. my $port = shift;
  598. my $port_num = shift;
  599. my $descr = shift;
  600. my $cmd;
  601. my $telnet_cmd_mode = 4;
  602. #eltex
  603. if ($device->{vendor_id} eq '2') {
  604. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  605. $cmd = "
  606. conf t
  607. interface $port
  608. $descr
  609. exit
  610. exit";
  611. }
  612. #huawei
  613. if ($device->{vendor_id} eq '3') {
  614. if (!$descr) { $descr = "undo description"; } else { $descr = "description $descr"; }
  615. $cmd = "
  616. interface $port
  617. $descr
  618. quit";
  619. }
  620. #zyxel
  621. if ($device->{vendor_id} eq '4') {
  622. $telnet_cmd_mode = 1;
  623. if (!$descr) { $descr = "name "; } else { $descr = "name $descr"; }
  624. $cmd = "
  625. conf t
  626. interface port-channel $port_num
  627. $descr
  628. exit
  629. exit";
  630. }
  631. #raisecom
  632. if ($device->{vendor_id} eq '5') {
  633. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  634. $cmd = "
  635. conf t
  636. interface $port_num
  637. $descr
  638. exit
  639. exit";
  640. }
  641. #SNR
  642. if ($device->{vendor_id} eq '6') {
  643. $telnet_cmd_mode = 1;
  644. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  645. $cmd = "
  646. conf t
  647. interface $port
  648. $descr
  649. exit
  650. exit";
  651. }
  652. #Dlink
  653. if ($device->{vendor_id} eq '7') {
  654. $telnet_cmd_mode = 1;
  655. if (!$descr) { $descr = "clear_description"; } else { $descr = "description $descr"; }
  656. $cmd = "config ports $port_num $descr";
  657. }
  658. #allied telesys x210,x610
  659. if ($device->{vendor_id} eq '8') {
  660. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  661. $telnet_cmd_mode = 2;
  662. $cmd = "
  663. conf t
  664. interface $port
  665. $descr
  666. exit
  667. exit";
  668. }
  669. #allied telesys 8000
  670. if ($device->{device_model_id} eq '3') {
  671. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  672. $telnet_cmd_mode = 2;
  673. $cmd = "
  674. conf
  675. interface ethernet $port
  676. $descr
  677. exit
  678. exit";
  679. }
  680. #allied telesys 8100
  681. if ($device->{device_model_id} eq '4') {
  682. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  683. $telnet_cmd_mode = 2;
  684. $cmd = "
  685. conf t
  686. interface $port
  687. $descr
  688. exit
  689. exit";
  690. }
  691. #mikrotik
  692. if ($device->{vendor_id} eq '9') {
  693. $telnet_cmd_mode = 4;
  694. if (!$descr) { $descr='""'; } else { $descr='"'.$descr.'"'; }
  695. $cmd = "/interface ethernet set [ find default-name=$port ] comment=".$descr;
  696. }
  697. #cisco
  698. if ($device->{vendor_id} eq '16') {
  699. if (!$descr) { $descr = 'description ""'; } else { $descr = "description $descr"; }
  700. $cmd = "
  701. conf t
  702. interface $port
  703. $descr
  704. exit
  705. exit";
  706. }
  707. #maipu
  708. if ($device->{vendor_id} eq '17') {
  709. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  710. $cmd = "
  711. conf t
  712. port $port
  713. $descr
  714. exit
  715. exit";
  716. }
  717. #Qtech
  718. if ($device->{vendor_id} eq '38') {
  719. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  720. $cmd = "
  721. conf t
  722. interface $port
  723. $descr
  724. exit
  725. exit";
  726. }
  727. #Extreme
  728. if ($device->{vendor_id} eq '39') {
  729. if ($descr) {
  730. $cmd = "configure port $port_num display $descr";
  731. } else {
  732. $cmd = "unconfigure port $port_num display";
  733. }
  734. }
  735. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
  736. }
  737. #---------------------------------------------------------------------------------
  738. sub netdev_set_hostname {
  739. my $session = shift;
  740. my $device = shift;
  741. my $cmd;
  742. my $telnet_cmd_mode = 4;
  743. #eltex
  744. if ($device->{vendor_id} eq '2') {
  745. $cmd = "
  746. conf t
  747. hostname $device->{device_name}
  748. exit";
  749. }
  750. #huawei
  751. if ($device->{vendor_id} eq '3') {
  752. $cmd = "sysname $device->{device_name}";
  753. }
  754. #zyxel
  755. if ($device->{vendor_id} eq '4') {
  756. $telnet_cmd_mode = 1;
  757. $cmd = "
  758. conf t
  759. hostname $device->{device_name}
  760. exit";
  761. }
  762. #raisecom
  763. if ($device->{vendor_id} eq '5') {
  764. $cmd = "hostname $device->{device_name}";
  765. }
  766. #SNR
  767. if ($device->{vendor_id} eq '6') {
  768. $telnet_cmd_mode = 1;
  769. $cmd = "
  770. conf t
  771. hostname $device->{device_name}
  772. exit";
  773. }
  774. #Dlink
  775. if ($device->{vendor_id} eq '7') {
  776. $telnet_cmd_mode = 1;
  777. $cmd = "config hostname $device->{device_name}";
  778. }
  779. #allied telesys x210,x610 - default
  780. if ($device->{vendor_id} eq '8') {
  781. $telnet_cmd_mode = 2;
  782. $cmd = "
  783. conf t
  784. hostname $device->{device_name}
  785. exit";
  786. }
  787. #allied telesys 8000
  788. if ($device->{device_model_id} eq '3') {
  789. $telnet_cmd_mode = 2;
  790. $cmd = "
  791. conf
  792. hostname $device->{device_name}
  793. exit";
  794. }
  795. #allied telesys 8100
  796. if ($device->{device_model_id} eq '4') {
  797. $telnet_cmd_mode = 2;
  798. $cmd = "
  799. conf t
  800. hostname $device->{device_name}
  801. exit";
  802. }
  803. #mikrotik
  804. if ($device->{vendor_id} eq '9') {
  805. $telnet_cmd_mode = 4;
  806. $cmd = "/system identity set name=$device->{device_name}";
  807. }
  808. #cisco
  809. if ($device->{vendor_id} eq '16') {
  810. $cmd = "
  811. conf t
  812. hostname $device->{device_name}
  813. exit";
  814. }
  815. #maipu
  816. if ($device->{vendor_id} eq '17') {
  817. $cmd = "
  818. conf t
  819. hostname $device->{device_name}
  820. exit";
  821. }
  822. #Qtech
  823. if ($device->{vendor_id} eq '38') {
  824. $cmd = "
  825. conf t
  826. hostname $device->{device_name}
  827. exit";
  828. }
  829. #Extreme
  830. if ($device->{vendor_id} eq '39') {
  831. $cmd = "configure snmp sysName $device->{device_name}";
  832. }
  833. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
  834. }
  835. #---------------------------------------------------------------------------------
  836. sub netdev_wr_mem {
  837. my $session = shift;
  838. my $device = shift;
  839. my $cmd;
  840. my $telnet_cmd_mode = 4;
  841. #eltex
  842. if ($device->{vendor_id} eq '2') {
  843. $cmd = "wr
  844. Y";
  845. }
  846. #huawei
  847. if ($device->{vendor_id} eq '3') {
  848. $cmd = "quit
  849. save
  850. Y
  851. ";
  852. }
  853. #zyxel
  854. if ($device->{vendor_id} eq '4') { $cmd = "wr mem"; }
  855. #raisecom
  856. if ($device->{vendor_id} eq '5') { $cmd = "wr"; }
  857. #SNR
  858. if ($device->{vendor_id} eq '6') {
  859. $cmd="copy running-config startup-config
  860. Y";
  861. }
  862. #Dlink
  863. if ($device->{vendor_id} eq '7') { $cmd="save"; }
  864. #allied telesys x210,x610
  865. if ($device->{vendor_id} eq '8') {
  866. $cmd = "wr
  867. Y";
  868. }
  869. #allied telesys 8000
  870. if ($device->{device_model_id} eq '3') {
  871. $telnet_cmd_mode=2;
  872. $cmd = "copy running-config startup-config
  873. Y
  874. ";
  875. }
  876. #allied telesys 8100
  877. if ($device->{device_model_id} eq '4') {
  878. $cmd = "wr
  879. Y";
  880. }
  881. #cisco
  882. if ($device->{vendor_id} eq '16') { $cmd="wr_mem"; }
  883. #maipu
  884. if ($device->{vendor_id} eq '17') {
  885. $cmd = "wr
  886. Yes
  887. ";
  888. }
  889. #Qtech
  890. if ($device->{vendor_id} eq '38') {
  891. $cmd = "copy running-config startup-config
  892. Y
  893. ";
  894. }
  895. #Extreme
  896. if ($device->{vendor_id} eq '39') { $cmd="save configuration primary"; }
  897. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
  898. }
  899. #---------------------------------------------------------------------------------
  900. 1;
  901. }