cmd.pm 26 KB

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