cmd.pm 27 KB

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