Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
go ahead... be a heretic
 
PerlMonks  

Re^2: regular expression matching in expect

by cmastays (Initiate)
on Aug 09, 2012 at 19:04 UTC ( #986586=note: print w/ replies, xml ) Need Help??


in reply to Re: regular expression matching in expect
in thread regular expression matching in expect

That regular expression works fine. Thanks a lot.

However i am facing a new issue now. After ssh, I can encounter either RSA key authentication and password prompt(First time) or only password prompt. Then I want to be able to continue to look for bash prompt so that I can continue my other linux commands.Here is my code snippet

$exp->spawn("ssh -l root $sshHostname") or die "cannot spawn ssh: $!\n +"; $exp->expect($timeout,[ qr/RSA/i,#/ sub { my $fh = shift; $fh->send("yes\n"); }); $exp->expect($timeout,'-re','password' => sub { my $fh = shift; $fh->send("$sshPWD\n");} ); $exp->log_file->print("#Successfully ssh'ed in\n"); $exp->expect($timeout, '-re','~]#' => sub {my $fh = shift; $fh->send("cd /u0x/tmp\n");}


Comment on Re^2: regular expression matching in expect
Download Code
Re^3: regular expression matching in expect
by aitap (Chaplain) on Aug 09, 2012 at 20:27 UTC
    Well, what error message do you receive? According to the documentation, near the New more Tcl/Expect-like interface of $object->expect(), you can return 'exp_continue' symbol from your callback sub to continue matching with next callback.
    Are you sure it won't be easier to use some SSH-related modules, for example, Net::SSH::Expect?
    Sorry if my advice was wrong.

      I did try using exp_continue; statement after every send but that didnt seem to help either.

      BTW. what advantage will Net::ssh offer that Use Expect doesnt?

        I did try using exp_continue; statement after every send but that didnt seem to help either.
        Did you try returning it?

        what advantage will Net::ssh offer that Use Expect doesnt?
        More clean Perl code? You will need only to do things in ssh instead of running ssh manually and agreeing with it.

        Sorry if my advice was wrong.
Re^3: regular expression matching in expect
by zengargoyle (Deacon) on Aug 10, 2012 at 11:31 UTC
    If you're not too picky about remote host authentication...
    ssh -o 'StrictHostKeyChecking no' -o 'CheckHostIP no' $host
    See man ssh_config for more details. It can also be handy to use options to set the known_hosts file to /dev/null. Otherwise if you're picky it's probably best to manually ssh to all of your destination hosts once and accept the key manually and then failing if you get anything but login / password. It may be even handier to use the -l option to ssh to specify the login name so you don't have to try and match for the login case. Then you get down to the way my simple scripts of this type work: ssh as a specific user, ignore host key stuff, just wait for password and then the prompt... anything else is a die.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-04-19 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls