#!/usr/bin/perl -w
# $Id: pssh,v 1.8 2001/03/07 04:18:15 btrott Exp $

use strict;

use Net::SSH::Perl;
use Net::SSH::Perl::Constants qw( :msg );
use Getopt::Long;

my %opts;
Getopt::Long::Configure('no_ignore_case');
GetOptions(\%opts, "c=s", "l=s", "p=i", "i=s@", "v", "t", "C");
my($host, $cmd) = @ARGV;

die "usage: pssh [options] hostname [command]"
    unless $host;

my %args = (interactive => 1);
$args{compression} = 1 if $opts{C};
$args{cipher} = $opts{c} if $opts{c};
$args{port} = $opts{p} if $opts{p};
$args{debug} = 1 if $opts{v};
$args{identity_files} = $opts{i} if $opts{i};
$args{use_pty} = 1 if $opts{t};

my $ssh = Net::SSH::Perl->new($host, %args);
$ssh->login($opts{l});

if ($cmd) {
    my($out, $err, $exit) = $ssh->cmd($cmd);
    print $out;
}
else {
    eval "use Term::ReadKey;";
    ReadMode('raw');
    eval "END { ReadMode('restore') };";
    $ssh->shell;
    print "Connection to $host closed.\n";
}
