cmd.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. package eyelib::cmd;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use strict;
  8. use English;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use base 'Exporter';
  12. use vars qw(@EXPORT @ISA);
  13. use Data::Dumper;
  14. use eyelib::config;
  15. use eyelib::main;
  16. use Net::Telnet;
  17. use Net::OpenSSH;
  18. use Expect;
  19. @ISA = qw(Exporter);
  20. @EXPORT = qw(
  21. log_cmd
  22. log_cmd2
  23. log_cmd3
  24. log_cmd4
  25. flush_telnet
  26. run_command
  27. netdev_login
  28. netdev_cmd
  29. netdev_backup
  30. netdev_set_port_descr
  31. netdev_set_hostname
  32. netdev_set_enable
  33. netdev_wr_mem
  34. );
  35. BEGIN
  36. {
  37. #---------------------------------------------------------------------------------
  38. # Execute command and wait answer from device
  39. # Args: t - telnet session, $command - command string, $sleep - pause after execute command (enabled by default)
  40. #
  41. sub log_cmd {
  42. my $t = shift;
  43. my $command = shift;
  44. my $sleep = shift || 1;
  45. if (!$t) { die "Telnet session not exists!"; }
  46. $t->binmode(0);
  47. $t->errmode('return');
  48. log_session('Send:'.$command);
  49. my @ret=$t->cmd(String => $command);
  50. my @a=();
  51. foreach my $row (@ret) {
  52. next if (!$row);
  53. #zyxel patch
  54. $row=~ s/\x1b\x37//g;
  55. #mikrotik patch
  56. $row=~ s/\x0d\x0d\x0d\x1b\x5b\x39\x39\x39\x39\x42//g;
  57. #new line
  58. $row=~ s/\n//g;
  59. $row=trim($row);
  60. if ($row) {
  61. my @tmp=split("\n",$row);
  62. foreach my $line (@tmp) {
  63. next if (!$line);
  64. $line=trim($line);
  65. next if (!$line);
  66. push(@a,$line);
  67. log_session('Get:'.$line);
  68. }
  69. }
  70. }
  71. select(undef, undef, undef, 0.15) if ($sleep);
  72. if (scalar(@a)) { return @a; }
  73. $t->cmd(String => "\n");
  74. my @tmp=flush_telnet($t);
  75. foreach my $line (@tmp) {
  76. next if (!$line);
  77. push(@a,$line);
  78. }
  79. return @a;
  80. }
  81. #---------------------------------------------------------------------------------
  82. # Execute command list array without confirmation from device
  83. # Args: t - telnet session, $command - array of command string, $sleep - pause after execute command (enabled by default)
  84. #
  85. sub log_cmd2 {
  86. my $t = shift;
  87. my $command = shift;
  88. my $sleep = shift || 1;
  89. if (!$t) { die "Telnet session not exists!"; }
  90. $t->binmode(0);
  91. $t->errmode("return");
  92. $t->cmd(String => "\n");
  93. $t->buffer_empty;
  94. my @a;
  95. foreach my $out (split("\n",$command)){
  96. if ($out =~ /SLEEP/) {
  97. if ($out =~ /SLEEP\s+(\d+)/) { sleep($1); } else { sleep(5); };
  98. next;
  99. }
  100. chomp($out);
  101. log_session('Send:'.$out);
  102. $t->print($out);
  103. #sleep 250 ms
  104. select(undef, undef, undef, 0.25) if ($sleep);
  105. foreach my $str ($t->waitfor($t->prompt)) {
  106. $str=trim($str);
  107. if ($str) {
  108. my @tmp=split("\n",$str);
  109. foreach my $line (@tmp) {
  110. next if (!$line);
  111. $line=trim($line);
  112. next if (!$line);
  113. push(@a,$line);
  114. log_session('Get:'.$line);
  115. }
  116. }
  117. }
  118. }
  119. chomp(@a);
  120. return @a;
  121. }
  122. #---------------------------------------------------------------------------------
  123. # Execute command list array without confirmation from device and press any key by device prompt
  124. # Args: t - telnet session, $command - array of command string, $sleep - pause after execute command (enabled by default)
  125. #
  126. sub log_cmd3 {
  127. my $t = shift;
  128. my $lines = shift;
  129. my $sleep = shift || 1;
  130. if (!$t) { die "Telnet session not exists!"; }
  131. $t->errmode("return");
  132. $t->buffer_empty;
  133. $t->binmode(0);
  134. my @result=();
  135. foreach my $out (split("\n",$lines)) {
  136. if ($out =~ /SLEEP/i) {
  137. if ($out =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
  138. next;
  139. }
  140. chomp($out);
  141. log_session('Send:'.$out);
  142. $t->print($out);
  143. #sleep 250 ms
  144. select(undef, undef, undef, 0.25) if ($sleep);
  145. my $end = 0;
  146. my $get;
  147. while ($end == 0) {
  148. foreach my $str ($t->waitfor('/[(#)(\>)(\:)(press)(sure)(Please input)(next page)(continue)(quit)(-- more --)(Confirm)(ESC)(^C$)]/')) {
  149. $t->print("\n") if $str =~ /ENTER/i;
  150. $t->print(" ") if $str =~ /ESC/i;
  151. $t->print(" ") if $str =~ /^C$/i;
  152. $t->print(" ") if $str =~ /(-- more --)/i;
  153. $t->print(" ") if $str =~ /SPACE/i;
  154. $t->print("y\n") if $str =~ /sure/i;
  155. $t->print("\n") if $str =~ /continue/i;
  156. $t->print("y\n") if $str =~ /Please input/i;
  157. $t->print("Y\n") if $str =~ /Confirm/i;
  158. $t->print("Y\n") if $str =~ /\:/;
  159. #last line!!!
  160. $end = 1 if $str =~ /\>/;
  161. $get .= $str;
  162. }
  163. }
  164. log_debug('Get:'.$get) if ($get);
  165. push(@result,split(/\n/,$get));
  166. }
  167. log_session('Get:'.Dumper(\@result));
  168. return @result;
  169. }
  170. #---------------------------------------------------------------------------------
  171. # Execute command list array without confirmation from device and press any key by device prompt
  172. # Args: t - telnet session, $command - array of command string
  173. #
  174. sub log_cmd4 {
  175. my $t = shift;
  176. my $lines = shift;
  177. if (!$t) { die "Telnet session not exists!"; }
  178. $t->errmode("return");
  179. $t->buffer_empty;
  180. $t->binmode(0);
  181. my @result=();
  182. log_session('Send:'.$lines);
  183. $t->print($lines);
  184. #sleep 250 ms
  185. select(undef, undef, undef, 0.25);
  186. my ($prematch, $match)=$t->waitfor('/\[.*\] >/');
  187. log_debug("Get: $prematch, $match");
  188. push(@result,split(/\n/,$prematch));
  189. log_session('Get:'.Dumper(\@result));
  190. return @result;
  191. }
  192. #---------------------------------------------------------------------------------
  193. sub flush_telnet {
  194. my $t = shift;
  195. return if (!$t);
  196. my @a=();
  197. $t->buffer_empty;
  198. $t->print("\n");
  199. foreach my $str ($t->waitfor($t->prompt)) {
  200. next if (!$str);
  201. my @tmp=split("\n",$str);
  202. foreach my $row (@tmp) {
  203. $row=trim($row);
  204. next if (!$row);
  205. log_session('Flush:'.$row);
  206. push(@a,$row);
  207. }
  208. }
  209. $t->buffer_empty;
  210. return(@a);
  211. }
  212. #---------------------------------------------------------------------------------
  213. sub run_command {
  214. my $t=shift;
  215. my $cmd=shift;
  216. my $cmd_id = shift || 1;
  217. my @tmp=();
  218. if (ref($cmd) eq 'ARRAY') {
  219. @tmp = @{$cmd};
  220. } else {
  221. push(@tmp,$cmd);
  222. }
  223. eval {
  224. foreach my $run_cmd (@tmp) {
  225. next if (!$run_cmd);
  226. log_cmd($t,$run_cmd) if ($cmd_id == 1);
  227. log_cmd2($t,$run_cmd) if ($cmd_id == 2);
  228. log_cmd3($t,$run_cmd) if ($cmd_id == 3);
  229. log_cmd4($t,$run_cmd) if ($cmd_id == 4);
  230. }
  231. };
  232. if ($@) { log_error("Abort: $@"); return 0; };
  233. return 1;
  234. }
  235. #---------------------------------------------------------------------------------
  236. sub netdev_login {
  237. my $device = shift;
  238. #skip unknown vendor
  239. if (!$switch_auth{$device->{vendor_id}}) { return 0; }
  240. my $t;
  241. #open my $out, '>', "/tmp/debug-$device->{ip}.txt" or warn $!;
  242. #$Net::OpenSSH::debug_fh = $out;
  243. #$Net::OpenSSH::debug = -1;
  244. if ($device->{proto} eq 'telnet') {
  245. if (!$device->{port}) { $device->{port} = '23'; }
  246. log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by telnet...");
  247. #zyxel patch
  248. if ($device->{vendor_id} eq '4') {
  249. eval {
  250. my $t1 = new Net::Telnet (Timeout => 5, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
  251. $t1->open($device->{ip}) or return 0;
  252. if (exists $switch_auth{$device->{vendor_id}}{login}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
  253. $t1->print($device->{login});
  254. if (exists $switch_auth{$device->{vendor_id}}{password}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
  255. $t1->print($device->{password});
  256. $t1->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  257. $t1->cmd("exit");
  258. $t1->close;
  259. };
  260. }
  261. eval {
  262. # $t = new Net::Telnet (Timeout => 10, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/", Dump_Log=>'/tmp/1');
  263. $t = new Net::Telnet (Timeout => 30, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
  264. $t->open($device->{ip}) or return;
  265. if (exists $switch_auth{$device->{vendor_id}}{login}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
  266. if ($device->{vendor_id} eq '9') { $t->print($device->{login}.'+ct400w'); } else { $t->print($device->{login}); }
  267. if (exists $switch_auth{$device->{vendor_id}}{password}) { $t->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
  268. $t->print($device->{password});
  269. $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/");
  270. };
  271. if ($@) { log_error("Login to $device->{device_name} ip: $device->{ip} by telnet aborted: $@"); return 0; } else { log_info("Login to $device->{device_name} ip: $device->{ip} by telnet success!"); }
  272. }
  273. if ($device->{proto} eq 'ssh') {
  274. if (!$device->{port}) { $device->{port} = '22'; }
  275. log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by OpenSSH...");
  276. $t = Net::OpenSSH->new($device->{ip},
  277. user=>$device->{login},
  278. password=>$device->{password},
  279. port=>$device->{port},
  280. timeout=>30,
  281. strict_mode=>0,
  282. master_opts => [
  283. -o => "StrictHostKeyChecking=no",
  284. -o => "PubkeyAcceptedKeyTypes=+ssh-dss",
  285. -o => "KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1",
  286. -o => "HostKeyAlgorithms=+ssh-dss",
  287. -o => "LogLevel=quiet",
  288. -o => "UserKnownHostsFile=/dev/null"
  289. ]
  290. );
  291. if ($t->error) { log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: ".$t->error); return 0; }
  292. }
  293. if ($device->{proto} eq 'essh') {
  294. if (!$device->{port}) { $device->{port} = '22'; }
  295. log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by ssh::expect...");
  296. $t = Expect->spawn("ssh -o StrictHostKeyChecking=no -o PubkeyAcceptedKeyTypes=+ssh-dss -o KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1 -o HostKeyAlgorithms=+ssh-dss -o LogLevel=quiet -o UserKnownHostsFile=/dev/null $device->{login}\@$device->{ip}");
  297. $t->log_stdout(0); # Disable logging to stdout
  298. $t->expect(30,
  299. [ qr/(?i)password:/ => sub {
  300. my $exp = shift;
  301. $exp->send("$device->{password}\n");
  302. exp_continue;
  303. }],
  304. [ qr/(?i)yes\/no/ => sub {
  305. my $exp = shift;
  306. $exp->send("yes\n");
  307. exp_continue;
  308. }]
  309. );
  310. }
  311. if ($t) {
  312. log_info("Login to $device->{device_name} ip: $device->{ip} by ssh success!");
  313. } else {
  314. log_error("Login to $device->{device_name} ip: $device->{ip} by ssh failed!");
  315. return 0;
  316. }
  317. netdev_set_enable($t,$device);
  318. my @init_cmd=();
  319. if ($device->{vendor_id} eq '2') {
  320. push(@init_cmd,"terminal datadump");
  321. push(@init_cmd,"no logging console");
  322. }
  323. if ($device->{vendor_id} eq '5') {
  324. push(@init_cmd,"terminal page-break disable");
  325. }
  326. if ($device->{vendor_id} eq '6') {
  327. push(@init_cmd,"terminal length 0");
  328. }
  329. if ($device->{vendor_id} eq '9') {
  330. push(@init_cmd,"/system note set show-at-login=no");
  331. }
  332. if ($device->{vendor_id} eq '16') {
  333. push(@init_cmd,"terminal width 0");
  334. }
  335. if ($device->{vendor_id} eq '17') {
  336. push(@init_cmd,"more displine 50");
  337. push(@init_cmd,"more off");
  338. }
  339. if ($device->{vendor_id} eq '38') {
  340. push(@init_cmd,"disable cli prompting");
  341. push(@init_cmd,"disable clipaging");
  342. }
  343. netdev_cmd($device,$t,\@init_cmd,3);
  344. return $t;
  345. }
  346. #---------------------------------------------------------------------------------
  347. sub netdev_set_enable {
  348. my $session = shift;
  349. my $device = shift;
  350. return if (!exists $switch_auth{$device->{vendor_id}}{enable});
  351. my $cmd = $switch_auth{$device->{vendor_id}}{enable};
  352. netdev_cmd($device,$session,$cmd,3);
  353. if ($device->{enable_password}) { netdev_cmd($device,$session,$device->{enable_password},3); }
  354. }
  355. #---------------------------------------------------------------------------------
  356. sub netdev_cmd {
  357. my ($device,$session,$cmd,$telnet_version)=@_;
  358. my @result=();
  359. my @tmp=();
  360. if (ref($cmd) eq 'ARRAY') { @tmp = @{$cmd}; } else { @tmp = split(/\n/,$cmd); }
  361. if ($device->{proto} eq 'ssh') {
  362. eval {
  363. foreach my $run_cmd (@tmp) {
  364. next if (!$run_cmd);
  365. if ($run_cmd =~ /SLEEP/i) {
  366. if ($run_cmd =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
  367. next;
  368. }
  369. log_session('Send:'.$run_cmd);
  370. select(undef, undef, undef, 0.25);
  371. my @row = $session->capture($run_cmd."\r\n");
  372. chomp(@row);
  373. push(@result,@row);
  374. # my ($output, $errput) = $session->capture2({timeout => 5}, $run_cmd);
  375. # $session->error and die "ssh failed: " . $session->error;
  376. # chomp($output);
  377. # push(@result,$output);
  378. }
  379. log_session('Get:'.Dumper(\@result));
  380. };
  381. if ($@) { log_error("Abort: $@"); return 0; };
  382. }
  383. if ($device->{proto} eq 'essh') {
  384. eval {
  385. foreach my $run_cmd (@tmp) {
  386. next if (!$run_cmd);
  387. if ($run_cmd =~ /SLEEP/i) {
  388. if ($run_cmd =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
  389. next;
  390. }
  391. log_session('Send:'.$run_cmd);
  392. $session->send("$run_cmd\n");
  393. select(undef, undef, undef, 0.25);
  394. $session->expect(10, -re => qr/$device->{prompt}/);
  395. push(@result,$session->before());
  396. }
  397. log_session('Get:'.Dumper(\@result));
  398. };
  399. if ($@) { log_error("Abort: $@"); return 0; };
  400. }
  401. if ($device->{proto} eq 'telnet') {
  402. if (!$telnet_version) { $telnet_version = 1; }
  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,$cmd,1);
  429. };
  430. }
  431. #huawei
  432. if ($device->{vendor_id} eq '3') {
  433. eval {
  434. my $cmd = "quit\ntftp $tftp_ip put vrpcfg.zip $device->{device_name}.zip\nSLEEP 5\n";
  435. netdev_cmd($device,undef,$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,$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,$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,$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,$cmd,1);
  470. };
  471. }
  472. #allied telesys x210,x610
  473. if (in_array([50..53],$device->{device_model_id})) {
  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,$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,$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,$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,$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,$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,$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,$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,$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. return if (!$session);
  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. $telnet_cmd_mode = 1;
  590. if (!$descr) { $descr = "name ''"; } else { $descr = "name $descr"; }
  591. $cmd = "
  592. conf t
  593. interface port-channel $port_num
  594. $descr
  595. exit
  596. exit";
  597. }
  598. #raisecom
  599. if ($device->{vendor_id} eq '5') {
  600. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  601. $cmd = "
  602. conf t
  603. interface $port_num
  604. $descr
  605. exit
  606. exit";
  607. }
  608. #SNR
  609. if ($device->{vendor_id} eq '6') {
  610. $telnet_cmd_mode = 1;
  611. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  612. $cmd = "
  613. conf t
  614. interface $port
  615. $descr
  616. exit
  617. exit";
  618. }
  619. #Dlink
  620. if ($device->{vendor_id} eq '7') {
  621. $telnet_cmd_mode = 1;
  622. if (!$descr) { $descr = "clear_description"; } else { $descr = "description $descr"; }
  623. $cmd = "config ports $port_num $descr";
  624. }
  625. #allied telesys x210,x610
  626. if ($device->{vendor_id} eq '8') {
  627. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  628. $telnet_cmd_mode = 2;
  629. $cmd = "
  630. conf t
  631. interface $port
  632. $descr
  633. exit
  634. exit";
  635. }
  636. #allied telesys 8000
  637. if ($device->{device_model_id} eq '3') {
  638. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  639. $telnet_cmd_mode = 2;
  640. $cmd = "
  641. conf
  642. interface ethernet $port
  643. $descr
  644. exit
  645. exit";
  646. }
  647. #allied telesys 8100
  648. if ($device->{device_model_id} eq '4') {
  649. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  650. $telnet_cmd_mode = 2;
  651. $cmd = "
  652. conf t
  653. interface $port
  654. $descr
  655. exit
  656. exit";
  657. }
  658. #mikrotik
  659. if ($device->{vendor_id} eq '9') {
  660. $telnet_cmd_mode = 4;
  661. if (!$descr) { $descr='""'; } else { $descr='"'.$descr.'"'; }
  662. $cmd = "/interface ethernet set [ find default-name=$port ] comment=".$descr;
  663. }
  664. #cisco
  665. if ($device->{vendor_id} eq '16') {
  666. if (!$descr) { $descr = 'description ""'; } else { $descr = "description $descr"; }
  667. $cmd = "
  668. conf t
  669. interface $port
  670. $descr
  671. exit
  672. exit";
  673. }
  674. #maipu
  675. if ($device->{vendor_id} eq '17') {
  676. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  677. $cmd = "
  678. conf t
  679. port $port
  680. $descr
  681. exit
  682. exit";
  683. }
  684. #Qtech
  685. if ($device->{vendor_id} eq '38') {
  686. if (!$descr) { $descr = "no description"; } else { $descr = "description $descr"; }
  687. $cmd = "
  688. conf t
  689. interface $port
  690. $descr
  691. exit
  692. exit";
  693. }
  694. #Extreme
  695. if ($device->{vendor_id} eq '39') {
  696. if ($descr) {
  697. $cmd = "configure port $port_num display $descr";
  698. } else {
  699. $cmd = "unconfigure port $port_num display";
  700. }
  701. }
  702. netdev_cmd($device,$session,$cmd,$telnet_cmd_mode);
  703. }
  704. #---------------------------------------------------------------------------------
  705. sub netdev_set_hostname {
  706. my $session = shift;
  707. my $device = shift;
  708. my $cmd;
  709. my $telnet_cmd_mode = 4;
  710. return if (!$session);
  711. #eltex
  712. if ($device->{vendor_id} eq '2') {
  713. $cmd = "
  714. conf t
  715. hostname $device->{device_name}
  716. exit";
  717. }
  718. #huawei
  719. if ($device->{vendor_id} eq '3') {
  720. $cmd = "sysname $device->{device_name}";
  721. }
  722. #zyxel
  723. if ($device->{vendor_id} eq '4') {
  724. $telnet_cmd_mode = 1;
  725. $cmd = "
  726. conf t
  727. hostname $device->{device_name}
  728. exit";
  729. }
  730. #raisecom
  731. if ($device->{vendor_id} eq '5') {
  732. $cmd = "hostname $device->{device_name}";
  733. }
  734. #SNR
  735. if ($device->{vendor_id} eq '6') {
  736. $telnet_cmd_mode = 1;
  737. $cmd = "
  738. conf t
  739. hostname $device->{device_name}
  740. exit";
  741. }
  742. #Dlink
  743. if ($device->{vendor_id} eq '7') {
  744. $telnet_cmd_mode = 1;
  745. $cmd = "config hostname $device->{device_name}";
  746. }
  747. #allied telesys x210,x610 - default
  748. if ($device->{vendor_id} eq '8') {
  749. $telnet_cmd_mode = 2;
  750. $cmd = "
  751. conf t
  752. hostname $device->{device_name}
  753. exit";
  754. }
  755. #allied telesys 8000
  756. if ($device->{device_model_id} eq '3') {
  757. $telnet_cmd_mode = 2;
  758. $cmd = "
  759. conf
  760. hostname $device->{device_name}
  761. exit";
  762. }
  763. #allied telesys 8100
  764. if ($device->{device_model_id} eq '4') {
  765. $telnet_cmd_mode = 2;
  766. $cmd = "
  767. conf t
  768. hostname $device->{device_name}
  769. exit";
  770. }
  771. #mikrotik
  772. if ($device->{vendor_id} eq '9') {
  773. $telnet_cmd_mode = 4;
  774. $cmd = "/system identity set name=$device->{device_name}";
  775. }
  776. #cisco
  777. if ($device->{vendor_id} eq '16') {
  778. $cmd = "
  779. conf t
  780. hostname $device->{device_name}
  781. exit";
  782. }
  783. #maipu
  784. if ($device->{vendor_id} eq '17') {
  785. $cmd = "
  786. conf t
  787. hostname $device->{device_name}
  788. exit";
  789. }
  790. #Qtech
  791. if ($device->{vendor_id} eq '38') {
  792. $cmd = "
  793. conf t
  794. hostname $device->{device_name}
  795. exit";
  796. }
  797. #Extreme
  798. if ($device->{vendor_id} eq '39') {
  799. $cmd = "configure snmp sysName $device->{device_name}";
  800. }
  801. netdev_cmd($device,$session,$cmd,$telnet_cmd_mode);
  802. }
  803. #---------------------------------------------------------------------------------
  804. sub netdev_wr_mem {
  805. my $session = shift;
  806. my $device = shift;
  807. my $cmd;
  808. my $telnet_cmd_mode = 4;
  809. return if (!$session);
  810. #eltex
  811. if ($device->{vendor_id} eq '2') {
  812. $cmd = "wr
  813. Y";
  814. }
  815. #huawei
  816. if ($device->{vendor_id} eq '3') {
  817. $cmd = "quit
  818. save
  819. Y
  820. ";
  821. }
  822. #zyxel
  823. if ($device->{vendor_id} eq '4') { $cmd = "wr mem"; }
  824. #raisecom
  825. if ($device->{vendor_id} eq '5') { $cmd = "wr"; }
  826. #SNR
  827. if ($device->{vendor_id} eq '6') {
  828. $cmd="copy running-config startup-config
  829. Y";
  830. }
  831. #Dlink
  832. if ($device->{vendor_id} eq '7') { $cmd="save"; }
  833. #allied telesys x210,x610
  834. if ($device->{vendor_id} eq '8') {
  835. $cmd = "wr
  836. Y";
  837. }
  838. #allied telesys 8000
  839. if ($device->{device_model_id} eq '3') {
  840. $telnet_cmd_mode=2;
  841. $cmd = "copy running-config startup-config
  842. Y
  843. ";
  844. }
  845. #allied telesys 8100
  846. if ($device->{device_model_id} eq '4') {
  847. $cmd = "wr
  848. Y";
  849. }
  850. #cisco
  851. if ($device->{vendor_id} eq '16') { $cmd="wr_mem"; }
  852. #maipu
  853. if ($device->{vendor_id} eq '17') {
  854. $cmd = "wr
  855. Yes
  856. ";
  857. }
  858. #Qtech
  859. if ($device->{vendor_id} eq '38') {
  860. $cmd = "copy running-config startup-config
  861. Y
  862. ";
  863. }
  864. #Extreme
  865. if ($device->{vendor_id} eq '39') { $cmd="save configuration primary"; }
  866. netdev_cmd($device,$session,$cmd,$telnet_cmd_mode);
  867. }
  868. #---------------------------------------------------------------------------------
  869. 1;
  870. }