#!/usr/local/bin/perl use strict; use FindBin; use lib "$FindBin::Bin/lib"; use MySSH; use Net::SSH::Perl::Constants qw( :msg ); my($sendonce) = 0; # note: this is packet-based, not line-based sub ssh1_recv { my($ssh, $packet) = @_; my $buffer = $packet->get_str; syswrite STDOUT, $buffer; if($buffer =~ /> / and not $sendonce) { ssh1_send($ssh,"?\n"); $sendonce = 1; } } # note: this is packet-based, not line-based sub ssh2_recv { my($channel, $buffer) = @_; my $str = $buffer->bytes; syswrite STDOUT, $str; if($buffer =~ /> / and not $sendonce) { ssh2_send($channel,"?\n"); $sendonce = 1; } } sub ssh1_send { my($ssh,$data) = @_; my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str($data); $packet->send; } sub ssh2_send { my($channel,$data) = @_; $channel->send_data($data); } my($host,$user,$pass) = @ARGV; my $ssh = MySSH->new($host, use_pty => 1, debug => $ENV{DEBUG} || 0, ) or die("new MySSH failed"); $ssh->login($user,$pass) or die("login failed"); if(ref($ssh) eq "Net::SSH::Perl::SSH1") { $ssh->register_handler(SSH_SMSG_STDOUT_DATA, \&ssh1_recv); } elsif(ref($ssh) eq "Net::SSH::Perl::SSH2") { $ssh->register_handler("stdout", \&ssh2_recv); } else { die("unknown ssh type: ".ref($ssh)); } $ssh->scripted_shell(); print "Connection to $host closed.\n";