Hi , I am trying to do a "su -" in a remote machine and run some commands as root.

With the below script i am able to login to the remote machine, but i get "su: incorrect password" error even when the password is correct, i guess i am not passing the password in the correct format.

Please look at the below code and let me know where i am going wrong Thanks.

#! /usr/bin/perl use Net::SSH::Expect; #Preparing for ssh session my $ssh = Net::SSH::Expect->new ( host=>"poseidon", user=>'testusr', timeout=>10, raw_pty=>1, password=>'wel123go', log_file=>"log.txt" ); #Logging in to the session $ssh->login(); print "Logged in \n"; # Commands executed $ssh->exec("stty raw -echo"); $whoami=$ssh->exec("whoami"); print "\$whoami : ",$whoami,"\n"; $ssh->send("su -"); $ssh->waitfor('Password:\s*\z', 1) or die "prompt 'Password' not found + after 1 second"; my $pass = 'passw0rd'; $result = $ssh->send($pass); print "\$result : ",$result,"\n"; $whoami=$ssh->exec("whoami"); print "\$whoami : ",$whoami,"\n"; # Expect to see root #closing the session $ssh->close();

Content of the log file :

Password:

Last login: Tue Sep 1 23:53:01 2009 from poseidon.com^M

testusr@poseidon.com:~> testusr@etshmctbox10:~> hscpe

testusr@poseidon.com:~> Password:

su: incorrect password

testusr@etshmctbox10:~>


In reply to Change user to root using ssh::expect by want2bemonk

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.