-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoshell.pl
executable file
·216 lines (183 loc) · 5.49 KB
/
autoshell.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/perl
use strict;
use Expect;
$Expect::Log_Stdout = 0;
#my $prg = debian_stable_check('mirror.yandex.ru');
my $prg = mplayer_ctrl('ctrl');
eval {
print "Current Debian STABLE is ",
$prg->(shell()
or die "Can`t spawn shell: $!"),
"\n";
};
print "Error: $@"
if $@;
#################################################################
sub mplayer_ctrl {
my $ctrl_file = shift;
my $env = { TIMEOUT => 1 };
runsh($env, "cat >/tmp/pppp",
__TIMEOUT => sub {
my $sh = shift @{$_[0]};
print $sh;
print $sh "PPP\n";
'exp_continue'
},
)
}
sub debian_stable_check {
my $mirror = shift;
my $env = { RVAL => '!!!UNKNOWN!!!' };
runsh($env, "lftp",
__READY =>
runsh($env, "open ".$mirror,
__READY =>
runsh($env, 'ls /debian/dists/',
qr"[^n]stable[^-]+\-\>[^\w]+(\w+)" =>
f_setenv('RVAL', f_getmatch()),
__READY =>
f_die('No symlink to stable'),
__TIMEOUT =>
f_die('Network error: timeout'),
),
),
__END => f_return(f_getenv('RVAL'))
)
}
#my $cc1 = ping('ya.ru');
#my $cc2 = ping('google.com');
#pinfo("Exe 1: ", exe($sh, $cc1, 'Hi', 'boss!'));
#pinfo("Exe 2: ", exe($sh, $cc2, 'Hi', 'boss!'));
# sub ping {
# my $host = shift
# || '127.0.0.1';
# sh({ }, "ping -c 1 ".$host, $SHELLPROMPT,
# '64 bytes from' => setrval("OK"),
# 'Unreachable' => setrval('Unreach'),
# # timeout => setrval('Timeout'),
# # 'packets transmitted' => debug(),
# );
# }
#################################################################
#################################################################
#################################################################
sub shell { Expect->spawn('/bin/sh') }
sub exe { $_[1]->( @_) }
sub runsh {
my ($env, $cmd, %args) = @_;
die "Env must be hash ref, but not ".ref $env
unless ref $env eq 'HASH';
$env->{PROMPT} = qr'[\$\>\#]\s$'
unless exists $env->{PROMPT};
my $promptc =
ref $args{__READY} eq 'CODE'
? sub { $args{__READY}->(@_) }
: sub { undef };
pre($env->{TIMEOUT} || 5, $env, $cmd."\n",
$env->{PROMPT} => $promptc,
%args,
)
}
sub f_setenv {
my ($key, $val, %flags) = @_;
my $type = ref $val;
sub {
my (undef, $env) = @_;
$env->{$key} = $type
? $type eq 'CODE'
? $val->(@_)
: $type eq 'ARRAY'
? [ @{$val} ]
: $type eq 'HASH'
? { %{$val} }
: $type eq 'SCALAR'
? \${$$val}
: die "Unwanted value type $type"
: $val;
$flags{'CONTINUE'}
? 'exp_continue'
: undef
}
}
sub f_getenv {
my ($key) = @_;
sub {
my (undef, $env) = @_;
$env->{$key}
}
}
#################################################################
#################################################################
sub f_getmatchlist { sub { shift->matchlist() } }
sub f_getmatch { sub { shift->matchlist()->[0] } }
sub f_die {
my @args = @_;
sub { die @args }
}
sub f_return {
my $val = shift;
my $type = ref $val;
sub {
$type
? $type eq 'CODE'
? $val->(@_)
: $type eq 'ARRAY'
? [ @{$val} ]
: $type eq 'HASH'
? { %{$val} }
: $type eq 'SCALAR'
? \${$$val}
: die "Unwanted value type $type"
: $val
}
}
sub debug {
my %flags = @_;
sub {
my ($sh, $ctx, @args) = @_;
pinfo("args: ", join(", ", @_));
pinfo("ctx: ", join(", ", map { $_."=".$ctx->{$_} } keys %{$ctx}));
# pinfo("matchs: ", join(", ", $sh->matchlist()));
$flags{'CONTINUE'}
? 'exp_continue'
: undef
}
}
sub pinfo { p("INFO", @_) };
sub p { print STDERR "\n\t[", shift, "] ", @_, "\n" };
#################################################################
sub pre {
my ($timeout, $_ctx, $cmd, %args) = @_;
my $beginc = $args{__BEGIN}
if $args{__BEGIN};
my $endinc = $args{__END}
if $args{__END};
my $ttlinc = ref $args{__TIMEOUT} eq 'CODE'
? sub { $args{__TIMEOUT}->(@_) }
: sub { die "Timeout on cmd $cmd" };
my %hooks =
map { $_ => $args{$_} }
grep { $_ !~ /\_\_/ }
keys %args;
sub {
$_[1] = $_ctx # Fix CTX (for first run)
unless ref $_[1];
my ($sh, @myargs) = @_;
die "Where mr. Expect? $sh"
unless ref($sh) eq 'Expect';
$beginc->($sh, @myargs)
if ref $beginc eq 'CODE';
print $sh $cmd
if $cmd;
my $rval = $sh->expect($timeout,
[ 'timeout', $ttlinc, @myargs],
map { [ $_, $hooks{$_}, @myargs] }
keys %hooks
);
ref $endinc eq 'CODE'
? $endinc->($sh, @myargs, $rval)
: defined $rval
? $rval
: undef
}
}