Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Net::SSH::Expect question

Status
Not open for further replies.
Oct 27, 2002
21
US
Greetings all,
Still in pursuit of a good login for my non interactive devices. I started reading about Net::SSH::Expect and I think my problem might be how it talks to SSH as I get output from the script, but I am not getting all of the output and I am not sure why.

this is the output I get from the code below:

[root@wally-lnx cgi-bin]# perl mytest.cgi
This is the login promt
Last login: Fri Mar 28 13:33:56 2008 from localhost.localdomain
[wally@wally-lnx ~]$
this is the ls
Test Printing
Test Print 2
[root@wally-lnx cgi-bin]#

I am not sure why I am not getting the simple ls command results back and would appreciate any assistance.
I added a few additional print lines to ensure that I am getting any output and I even added text into the line where the ls is returned and I see that also so not sure why I am not seeing the ls itself.

Code is below:

Code:
#! /usr/bin/perl -w
use Net::SSH::Expect;

        #
        # You can do SSH authentication with user-password or without it.
        #

        # Making an ssh connection with user-password authentication
        # 1) construct the object
        my $ssh = Net::SSH::Expect->new (
            host => "localhost", 
            password=> 'mypassword', 
            user => 'myusername', 
            raw_pty => 1,
	 );

        # 2) logon to the SSH server using those credentials.
        # test the login output to make sure we had success
        my $login_output = $ssh->login();
print "This is the login promt $login_output";
print "\n";        

if ($login_output !~ /login/) {
            die "Login has failed. Login output was $login_output";
        }
        
        # 2) now start the ssh process
        $ssh->run_ssh() or die "SSH process couldn't start: $!";
        
        # 3) you should be logged on now. Test if you received the remote prompt:
        ($ssh->read_all(2) =~ /wally/) or die "where's the remote prompt?";

        # - now you know you're logged in - #

        # disable terminal translations and echo on the SSH server
        # executing on the server the stty command:
        $ssh->exec("stty raw -echo");

        # runs arbitrary commands and print their outputs 
        # (including the remote prompt comming at the end)
        my $ls = $ssh->exec("ls /home/wally");
        print ("this is the ls $ls\n");
        print ("Test Printing\n");
        print "Test Print 2\n";        
        
        $ssh->close();
 
Although I'm not familiar with Net::SSH::Expect, I am familiar (and use) Net::SSH::perl:

my $ssh = Net::SSH::perl->new('server');
$ssh->login('user','pass');
my ($o, $e, $x) = $ssh->cmd('ls -al /tmp');
print STDOUT "STDOUT was: " . $o . "\n";
print STDERR "STDERR was: " . $e . "\n";
print "exit code was: " . $x . "\n";


If you want to pass info to the command (i.e. STDIN), try this syntax:

$ssh->cmd($command, [ $stdin ]);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top