分类:
2008-10-17 13:36:12
#!/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 = ; $cmd =~ s/[\r\n]*$//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;