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!

permission denied with Net::Telnet

Status
Not open for further replies.
Oct 27, 2002
21
0
0
US
Greetings all,
I am trying to use interactive login and have been using the Net::Telnet command as that is the route I was pointed to by a friend. I am using the script from CPAN for the ssh example. I keep getting a

"problem connecting to host: Permission denied (password)"

which is the result I do get when I log in to the device and put in the password wrong three times. So I am pretty sure it is logging in to the device, just not sure why it is reading the password wrong or if it is really reading it.

I have pasted the code I am using below and just changed the host, username and password. I am trying to log in to a Juniper Netscreen VPN Device.

Any pointers would be appreciated. If there is a better way to do the interactive login and you can point me in that direction that would be great also. Netscreen devices here require Interactive login. examples would rock, but any help is appreciated.

Thanks

Wally


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

## Main program.
    {
        my ($pty, $ssh, @lines);
        my $host = "myservername";
        my $user = "myusername";
        my $password = "Mypassword";
        my $prompt = '/:~> $/';

        ## Start ssh program.
        $pty = &spawn("ssh", "-l", $user, $host);  # spawn() defined below

        ## 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("get hostname");
        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
 
Change all of these
my $host = "myservername";
my $user = "myusername";
my $password = "Mypassword";
to single quotes. Chances are your password has a $ or @ in it which perl interpreted.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I tried it with single quotes and got the same thing.

My username contains an underscore _ but I have tried that with both a \_ and regular _

I know I am missing something simple, just not sure what it is.

TIA

Wally
 
do you need to hit enter after you type the pass?
ssh->print("$password\n");

Net telnet has a few dump files you can enable that will help you out.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
hitting enter is not working either. Kinda weird, I know I am missing something really basic.

I will try to use the dump files and see what I get. Any clues on the syntax for outputting the output log and input log? The examples don't show it being used. I hvae tried outputting to an array but I get no return results. Do you have an example of the output and input log entries?

TIA

Wally
 
$ssh = new Net::Telnet (-fhopen => $pty,
-prompt => $prompt,
-telnetmode => 0,
-cmd_remove_mode => 1,
-output_record_separator => "\r",
-input_log => '/path/to/log/input_log.txt',
-dump_log => '/path/to/log/dump_log.txt',
);



now I have never prepended these with dashes but I'm assuming it's working if you are doing it now.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travs69,
Thanks alot for all your help. I got the dump files working and as soon as I did that, I figured out where the glitch was and got the script working. Thanks for all your help.

Wally
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top