cmd.pm 27 KB

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