IPC::Session - remote shell persistent session object; encapsulates
open3()
http://www.cpan.org/authors/id/S/ST/STEVEGT/IPC-Session-0.03.tar.gz
use IPC::Session;
# open ssh session to fred # -- set timeout of 30 seconds for all send() calls my $session = new IPC::Session("ssh fred",30); $session->send("hostname"); # run `hostname` command on fred print $session->stdout(); # prints "fred" $session->send("date"); # run `date` within same ssh print $session->stdout(); # prints date # use like 'expect': $session->send("uname -s"); for ($session->stdout) { /IRIX/ && do { $netstat = "/usr/etc/netstat" }; /ConvexOS/ && do { $netstat = "/usr/ucb/netstat" }; /Linux/ && do { $netstat = "/bin/netstat" }; } # errno returned in scalar context: $errno = $session->send("$netstat -rn"); # try this: $session->send("grep '^$user:' /etc/passwd") && warn "$user not there"; # hash returned in array context: %netstat = $session->send("$netstat -in"); print "$netstat{'stdout'}\n"; # prints interface table print "$netstat{'stderr'}\n"; # prints nothing (hopefully) print "$netstat{'errno'}\n"; # prints 0
This module encapsulates the open3()
function call (see IPC) and its associated filehandles, making it easy to maintain multiple
persistent 'ssh' and/or 'rsh' sessions within the same perl script.
The remote shell session is kept open for the life of the object; this avoids the overhead of repeatedly opening remote shells via multiple ssh or rsh calls. This persistence is particularly useful if you are using ssh for your remote shell invocation; it helps you overcome the high ssh startup time.
For applications requiring remote command invocation, this module provides functionality that is similar to 'expect' or Expect.pm, but in a lightweight more Perlish package, with discrete STDOUT, STDERR, and return code processing.
The constructor accepts the command string to be used to open the remote shell session, such as ssh or rsh; it also accepts an optional timeout value, in seconds. It returns a reference to the unique session object.
If the timeout is not specified then it defaults to 60 seconds. The timeout value can also be changed later; see timeout().
The send()
method accepts a command string to be executed on
the remote host. The command will be executed in the context of the default
shell of the remote user (unless you start a different shell by sending the
appropriate command...). All shell escapes, command line terminators,
pipes, redirectors, etc. are legal and should work, though you of course
will have to escape special characters that have meaning to Perl.
In a scalar context, this method returns the return code produced by the command string.
In an array context, this method returns a hash containing the return code as well as the full text of the command string's output from the STDOUT and STDERR file handles. The hash keys are 'stdout', 'stderr', and 'errno'.
Returns the full STDOUT text generated from the last send()
command string.
Also available via array context return codes -- see send().
Returns the full STDERR text generated from the last send()
command string.
Also available via array context return codes -- see send().
Returns the return code generated from the last send()
command
string.
Also available via array context return codes -- see send().
Allows you to change the timeout for subsequent send()
calls.
The timeout value is in seconds. Fractional seconds are allowed. The
timeout applies to all send()
calls.
Returns the current timeout. Can be called with no args.
The remote shell command you specify in new()
is assumed to
not prompt for any passwords or present any challenge codes; i.e.; you must
use .rhosts or the equivalent. This restriction may be removed in future
versions of this module, but it's there now.
Steve Traugott <[email protected]>
IPC, rsh(1), ssh(1), Expect, expect(1)