parse show lldp neighbors
[dell-switch] / mikrotik-switch.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 # example usage as pipe:
7 # ./ips | sed 's/^/ping /' | NO_LOG=1 ./dell-switch.pl sw-dpc
8
9 use Net::OpenSSH;
10 use Data::Dump qw(dump);
11 use Time::HiRes qw(sleep);
12
13 our $login;
14 our $passwd;
15 our $debug = $ENV{DEBUG} || 0;
16
17 use lib '.';
18 require 'config.pl';
19
20 #$Net::OpenSSH::debug = ~0;
21
22 my $hostname = shift @ARGV || die "usage: $0 hostname command[ command ...]\n";
23 my @commands = @ARGV;
24
25 if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into
26         while(<>) {
27                 push @commands, $_;
28         }
29 } elsif ( ! @commands ) {
30 #       push @commands, "/export verbose file=$hostname.rsc";
31         push @commands, "/export file=$hostname.rsc";
32         push @commands, "/tool fetch address=10.20.0.216 mode=ftp src-path=$hostname.rsc dst-path=upload/$hostname.rsc upload=yes";
33         my $file = "/srv/ftp/upload/$hostname.rsc";
34         if ( -e $file ) {
35                 system "sudo rm -vf $file";
36         }
37 }
38
39 $login .= '+c'; # Mikrotik console without colors
40
41 warn "\n## ssh $login\@$hostname\n";
42 my $ssh = Net::OpenSSH->new($hostname, user => $login, passwd => $passwd, 
43     ssh_cmd => '/usr/bin/ssh1', # apt-get install openssh-client-ssh1
44     master_opts => [
45         -o => "StrictHostKeyChecking=no",
46         -F => '/home/dpavlin/dell-switch/ssh1-config'
47         ],
48     default_ssh_opts => [
49         -o => "StrictHostKeyChecking=no",
50         -F => '/home/dpavlin/dell-switch/ssh1-config'
51         ],
52 );
53
54 my $command;
55 my @commands_while = ( @commands );
56
57 while ( my $command = shift @commands_while ) {
58
59         print "## $command\n";
60         my ($out, $err) = $ssh->capture2($command);
61       $ssh->error and
62         die "remote find command failed: " . $ssh->error;
63
64         warn "# out = ",dump($out);
65
66         print $out;
67 }
68
69 if ( ! @commands ) {
70         system "cp -v /srv/ftp/upload/$hostname.rsc mikrotik/";
71 }