-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_files.pl
86 lines (77 loc) · 2.05 KB
/
send_files.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
#!/usr/bin/perl
system ("cat ./unlock.sh");
# TODO Add command line options -files x x x x x x and -servers x.x.x.x x.x.x.x and -list and -help
print "\n\nWhich files would you like to send?";
print "\n-----------------------------------\n";
# Print file names in this directory
my @ls = `ls`;
my $i = 0;
for my $file_name (@ls){
print "$i. $file_name";
$i++;
}
# Choose which files to send
# TODO Loop until correct input is given
print "Enter numbers separated by spaces:\n";
my $response = <STDIN>;
if ($response !~ /^\s*(\d+\s+)+$/){
print "Invalid Input!\n";
exit;
}
my @files = split / /,$response;
for my $file(@files){
chomp($file);
if ($file >= $i){
print "\"$file\" is not in the list!\n";
exit;
}
}
# Choose servers that will receive file
# TODO Loop until correct input is given
print "Enter the FQDNs or IP Adresses of servers that will receive these files, separated by spaces:\n";
$response = <STDIN>;
if ($response !~ /^\s*([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+\s+)+$/){
print "Invalid Input!\n";
exit;
}
my @servers;
my @unpinged = split / /,$response;
for my $server (@unpinged){
chomp($server);
#my $ping = system("ping -qc 1 $server > /dev/null 2> /dev/null");
if ($ping){
print "$server was unreachable\n"
} else {
push @servers,"$server";
}
}
if (!@servers){
print "No servers were reachable\n";
exit;
}
# Choose the location for saving the files
print "Save file to [~]:\n";
chomp(my $location = <STDIN>);
$location ||= "~";
# Choose username to use
print "Username [root]:\n";
chomp(my $user = <STDIN>);
$user ||= "root";
if ($user =~ /\s/){
print "Invalid user";
}
# Send the files to servers
for my $server (@servers){
chomp($server);
my $files_to_transfer = "";
for my $number (@files){
chomp($file_name = @ls[$number]);
if (!$files_to_transfer){
$files_to_transfer = $file_name
} else {
$files_to_transfer = "$file_name,$files_to_transfer"
}
}
print "scp ./{$files_to_transfer} $user\@$server:$location\n";
`scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ./{$files_to_transfer} $user\@$server:$location`;
}