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