Here is what I am trying to do. I have few servers on which i have to make sure the password for a normal user and root is what I think it should. Here is my logic:

write expect script.

ssh as normal user and provide password via expect when needed. --------->If this login is successful, then "su root" and provide root password when needed.

every successful/failed attempt will be written to a log file the script will loop through all the IP address that i have saved in a text file.

My problem is, I am able to verify the normal user's password, but if it is correct, my script is failing to run "su -". here is the script:

#!/usr/bin/perl #use strict; use warnings; use Expect; open WRITE, "> output" or die $!; open READ, "ip_address" or die $!; my @ip_array = <READ>; foreach (@ip_array) { chomp ($_); my $command = "ssh"; my @parameters = ("slayedbylucifer\@$_", "uptime"); my $exp = new Expect; $exp->raw_pty(1); $exp->spawn($command, @parameters) or die "Cannont spwan $command: $!\n"; $exp->log_stdout(0); # stop the output going to STDOUT my $timeout=10; $exp->expect($timeout, [ qr/yes/ => sub { my $exp = shift; $exp->send("yes\r"); exp_continue; } ], [qr/assword/i => sub { my $exp = shift; $exp->send("12345\n"); exp_continue; } ] ); if ($? == 0) { print WRITE "The password is correct for \"slayedbylucifer\" on $ +_\n"; ######### Let's su and check the password for root ####### my $command1 = "su"; my @parameters1 = ("-"); my $exp1 = new Expect; $exp1->raw_pty(1); $exp->spawn($command1, @parameters1) or die "Cannt spawn $command1: $!\n"; $exp->expect($timeout, [qr/assword/i => sub { my $exp1 = shift; $exp->send("root_password\n"); exp_continue; } ] ); if ( $? == 0 ) { print WRITE "The password is correct for \"root\" on $_\n"; } else { print WRITE "The password is wrong for \"root\" on $_\n"; } } else { print WRITE "The password is wrong for \"slayedbylucifer\" on $_ +\n"; } } close WRITE;

and here is the output I get:

Cannot reuse an object with an already spawned command at ./ping.pl li +ne 43

I am not good with the OO stuff, but that's how Expect module works. Thanks.


In reply to Expect module and running "su" by slayedbylucifer

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.