Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

ssh using expect

by bala_999 (Novice)
on Dec 04, 2007 at 18:10 UTC ( #654894=perlquestion: print w/ replies, xml ) Need Help??
bala_999 has asked for the wisdom of the Perl Monks concerning the following question:

HI, I am new to perl.I have written a piece of code to automate login.I was able to login to my host,i am unable to switch to super user mode ..Also,i want to execute multiple commands on host & extract output of each executed commands.How to extract Output of each command i executed? Im sorry that im asking you to write the code.I will appreciate ur suggestion..
#!/bin/perl use Expect; $host = xxxxx; $user = yyyyy; $pw = shift; #--- CONFIGURATION --------------------------------------------------- +--------# $cmd = "ssh -l $user $host"; $prompt = "[Pp]assword"; #--- START SSH LOGIN SEQUENCE ! -------------------------------------- +--------# $exp = new Expect(); $exp->log_file("SSHLOGFILE.txt"); $exp->raw_pty(1); $exp->spawn($cmd); $exp->expect(10, [ qr/\(yes\/no\)\?\s*$/ => sub { $spawn->send("yes\n"); + exp_continue; } ], [ $prompt => sub { $_[0]->send("pwd\n"); } ]); Here im unable to switch to super user login $exp->send("su - root\n"); $exp->expect(10, [ $prompt => sub { $_[0]->send("pwd\n"); } ]); $exp->close();

Comment on ssh using expect
Download Code
Re: ssh using expect
by moritz (Cardinal) on Dec 04, 2007 at 18:18 UTC
    Here im unable to switch to super user login

    So what happens?

      HI , Using Expect iwas able to login but Net::SSH::Expect does work for me. I was not able to login at all.If u have chance can u look at my script ..I am facing timeout problem. Thanks,
Re: ssh using expect
by tirwhan (Abbot) on Dec 04, 2007 at 18:23 UTC

    My suggestion: use Net::SSH::Expect, it has an example for interactive commands right there in documentation Synopsis.


    All dogma is stupid.
      I Have tried with Net::SSH::Expect ...IF i run my script sometime,i was able to login to host..Sometimes not.. Not sure that i had used Net::SSH::Expect correctly..! Let me know whats wrong in this code..
      #! /usr/bin/perl use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new ( host=>"xxx", user=>'yyy', timeout=>10, raw_pty=>1 ); $prompt="[Pp]assword"; $ssh->run_ssh(); $ssh->waitfor('qr/\(yes\/no\)\?$/',2); $ssh->send("yes\n"); $ssh->waitfor('qr/$prompt:\s*$/',5); $ssh->send("password"); my $peshlogin=$ssh->exec("su - root\n"); $ssh->waitfor('qr/$prompt:\s*$/',5); $ssh->send("password\n"); my $peshlogin=$ssh->exec('echo $?' . "\n","4"); print "THE LOGIN is $peshlogin\n"; $ssh->close();

        One major thing is wrong: no error checking anywhere. Take a look at the Synopsis again, all those "or die" statements are there for a purpose (change them to "or warn" if you want a softer fail).

        Also, you're mixing calls to $ssh->exec() with $ssh->waitfor, which is wrong. exec reads out the data sent by the server, so by the time waitfor is called it has nothing to ... err... wait for. You should use "send" in conjunction with "waitfor". Again, RTFM


        All dogma is stupid.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://654894]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-09-17 01:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls