使用Expect和命名管状远程控制SQL*Plus
2008-12-17 13:06:03 来源:WEB开发网 #!/usr/bin/perl -w
# spd.pl - the SQL*Plus daemon server
use strict;
use Expect;
use Socket;
# set up expect
# -- timeout after about 10 minutes
my $timeout = 600;
# -- scan for the SQL*Plus prompt
my $prompt = 'SQL>';
my $exp = new Expect();
$exp->raw_pty(1);
$exp->log_stdout(0);
$exp->spawn('sqlplus','/nolog') || die "unable to spawn sqlplus: $!";
$exp->expect($timeout,'-ex',$prompt) || die $exp->error();
print $exp "set sqlprompt $prompt;n";
$exp->expect($timeout,'-ex',$prompt) || die $exp->error();
$exp->clear_accum();
my $name = "/tmp/sp_$ENV{USER}";
unlink($name);
socket(S,PF_UNIX,SOCK_STREAM,0) || die "socket: $!";
bind(S,sockaddr_un($name)) || die "bind: $!";
listen(S,SOMAXCONN) || die "listen: $!";
while(accept(C,S))
{
# single threaded to avoid confusion
my $cmd = <C>;
$cmd =~ s/[rn]*$//g;
print $exp $cmd,"n";
if ($cmd =~ /^exit$/mi)
{
print C "exit.n";
close C;
last;
}
$exp->expect($timeout,$prompt) || die $exp->error();
print C $exp->before();
close C;
}
$exp->soft_close();
close S;
unlink($name);
exit;
更多精彩
赞助商链接