-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmikrotik_backup.pl
120 lines (67 loc) · 2.33 KB
/
mikrotik_backup.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl
use strict;
use Net::OpenSSH;
my @opt = ({object=>'test1',username=>'admin', password => 'killer86', ip=>'192.168.88.117'},
{object=>'test2', username=>'admin', password=>'1234', ip=>'192.168.88.115'} );
my $backup_name = "backup$$";
############################### COMMANDS #######################################
my $RSC_EXPORT = "/export file=$backup_name";
my $BACKUP = "/system backup save name=$backup_name";
my $INFO = '/system resource print';
my $ROUTING ='/ip route print';
my $IP_ADDRESS = '/ip address print';
my $INTERFACE = '/interface print';
my $FIRMWARE ='/system routerboard print';
my $HOSTNAME ='/system identity print';
#my $SYSTEM_UPGRADE ='/system routerboard upgrade';
my $SSHPASS = '/usr/bin/sshpass';
my $SCP = '/usr/bin/scp';
################################ WORK #########################################
print"Begin|".scalar localtime."|"."\n";
foreach my $obj (@opt) {
my $ip=$obj->{ip};
my $backup_dir = "/home/projects/mikrotik_task/tmp/$obj->{object}_$$/";
`mkdir -p $backup_dir`;
my $ssh = Net::OpenSSH->new("$obj->{username}\@$ip",timeout => 30, password=>$obj->{password});
$ssh->error and die "Unable to connect: " + $ssh->error;
$ssh->system($RSC_EXPORT);
`$SSHPASS -p $obj->{password} $SCP $obj->{username}\@$ip:/$backup_name.rsc $backup_dir`;
$ssh->system($BACKUP);
`$SSHPASS -p $obj->{password} $SCP $obj->{username}\@$ip:/$backup_name.backup $backup_dir`;
#$ssh->system($SYSTEM_UPGRADE);
my $hostname = $ssh->pipe_out($HOSTNAME) or die $!;
while(<$hostname>){
$hostname = $_;
}
chomp($hostname);
$hostname =~ s/name://;
$hostname =~ s/\s+|\t+//;
$hostname =~ s/\r//;
$hostname =~ s/$//;
open(FH, '>', $backup_dir.$hostname ."_$$.txt");
print "Connected to $ip\n";
my $info = $ssh->pipe_out($INFO) or die "Unable to run command";
while (<$info>) {
print FH $_;
}
$info= $ssh->pipe_out($ROUTING) or die $!;
while (<$info>){
print FH $_;
}
$info = $ssh->pipe_out($IP_ADDRESS) or die $!;
while (<$info>){
print FH $_;
}
$info = $ssh->pipe_out($INTERFACE) or die $!;
while (<$info>){
print FH $_;
}
$info = $ssh->pipe_out($FIRMWARE) or die $!;
while (<$info>){
print FH $_;
}
close FH;
close $info;
undef $ssh;
}
print"Complete|".scalar localtime."|"."\n";