Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Change user to root using ssh::expect

by want2bemonk (Initiate)
on Sep 01, 2009 at 18:32 UTC ( [id://792713]=perlquestion: print w/replies, xml ) Need Help??

want2bemonk has asked for the wisdom of the Perl Monks concerning the following question:

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:~>

Replies are listed 'Best First'.
Re: Change user to root using ssh::expect
by ig (Vicar) on Sep 01, 2009 at 19:51 UTC

    I don't know why it isn't working.

    I would not embed passwords in a script - particularly root passwords. Therefore I wouldn't try to solve the problem you are experiencing.

    I would use public key authentication to enable automated access on a remote system, particularly in the case of root access. I might also use sudo to enable select commands for non-root users, rather than su to root.

Re: Change user to root using ssh::expect
by skx (Parson) on Sep 01, 2009 at 19:59 UTC

    I'd double-check the password just to be sure, but if you are sure then it might be something such as a missing "\n" (or newline)

    You could test for that by changing your code to read:

    my $pass = 'passw0rd'; $result = $ssh->send($pass . "\n" );
    Steve
    --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found