Hi,

I've been trying to get the SSH example on the Net::Telnet manpage to work without success. Here is the script:
## Main program. { my ($pty, $ssh, @lines); my $host = "changeme"; my $user = "changeme"; my $password = "changeme"; my $prompt = '/changeme:~> $/'; ## Start ssh program. $pty = &spawn("ssh", "-l", $user, $host); # spawn() defined b +elow ## Create a Net::Telnet object to perform I/O on ssh's tty. use Net::Telnet; $ssh = new Net::Telnet (-fhopen => $pty, -prompt => $prompt, -telnetmode => 0, -cmd_remove_mode => 1, -output_record_separator => "\r"); ## Login to remote host. $ssh->waitfor(-match => '/password: ?$/i', -errmode => "return") or die "problem connecting to host: ", $ssh->lastline; $ssh->print($password); $ssh->waitfor(-match => $ssh->prompt, -errmode => "return") or die "login failed: ", $ssh->lastline; ## Send command, get and print its output. @lines = $ssh->cmd("who"); print @lines; exit; } # end main program sub spawn { my(@cmd) = @_; my($pid, $pty, $tty, $tty_fd); ## Create a new pseudo terminal. use IO::Pty (); $pty = new IO::Pty or die $!; ## Execute the program in another process. unless ($pid = fork) { # child process die "problem spawning program: $!\n" unless defined $pid; ## Disassociate process from existing controlling terminal +. use POSIX (); POSIX::setsid or die "setsid failed: $!"; ## Associate process with a new controlling terminal. $tty = $pty->slave; $tty_fd = $tty->fileno; close $pty; ## Make stdio use the new controlling terminal. open STDIN, "<&$tty_fd" or die $!; open STDOUT, ">&$tty_fd" or die $!; open STDERR, ">&STDOUT" or die $!; close $tty; ## Execute requested program. exec @cmd or die "problem executing $cmd[0]\n"; } # end child process $pty; } # end sub spawn
Of course I changed the values changeme to the right ones. I've also defined the input_log to be /tmp/inputlog, and all I ever get to see is:
Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password,keyboard-interactive).

Just to verify the ssh binary is in working order, I can ssh to the server with the username/password I've defined in the script from the shell without probs.

As far as I've been able to troubleshoot the problem, it would seem like the SSH password prompt which is normally sent to the controlling TTY by the ssh binary is not being read' by the Perl script.

I have also tried to read what $tty is reading in and the password prompt is definately not there, only the Permission denied messages.

I'd appreciate any light you Perl monks can shed on this! :-)

Infact, if you have the time, try copying and pasting the above code into a file, modify the changeme values, ensure you have IO::Pty and Net::Telnet installed, and see if it works for you. I've tried it on 2 different systems and the password prompt is simply not read.

Thanks!

Janitored by tye: Add READMORE tags


In reply to IO::Pty, Net::Telnet and SSH [SOLVED] by gonza

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.