cmd.pm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. package Rstat::cmd;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use strict;
  7. use English;
  8. no if $] >= 5.018, warnings => "experimental::smartmatch";
  9. use FindBin '$Bin';
  10. use lib "$Bin";
  11. use base 'Exporter';
  12. use vars qw(@EXPORT @ISA);
  13. use Data::Dumper;
  14. use Rstat::config;
  15. use Rstat::main;
  16. use Net::Telnet;
  17. use Net::OpenSSH;
  18. @ISA = qw(Exporter);
  19. @EXPORT = qw(
  20. log_cmd
  21. log_cmd2
  22. log_cmd3
  23. log_cmd4
  24. flush_telnet
  25. run_command
  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_login {
  236. my $device = shift;
  237. #skip unknown vendor
  238. if (!$switch_auth{$device->{vendor_id}}) { return; }
  239. my $t;
  240. #open my $out, '>', "/tmp/debug-$device->{ip}.txt" or warn $!;
  241. #$Net::OpenSSH::debug_fh = $out;
  242. #$Net::OpenSSH::debug = -1;
  243. if ($device->{proto} eq 'telnet') {
  244. if (!$device->{port}) { $device->{port} = '23'; }
  245. log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by telnet...");
  246. #zyxel patch
  247. if ($device->{vendor_id} eq '4') {
  248. eval {
  249. my $t1 = new Net::Telnet (Timeout => 5, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
  250. $t1->open($device->{ip}) or return;
  251. if (exists $switch_auth{$device->{vendor_id}}{login}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
  252. $t1->print($device->{login});
  253. if (exists $switch_auth{$device->{vendor_id}}{password}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
  254. $t1->print($device->{password});
  255. $t1->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  256. $t1->cmd("exit");
  257. $t1->close;
  258. };
  259. }
  260. eval {
  261. # $t = new Net::Telnet (Timeout => 10, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/", Dump_Log=>'/tmp/1');
  262. $t = new Net::Telnet (Timeout => 30, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
  263. $t->open($device->{ip}) or return;
  264. if (exists $switch_auth{$device->{vendor_id}}{login}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
  265. if ($device->{vendor_id} eq '9') { $t->print($device->{login}.'+ct400w'); } else { $t->print($device->{login}); }
  266. if (exists $switch_auth{$device->{vendor_id}}{password}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
  267. $t->print($device->{password});
  268. $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  269. if (exists $switch_auth{$device->{vendor_id}}{enable}) {
  270. $t->print($switch_auth{$device->{vendor_id}}{enable});
  271. $t->print($device->{enable_password}) if ($device->{enable_password});
  272. $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  273. }
  274. if ($device->{vendor_id} eq '2') {
  275. log_cmd($t,"terminal datadump");
  276. log_cmd($t,"no logging console");
  277. }
  278. if ($device->{vendor_id} eq '5') { log_cmd($t,"terminal page-break disable"); }
  279. if ($device->{vendor_id} eq '6') { log_cmd($t,"terminal length 0"); }
  280. if ($device->{vendor_id} eq '9') { log_cmd4($t,"/system note set show-at-login=no"); }
  281. if ($device->{vendor_id} eq '16') { log_cmd($t,"terminal width 0"); }
  282. if ($device->{vendor_id} eq '17') {
  283. log_cmd($t,"more displine 50");
  284. log_cmd($t,"more off");
  285. }
  286. if ($device->{vendor_id} eq '38') {
  287. log_cmd($t,"disable cli prompting");
  288. log_cmd($t,"disable clipaging");
  289. }
  290. };
  291. 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!"); }
  292. }
  293. if ($device->{proto} eq 'ssh') {
  294. if (!$device->{port}) { $device->{port} = '22'; }
  295. log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by ssh...");
  296. $t = Net::OpenSSH->new($device->{ip},
  297. user=>$device->{login},
  298. password=>$device->{password},
  299. port=>$device->{port},
  300. timeout=>30,
  301. master_opts => [
  302. -o => "StrictHostKeyChecking=no",
  303. -o => "PubkeyAcceptedKeyTypes=+ssh-dss",
  304. -o => "KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1",
  305. -o => "HostKeyAlgorithms=+ssh-dss",
  306. -o => "LogLevel=quiet",
  307. -o => "UserKnownHostsFile=/dev/null"
  308. ]
  309. );
  310. if ($t->error) { log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: ".$t->error); }
  311. netdev_set_enable($t,$device);
  312. if ($device->{vendor_id} eq '2') {
  313. $t->capture("terminal datadump");
  314. $t->capture("no logging console");
  315. }
  316. if ($device->{vendor_id} eq '5') {
  317. $t->capture("terminal page-break disable");
  318. }
  319. if ($device->{vendor_id} eq '6') {
  320. $t->capture("terminal length 0");
  321. }
  322. if ($device->{vendor_id} eq '9') {
  323. $t->capture("/system note set show-at-login=no");
  324. }
  325. if ($device->{vendor_id} eq '16') {
  326. $t->capture("terminal width 0");
  327. }
  328. if ($device->{vendor_id} eq '17') {
  329. $t->capture("more displine 50");
  330. $t->capture("more off");
  331. }
  332. if ($device->{vendor_id} eq '38') {
  333. $t->capture("disable cli prompting");
  334. $t->capture("disable clipaging");
  335. }
  336. log_info("Login to $device->{device_name} ip: $device->{ip} by ssh success!");
  337. }
  338. return $t;
  339. }
  340. #---------------------------------------------------------------------------------
  341. sub netdev_set_enable {
  342. my $session = shift;
  343. my $device = shift;
  344. return if (!exists $switch_auth{$device->{vendor_id}}{enable});
  345. my $cmd = $switch_auth{$device->{vendor_id}}{enable};
  346. netdev_cmd($device,$session,$device->{proto},$cmd,3);
  347. if ($device->{enable_password}) { netdev_cmd($device,$session,$device->{proto},$device->{enable_password},3); }
  348. }
  349. #---------------------------------------------------------------------------------
  350. sub netdev_cmd {
  351. my ($device,$session,$proto,$cmd,$telnet_version)=@_;
  352. my @result=();
  353. my @tmp=();
  354. if (ref($cmd) eq 'ARRAY') { @tmp = @{$cmd}; } else { @tmp = split(/\n/,$cmd); }
  355. if ($proto eq 'ssh') {
  356. eval {
  357. foreach my $run_cmd (@tmp) {
  358. next if (!$run_cmd);
  359. if ($run_cmd =~ /SLEEP/i) {
  360. if ($run_cmd =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
  361. next;
  362. }
  363. log_session('Send:'.$run_cmd);
  364. my @row = $session->capture($run_cmd);
  365. chomp(@row);
  366. push(@result,@row);
  367. select(undef, undef, undef, 0.25);
  368. }
  369. log_session('Get:'.Dumper(\@result));
  370. };
  371. if ($@) { log_error("Abort: $@"); return 0; };
  372. }
  373. if ($proto eq 'tssh') {
  374. my $t = Net::OpenSSH->new($device->{ip},
  375. user=>$device->{login},
  376. password=>$device->{password},
  377. port=>$device->{port},
  378. timeout=>30,
  379. master_opts => [
  380. -o => "StrictHostKeyChecking=no",
  381. -o => "PubkeyAcceptedKeyTypes=+ssh-dss",
  382. -o => "KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1",
  383. -o => "HostKeyAlgorithms=+ssh-dss",
  384. -o => "LogLevel=quiet",
  385. -o => "UserKnownHostsFile=/dev/null"
  386. ]
  387. );
  388. if ($t->error) { log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: ".$t->error); }
  389. my ($pty, $pid) = $t->open2pty({stderr_to_stdout => 1}) or die "unable to start remote shell: " . $t->error;
  390. my $telnet = Net::Telnet->new(-fhopen => $pty, -prompt => "/$switch_auth{$device->{vendor_id}}{prompt}/", -telnetmode => 0,-cmd_remove_mode => 1,-output_record_separator => "\r");
  391. $telnet->waitfor(-match => $telnet->prompt, -errmode => "return") or die "login failed: " . $telnet->lastline;
  392. if (exists $switch_auth{$device->{vendor_id}}{enable}) {
  393. $telnet->print($switch_auth{$device->{vendor_id}}{enable});
  394. $telnet->print($device->{enable_password});
  395. $telnet->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  396. }
  397. if (!$telnet_version) { $telnet_version = 1; }
  398. eval {
  399. foreach my $run_cmd (@tmp) {
  400. next if (!$run_cmd);
  401. my @ret=();
  402. @ret=log_cmd($telnet,$run_cmd) if ($telnet_version == 1);
  403. @ret=log_cmd2($telnet,$run_cmd) if ($telnet_version == 2);
  404. @ret=log_cmd3($telnet,$run_cmd) if ($telnet_version == 3);
  405. @ret=log_cmd4($telnet,$run_cmd) if ($telnet_version == 4);
  406. if (scalar @ret) { push(@result,@ret); }
  407. select(undef, undef, undef, 0.25);
  408. }
  409. };
  410. $telnet->close;
  411. waitpid($pid, 0);
  412. if ($@) { log_error("Abort: $@"); return 0; };
  413. }
  414. if ($proto eq 'telnet') {
  415. if (!$telnet_version) { $telnet_version = 1; }
  416. eval {
  417. foreach my $run_cmd (@tmp) {
  418. next if (!$run_cmd);
  419. my @ret=();
  420. @ret=log_cmd($session,$run_cmd) if ($telnet_version == 1);
  421. @ret=log_cmd2($session,$run_cmd) if ($telnet_version == 2);
  422. @ret=log_cmd3($session,$run_cmd) if ($telnet_version == 3);
  423. @ret=log_cmd4($session,$run_cmd) if ($telnet_version == 4);
  424. if (scalar @ret) { push(@result,@ret); }
  425. select(undef, undef, undef, 0.25);
  426. }
  427. };
  428. if ($@) { log_error("Abort: $@"); return 0; };
  429. }
  430. return @result;
  431. }
  432. #---------------------------------------------------------------------------------
  433. sub netdev_backup {
  434. my $device = shift;
  435. my $tftp_ip = shift;
  436. #eltex
  437. if ($device->{vendor_id} eq '2') {
  438. eval {
  439. my $session = netdev_login($device);
  440. my $cmd = "upload startup-config tftp $tftp_ip $device->{device_name}.cfg";
  441. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  442. };
  443. }
  444. #huawei
  445. if ($device->{vendor_id} eq '3') {
  446. eval {
  447. my $cmd = "quit\ntftp $tftp_ip put vrpcfg.zip $device->{device_name}.zip\nSLEEP 5\n";
  448. netdev_cmd($device,undef,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
  449. };
  450. }
  451. #zyxel
  452. if ($device->{vendor_id} eq '4') {
  453. eval {
  454. my $session = netdev_login($device);
  455. my $cmd = "copy running-config tftp $tftp_ip $device->{device_name}.cfg";
  456. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  457. };
  458. }
  459. #raisecom
  460. if ($device->{vendor_id} eq '5') {
  461. eval {
  462. my $session = netdev_login($device);
  463. my $cmd = "upload startup-config tftp $tftp_ip $device->{device_name}.cfg";
  464. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  465. };
  466. }
  467. #SNR
  468. if ($device->{vendor_id} eq '6') {
  469. eval {
  470. my $session = netdev_login($device);
  471. my $cmd = "copy running-config tftp://$tftp_ip/$device->{device_name}.cfg
  472. Y
  473. ";
  474. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,3);
  475. };
  476. }
  477. #Dlink
  478. if ($device->{vendor_id} eq '7') {
  479. eval {
  480. my $session = netdev_login($device);
  481. my $cmd = "upload cfg_toTFTP $tftp_ip dest_file $device->{device_name}.cfg src_file config.cfg";
  482. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  483. };
  484. }
  485. #allied telesys x210,x610
  486. if ($device->{device_model_id} ~~ [50..53]) {
  487. eval {
  488. my $session = netdev_login($device);
  489. my $cmd = "copy running-config tftp
  490. SLEEP 2
  491. $tftp_ip
  492. SLEEP 2
  493. $device->{device_name}.cfg
  494. SLEEP 5
  495. ";
  496. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  497. };
  498. }
  499. #allied telesys 8000
  500. if ($device->{device_model_id} eq '3') {
  501. eval {
  502. my $session = netdev_login($device);
  503. my $cmd = "copy running-config tftp://$tftp_ip/$device->{device_name}.cfg";
  504. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  505. };
  506. }
  507. #allied telesys 8100
  508. if ($device->{device_model_id} eq '4') {
  509. eval {
  510. my $session = netdev_login($device);
  511. my $cmd = "copy flash tftp $tftp_ip boot.cfg";
  512. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  513. rename $tftp_dir."/boot.cfg",$tftp_dir."/$device->{device_name}".".cfg";
  514. };
  515. }
  516. #mikrotik
  517. if ($device->{vendor_id} eq '9') {
  518. eval {
  519. my $session = netdev_login($device);
  520. log_cmd($session,"/system note set show-at-login=no",1,$session->prompt);
  521. my $cmd = "/export";
  522. my @netdev_cfg = netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,4);
  523. write_to_file($tftp_dir."/$device->{device_name}.cfg","Config for $device->{device_name}",0);
  524. foreach my $row (@netdev_cfg) { write_to_file($tftp_dir."/$device->{device_name}.cfg",$row,1); }
  525. };
  526. }
  527. #cisco
  528. if ($device->{vendor_id} eq '16') {
  529. eval {
  530. my $session = netdev_login($device);
  531. my $cmd = "
  532. copy system:/running-config tftp:
  533. SLEEP 2
  534. $tftp_ip
  535. SLEEP 2
  536. $device->{device_name}.cfg
  537. SLEEP 5
  538. ";
  539. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,2);
  540. };
  541. }
  542. #maipu
  543. if ($device->{vendor_id} eq '17') {
  544. eval {
  545. my $session = netdev_login($device);
  546. my $cmd = "
  547. filesystem
  548. copy running-config tftp $tftp_ip $device->{device_name}.cfg
  549. SLEEP 5
  550. exit
  551. ";
  552. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  553. };
  554. }
  555. #Qtech
  556. if ($device->{vendor_id} eq '38') {
  557. eval {
  558. my $session = netdev_login($device);
  559. my $cmd = "upload configuration tftp $tftp_ip $device->{device_name}.cfg";
  560. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  561. };
  562. }
  563. #Extreme
  564. if ($device->{vendor_id} eq '39') {
  565. eval {
  566. my $session = netdev_login($device);
  567. my $cmd = "upload configuration $tftp_ip $device->{device_name}.cfg vr \"VR-Default\"";
  568. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,1);
  569. };
  570. }
  571. }
  572. #---------------------------------------------------------------------------------
  573. sub netdev_set_port_descr {
  574. my $session = shift;
  575. my $device = shift;
  576. my $port = shift;
  577. my $port_num = shift;
  578. my $descr = shift;
  579. my $cmd;
  580. my $telnet_cmd_mode = 4;
  581. #eltex
  582. if ($device->{vendor_id} eq '2') {
  583. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  584. $cmd = "
  585. conf t
  586. interface $port
  587. $descr
  588. exit
  589. exit";
  590. }
  591. #huawei
  592. if ($device->{vendor_id} eq '3') {
  593. if (!$descr) { $descr = "undo description"; } else { $descr = "description $descr"; }
  594. $cmd = "
  595. interface $port
  596. $descr
  597. quit";
  598. }
  599. #zyxel
  600. if ($device->{vendor_id} eq '4') {
  601. $telnet_cmd_mode = 1;
  602. if (!$descr) { $descr = "name "; } else { $descr = "name $descr"; }
  603. $cmd = "
  604. conf t
  605. interface port-channel $port_num
  606. $descr
  607. exit
  608. exit";
  609. }
  610. #raisecom
  611. if ($device->{vendor_id} eq '5') {
  612. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  613. $cmd = "
  614. conf t
  615. interface $port_num
  616. $descr
  617. exit
  618. exit";
  619. }
  620. #SNR
  621. if ($device->{vendor_id} eq '6') {
  622. $telnet_cmd_mode = 1;
  623. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  624. $cmd = "
  625. conf t
  626. interface $port
  627. $descr
  628. exit
  629. exit";
  630. }
  631. #Dlink
  632. if ($device->{vendor_id} eq '7') {
  633. $telnet_cmd_mode = 1;
  634. if (!$descr) { $descr = "clear_description"; } else { $descr = "description $descr"; }
  635. $cmd = "config ports $port_num $descr";
  636. }
  637. #allied telesys x210,x610
  638. if ($device->{vendor_id} eq '8') {
  639. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  640. $telnet_cmd_mode = 2;
  641. $cmd = "
  642. conf t
  643. interface $port
  644. $descr
  645. exit
  646. exit";
  647. }
  648. #allied telesys 8000
  649. if ($device->{device_model_id} eq '3') {
  650. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  651. $telnet_cmd_mode = 2;
  652. $cmd = "
  653. conf
  654. interface ethernet $port
  655. $descr
  656. exit
  657. exit";
  658. }
  659. #allied telesys 8100
  660. if ($device->{device_model_id} eq '4') {
  661. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  662. $telnet_cmd_mode = 2;
  663. $cmd = "
  664. conf t
  665. interface $port
  666. $descr
  667. exit
  668. exit";
  669. }
  670. #mikrotik
  671. if ($device->{vendor_id} eq '9') {
  672. $telnet_cmd_mode = 4;
  673. if (!$descr) { $descr='""'; } else { $descr='"'.$descr.'"'; }
  674. $cmd = "/interface ethernet set [ find default-name=$port ] comment=".$descr;
  675. }
  676. #cisco
  677. if ($device->{vendor_id} eq '16') {
  678. if (!$descr) { $descr = 'description ""'; } else { $descr = "description $descr"; }
  679. $cmd = "
  680. conf t
  681. interface $port
  682. $descr
  683. exit
  684. exit";
  685. }
  686. #maipu
  687. if ($device->{vendor_id} eq '17') {
  688. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  689. $cmd = "
  690. conf t
  691. port $port
  692. $descr
  693. exit
  694. exit";
  695. }
  696. #Qtech
  697. if ($device->{vendor_id} eq '38') {
  698. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  699. $cmd = "
  700. conf t
  701. interface $port
  702. $descr
  703. exit
  704. exit";
  705. }
  706. #Extreme
  707. if ($device->{vendor_id} eq '39') {
  708. if ($descr) {
  709. $cmd = "configure port $port_num display $descr";
  710. } else {
  711. $cmd = "unconfigure port $port_num display";
  712. }
  713. }
  714. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
  715. }
  716. #---------------------------------------------------------------------------------
  717. sub netdev_set_hostname {
  718. my $session = shift;
  719. my $device = shift;
  720. my $cmd;
  721. my $telnet_cmd_mode = 4;
  722. #eltex
  723. if ($device->{vendor_id} eq '2') {
  724. $cmd = "
  725. conf t
  726. hostname $device->{device_name}
  727. exit";
  728. }
  729. #huawei
  730. if ($device->{vendor_id} eq '3') {
  731. $cmd = "sysname $device->{device_name}";
  732. }
  733. #zyxel
  734. if ($device->{vendor_id} eq '4') {
  735. $telnet_cmd_mode = 1;
  736. $cmd = "
  737. conf t
  738. hostname $device->{device_name}
  739. exit";
  740. }
  741. #raisecom
  742. if ($device->{vendor_id} eq '5') {
  743. $cmd = "hostname $device->{device_name}";
  744. }
  745. #SNR
  746. if ($device->{vendor_id} eq '6') {
  747. $telnet_cmd_mode = 1;
  748. $cmd = "
  749. conf t
  750. hostname $device->{device_name}
  751. exit";
  752. }
  753. #Dlink
  754. if ($device->{vendor_id} eq '7') {
  755. $telnet_cmd_mode = 1;
  756. $cmd = "config hostname $device->{device_name}";
  757. }
  758. #allied telesys x210,x610 - default
  759. if ($device->{vendor_id} eq '8') {
  760. $telnet_cmd_mode = 2;
  761. $cmd = "
  762. conf t
  763. hostname $device->{device_name}
  764. exit";
  765. }
  766. #allied telesys 8000
  767. if ($device->{device_model_id} eq '3') {
  768. $telnet_cmd_mode = 2;
  769. $cmd = "
  770. conf
  771. hostname $device->{device_name}
  772. exit";
  773. }
  774. #allied telesys 8100
  775. if ($device->{device_model_id} eq '4') {
  776. $telnet_cmd_mode = 2;
  777. $cmd = "
  778. conf t
  779. hostname $device->{device_name}
  780. exit";
  781. }
  782. #mikrotik
  783. if ($device->{vendor_id} eq '9') {
  784. $telnet_cmd_mode = 4;
  785. $cmd = "/system identity set name=$device->{device_name}";
  786. }
  787. #cisco
  788. if ($device->{vendor_id} eq '16') {
  789. $cmd = "
  790. conf t
  791. hostname $device->{device_name}
  792. exit";
  793. }
  794. #maipu
  795. if ($device->{vendor_id} eq '17') {
  796. $cmd = "
  797. conf t
  798. hostname $device->{device_name}
  799. exit";
  800. }
  801. #Qtech
  802. if ($device->{vendor_id} eq '38') {
  803. $cmd = "
  804. conf t
  805. hostname $device->{device_name}
  806. exit";
  807. }
  808. #Extreme
  809. if ($device->{vendor_id} eq '39') {
  810. $cmd = "configure snmp sysName $device->{device_name}";
  811. }
  812. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
  813. }
  814. #---------------------------------------------------------------------------------
  815. sub netdev_wr_mem {
  816. my $session = shift;
  817. my $device = shift;
  818. my $cmd;
  819. my $telnet_cmd_mode = 4;
  820. #eltex
  821. if ($device->{vendor_id} eq '2') {
  822. $cmd = "wr
  823. Y";
  824. }
  825. #huawei
  826. if ($device->{vendor_id} eq '3') {
  827. $cmd = "quit
  828. save
  829. Y
  830. ";
  831. }
  832. #zyxel
  833. if ($device->{vendor_id} eq '4') { $cmd = "wr mem"; }
  834. #raisecom
  835. if ($device->{vendor_id} eq '5') { $cmd = "wr"; }
  836. #SNR
  837. if ($device->{vendor_id} eq '6') {
  838. $cmd="copy running-config startup-config
  839. Y";
  840. }
  841. #Dlink
  842. if ($device->{vendor_id} eq '7') { $cmd="save"; }
  843. #allied telesys x210,x610
  844. if ($device->{vendor_id} eq '8') {
  845. $cmd = "wr
  846. Y";
  847. }
  848. #allied telesys 8000
  849. if ($device->{device_model_id} eq '3') {
  850. $telnet_cmd_mode=2;
  851. $cmd = "copy running-config startup-config
  852. Y
  853. ";
  854. }
  855. #allied telesys 8100
  856. if ($device->{device_model_id} eq '4') {
  857. $cmd = "wr
  858. Y";
  859. }
  860. #cisco
  861. if ($device->{vendor_id} eq '16') { $cmd="wr_mem"; }
  862. #maipu
  863. if ($device->{vendor_id} eq '17') {
  864. $cmd = "wr
  865. Yes
  866. ";
  867. }
  868. #Qtech
  869. if ($device->{vendor_id} eq '38') {
  870. $cmd = "copy running-config startup-config
  871. Y
  872. ";
  873. }
  874. #Extreme
  875. if ($device->{vendor_id} eq '39') { $cmd="save configuration primary"; }
  876. netdev_cmd($device,$session,$switch_auth{$device->{vendor_id}}{proto},$cmd,$telnet_cmd_mode);
  877. }
  878. #---------------------------------------------------------------------------------
  879. 1;
  880. }