-
Notifications
You must be signed in to change notification settings - Fork 0
/
init
executable file
·60 lines (48 loc) · 1.56 KB
/
init
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
#!/usr/bin/perl
use strict;
use DBI;
use Expect;
use FindBin;
print STDERR "\n\nWarning: Not running from terminal!\n" unless -t;
print STDERR "\n\nWarning: SSH Agent not found!\n" unless $ENV{SSH_AUTH_SOCK};
my $h=DBI->connect("DBI:mysql::mysql_read_default_file=$FindBin::Bin/.my.cnf");
prepareFilter();
my $upd=$h->prepare("Update device Set itime=Now() Where id=?");
my $s=$h->prepare(<<SQL);
Select device.*, snippet.command
From device, snippet
Where Not disable And itime is Null
And device.model=snippet.model And snippet.name='init',
(Select command From snippet Where model=device.model And name='<session>') As cmdA,
(Select command From snippet Where model=device.model And name='</session>') As cmdB,
(Select command From snippet Where model=device.model And name='<config>') As cmdX,
(Select command From snippet Where model=device.model And name='</config>') As cmdZ
SQL
$s->execute;
while(my $r=$s->fetchrow_hashref)
{
print $r->{name}, "\t[", $r->{host}, "]\n" if -t;
my $z=new Expect;
$z->slave->stty(qw(-echo));
$z->raw_pty(1);
$z->log_stdout(0);
$z->restart_timeout_upon_receive(1);
$z->spawn(qw(/usr/bin/plink -A -pw NoNe), $r->{host});
$z->expect(3, ['Store key in cache', sub{shift->send("Y\n"); exp_continue;}]);
next if $z->error<0; # EOF=Access denied
$z->send(
$r->{cmdA}."\n".
$r->{cmdX}."\n".
$r->{command}."\n".
$r->{cmdZ}."\n".
$r->{cmdB}."\n");
$z->expect(3);
$upd->execute($r->{id});
my $S=$z->before;
$S=~s/\e.*?[a-z]//ig if $r->{ANSI}; # Clear ANSI terminal escapes
print "$S\n";
}
sub prepareFilter
{
}
__END__