cmd.pm 26 KB

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