Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: send su password in ssh

by salva (Canon)
on Sep 28, 2010 at 08:09 UTC ( [id://862369]=note: print w/replies, xml ) Need Help??


in reply to send su password in ssh

The script below (copied from Net::OpenSSH distribution) does exactly what you want.

It uses Net::OpenSSH to establish the connection to the remote host and Expect to handle the interaction with sudo.

#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Expect; select STDOUT; $| = 1; select STDERR; $| = 1; my $password = $ARGV[0]; my $timeout = 20; my $debug = 0; my $ssh = Net::OpenSSH->new('test@127.0.0.1', password => $password); # my ($pty, $pid) = $ssh->open2pty("sudo cat /etc/shadow") # After a successful sudo operation, it doesn't request the password # again until some time after, handling this undeterministic behaviour # is a pain in the ass, so we just clear any cached credentials # calling "sudo -k" first as follows: my ($pty, $pid) = $ssh->open2pty("sudo -k; sudo cat /etc/shadow") or die "open2pty failed: " . $ssh->error . "\n"; my $expect = Expect->init($pty); $expect->raw_pty(1); $debug and $expect->log_user(1); $debug and print "waiting for password prompt\n"; $expect->expect($timeout, ':') or die "expect failed\n"; $debug and print "prompt seen\n"; $expect->send("$password\n"); $debug and print "password sent\n"; $expect->expect($timeout, "\n") or die "bad password\n"; $debug and print "password ok\n"; while(<$pty>) { print "$. $_" }
Note that Net::OpenSSH does not work on Windows.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found