cmd.pm 26 KB

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