cmd.pm 25 KB

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