cmd.pm 26 KB

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