cmd.pm 27 KB

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