cmd.pm 27 KB

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