cmd.pm 25 KB

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