cmd.pm 25 KB

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