-
Notifications
You must be signed in to change notification settings - Fork 0
/
get
executable file
·61 lines (51 loc) · 1.78 KB
/
get
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
#!/usr/bin/perl
use strict;
use DBI;
use Expect;
use FindBin;
use Digest::SHA1 qw(sha1_hex);
my $h=DBI->connect("DBI:mysql::mysql_read_default_file=$FindBin::Bin/.my.cnf");
$h->do('Insert Into `log`() Values()');
my $logID=$h->{'mysql_insertid'};
my $ins=$h->prepare("Insert Into config(`log`, device, info) values($logID, ?, ?)");
my $s=$h->prepare(<<SQL);
Select device.*, model.ANSI, snippet.command,
sha1((Select info From config Where device=device.id Order By ctime Desc Limit 1)) As sha1,
(Select command From snippet Where model=model.id And name='<session>') As cmdA,
(Select command From snippet Where model=model.id And name='</session>') As cmdB,
(Select command From snippet Where model=model.id And name='<#>') As cmdX,
(Select command From snippet Where model=model.id And name='</#>') As cmdZ
From device, model, snippet
Where Not disable And itime is Not Null
And device.model=model.id
And device.model=snippet.model And snippet.name='config'
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 -batch -i), "$FindBin::Bin/.ssh/cfgather.ppk", "cfgather\@$r->{host}");
$z->send(
$r->{cmdA}."\n".
($r->{cmdX} or '#')."<Configuration>".$r->{cmdZ}."\n".
$r->{command}."\n".
($r->{cmdX} or '#')."</Configuration>".$r->{cmdZ}."\n".
$r->{cmdB}."\n");
$z->expect(10);
my $S=$z->before;
undef $z;
$S=~s/\e\[.*?[a-z]//ig if $r->{ANSI}; # Clear ANSI terminal escapes
$S=~s|(?s).*(?m-s)<Configuration>.*$||;
$S=~s|(?m)^.*(?s-m)</Configuration>.*||;
$S=~s/^\s+//;
$S=~s/\s+$//;
$S.="\n";
next if sha1_hex($S) eq $r->{sha1};
$ins->execute($r->{id}, $S);
}
__END__