/
PerlCheckSSH.pl
138 lines (126 loc) · 4.11 KB
/
PerlCheckSSH.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/perl
use strict;
#use Expect;
#use IPC::Run qw( start pump finish timeout );
use PerlShareCommon::Dirs;
use PerlShareCommon::Constants;
#
# Connect using Expect to the host we want
# Exit codes determine what has happened
#
my $share = shift @ARGV or die usage();
my $host = shift @ARGV or die usage();
my $user = shift @ARGV or die usage();
my $pass = shift @ARGV or die usage();
my $public_keyfile = shift @ARGV;
my $user_agent = user_agent();
# Try to login to the host (using proxytunnel)
# And send over the public key file, if public_keyfile has been given
my $os = $^O;
my $keepalives = ($os=~/^MSWin/) ? "-o 'ProtocolKeepAlives 5'" : "";
my $cat = ($os=~/^MSWin/) ? "type" : "cat";
my $remote_dir = "/home/perlshare/$user/$share";
#my ($in, $out, $err, $exp);
#my $in;
#my $out;
#my $err;
#my $exp;
#my @c = ("ssh","-f","-l","hans\@oesterolt.net", "iconnect","echo OK");
#$exp = start(\@c, \$in, \$out, \$err, timeout(3));
#pump $exp until $out =~ /password|OK/;
#print "$out";
#exit 0;
if (defined($public_keyfile)) {
my $cmd = "$cat $public_keyfile | ".
"ssh -o 'StrictHostKeyChecking no' ".
"-o 'ProxyCommand proxytunnel -q -p $host:80 -d localhost:22 -H \"$user_agent\"' ".
"$keepalives ".
"-l $user $host ".
"\"cat >>.ssh/authorized_keys2;echo 'OKOKOK'\"";
} else {
my $cmd = "ssh -o 'StrictHostKeyChecking no' ".
"-o 'ProxyCommand proxytunnel -q -p $host:80 -d localhost:22 -H \"$user_agent\"' ".
"-o 'ProtocolKeepAlives 5' ".
"-l $user $host ".
"\"mkdir -p $remote_dir;chmod 775 $remote_dir;echo -10 >$remote_dir/.count;echo 'OKOKOK'\"";
open my $fh, "sshpass -p $pass $cmd |";
while (my $line = <$fh>) {
print $line;
}
close($fh);
exit(0);
# my @cmd = ("ssh", "-o","StrictHostKeyChecking no",
# "-o","ProxyCommand proxytunnel -q -p $host:80 -d localhost:22 -H \"$user_agent\"",
# "$keepalives",
# "-l","$user",
# "$host",
# "mkdir -p $remote_dir;chmod 775 $remote_dir;echo -10 >$remote_dir/.count;echo 'OKOKOK'"
# );
# $exp = start( \@cmd, \$in, \$out, \$err, timeout(3) );
}
# pump $exp until $out =~ /(password:|OKOKOK)/ || $err =~/(password:|OKOKOK)/;
# if ($1 eq "") {
# if ($1 eq "password:") {
# $in .= "$pass\n";
# pump $exp until $out =~ /(OKOKOK)/;
# if ($1 eq "OKOKOK") {
# # ok
# } else {
# finish($exp);
# exit 2; # password not accepted
# }
# } else {
# # ok
# }
# } else {
# finish($exp);
# exit 1; # not even connected
# }
#
# finish($exp);
exit 0;
# my $exp = new Expect();
# my $remote_dir = "/home/perlshare/$user/$share";
#
# if (defined($public_keyfile)) {
# $exp->spawn("cat $public_keyfile | ".
# "ssh -o 'StrictHostKeyChecking no' ".
# "-o 'ProxyCommand proxytunnel -q -p $host:80 -d localhost:22 -H \"$user_agent\"' ".
# "-o 'ProtocolKeepAlives 5' ".
# "-l $user $host ".
# "\"cat >>.ssh/authorized_keys2;echo 'OKOKOK'\""
# );
# } else {
# $exp->spawn(
# "ssh -o 'StrictHostKeyChecking no' ".
# "-o 'ProxyCommand proxytunnel -q -p $host:80 -d localhost:22 -H \"$user_agent\"' ".
# "-o 'ProtocolKeepAlives 5' ".
# "-l $user $host ".
# "\"mkdir -p $remote_dir;chmod 775 $remote_dir;echo -10 >$remote_dir/.count;echo 'OKOKOK'\""
# );
# }
#
# my $index = $exp->expect(3, "password:", "OKOKOK");
#
# if (defined($index)) {
# if ($index == 1) {
# $exp->send("$pass\n");
# $index = $exp->expect(3, "OKOKOK");
# if (defined($index)) {
# # do nothing
# } else {
# exit 2; # Password not accepted
# }
# } else {
# # do nothing
# }
# } else {
# exit 1; # Not even a connection possible
# }
#
# $exp->soft_close();
exit 0;
sub usage() {
print "$0 share host user pass [public keyfile]\n";
exit 3;
}