sameerperl has asked for the wisdom of the Perl Monks concerning the following question:

In my program, i have used Net::SSH2 module to connect
remote system with non root user, and create
channel.
Trying to execute command
print "Channel is created\n";
$chan->shell;
$chan->blocking(0);
$chan->ext_data("merge");
print $chan "ssh -t 192.168.2.9\n";
sleep(3);
print $chan "access\n";
print $chan "pwd \n"
The above mention code tries to execute ssh command to
remote system,then it will ask for prompt how can i
stop that, I guess their is a one way going
with,private key authentication.
And the output of the above code is

Pseudo-terminal will not be allocated because stdin is not a terminal.
Permission denied, please try again.
Permission denied, please try again.
Permission denied
(publickey,gssapi-with-mic,password). -bash: line 2: access: command not found
/home/pwdir

Replies are listed 'Best First'.
Re: executing command with Net::SSH2
by zentara (Cardinal) on Jun 26, 2008 at 19:02 UTC
Re: executing command with Net::SSH2
by moritz (Cardinal) on Jun 26, 2008 at 16:29 UTC
    So are your trying to open a SSH connection on your server to a third host?

    That won't work, because ssh asks for a password, and wants to read that from the terminal, not from STDIN. You can try to use Net::SSH::Expect as a workaround.

      A simple example of Net::SSH::Expect..this simply connects
      through ssh then check the prompt then sends a command or input..
      my $ssh= Net::SSH::Expect->new( host => $host, password=> $password, user => $user, raw_pty =>1, log_file => $outputfile."\.raw" ); $ssh->login(); $ssh->send("sudo su"); if ($ssh->peek(1)=~/Password\:/ig){######check if Password prompt come +s up### $ssh->send("supassword"); } my $commandline=$ssh->eat($ssh->peek(1)); $commandline=~ s/Password\://g; #####remove string "Password:" $commandline=~ s/su|sudo su//g; #####remove string "su" or "sudo su" $commandline=~ s/^\s+|\s+$//g; ####remove trailing and leading whites +paces

      sorry for my regex..i know its bad..=p
      i hope the example helps

      P.S. please do not forget to use strict and warnings..=)