Perl Expect Module to give passwords for remote sudo commands
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Perl Expect Module to give passwords for remote sudo commands
Hi All,
I have written a script to run commands on remote servers, it is working fine.
But when I am running "sudo commands" on the remote servers, it asks for me password after prompting for ssh password. I am unable to automate this password prompt (which is just after ssh password prompt).
This is the function I am using to provide passwords
Code:
pass ()
{
cd $DIR/"$dt1"_"$dt"
/usr/bin/perl << 'EOF'
use strict;
use Expect;
my $timeout = '320';
my $pass = `cat password`;
my $cmd = `cat cmd`;
my $exp = Expect->spawn("$cmd")
or die "Cannot spawn ssh: $!\n";
$exp->expect($timeout,
["assword:",sub {$exp->send("$pass\n");}]
);
$exp->soft_close()
EOF
cd $DIR
}
I want the same function to be used , when it expects for sudo passwords for any of the below lines:
++ ping -c 2 random
ping: unknown host random
++ '[' 2 -eq 0 ']'
++ echo -e -n '\n\n random is UNREACHABLE...\n\n\t\t Please CHECK ...!!\n\n'
random is UNREACHABLE...
Please CHECK ...!!
++ echo random
++ set +x
++ ping -c 2 192.168.1.100
++ '[' 0 -eq 0 ']'
++ echo 192.168.1.100
++ set +x
Please enter password for vikas : Vicky#098
++ host=192.168.1.100
++ '[' 192.168.1.100 == CBICDG-J04 ']'
++ echo 'scp -p -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /home/vikas/May_31/rcmds 192.168.1.100:'
++ pass
++ cd /home/vikas/May_31
++ /usr/bin/perl
Warning: Permanently added '192.168.1.100' (RSA) to the list of known hosts.
reverse mapping checking getaddrinfo for kick.168.192.in-addr.arpa failed - POSSIBLE BREAK-IN ATTEMPT!
vikas@192.168.1.100's password:
rcmds 100% 189 0.2KB/s 00:00
++ cd /home/vikas
++ echo 'ssh -t -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 192.168.1.100 "bash rcmds"'
++ pass
++ cd /home/vikas/May_31
++ /usr/bin/perl
Warning: Permanently added '192.168.1.100' (RSA) to the list of known hosts.
reverse mapping checking getaddrinfo for kick.168.192.in-addr.arpa failed - POSSIBLE BREAK-IN ATTEMPT!
vikas@192.168.1.100's password: <--- This is working fine
[sudo] password for vikas: <--- I am stucked in giving this password
Any suggestions, how to automate the password prompt required for sudo commands.
Thanks in advance.
Last edited by vikas027; 05-31-2011 at 08:06 AM.
Reason: wrote more info.
I was not able to provide two password entries simultaneously, but I did a work around. I first added ssh-keys and then ran sudo command through ssh , and then supplied password from my pass function. This indeed worked well for me
BUT, still my question remains the same, how to provide input automatically when there is more than one input is required.
For E.g. ssh-keygen -t dsa requires user to press enter key (taking all defaults) 4-5 times.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.