LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2011, 08:03 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question 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:
Code:
[sudo] password for vikas:
or
Password:
This is my "cmd" file passed in pass () function.
Code:
ssh -t -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 192.168.1.100 "bash rcmds"


This is my script output
Quote:
[vikas@box1 ~]$ ./rscript.sh
++ rm -rf /home/vikas/May_31
++ mkdir -p /home/vikas/May_31
++ set +x


Checking if Hosts are reachable .........

++ 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.
 
Old 05-31-2011, 05:05 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
I think perl's expect handles regex in the patterns so you could use a pattern like below to match both of them

Code:
$exp->expect($timeout,
 ['-re',"assword.*?:",sub {$exp->send("$pass\n");}]
 );
 
Old 06-01-2011, 06:04 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Unhappy

Quote:
Originally Posted by estabroo View Post
I think perl's expect handles regex in the patterns so you could use a pattern like below to match both of them

Code:
$exp->expect($timeout,
 ['-re',"assword.*?:",sub {$exp->send("$pass\n");}]
 );
Hi estabroo,
This too not working for me.
Same error.
 
Old 06-01-2011, 02:20 PM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Hi All/Estabroo

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.

Anyway to automate this through perl-Expect ?
 
Old 06-01-2011, 05:01 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
edit the sudoers file to disable passwords for some commands.
 
Old 06-01-2011, 05:09 PM   #6
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by bigearsbilly View Post
edit the sudoers file to disable passwords for some commands.
That is the easiest way to do so, but sadly my customer prohibits that.
 
Old 06-01-2011, 05:13 PM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you have a silly customer.
:-)

perhaps the admin at the other end is a bit rubbish?
I'm glad it isn't my job.

can you not convince them this is impractical?
 
Old 06-01-2011, 05:19 PM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
have you tried using autoexpect

it usually comes with expect.

That's usually where I start.
 
Old 06-02-2011, 04:12 AM   #9
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by bigearsbilly View Post
have you tried using autoexpect

it usually comes with expect.

That's usually where I start.
I do not have any option apart from perl-Expect.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Expect To Execute Remote Commands samarudge Linux - Software 3 09-22-2011 04:10 AM
perl module for getting information from remote machines kdelover Programming 15 10-17-2010 10:13 PM
list perl module & install perl module DBD::mysql linson_85 Linux - Newbie 4 06-22-2009 10:42 AM
Problem with SUDO : sudo: pam_authenticate: Module is unknown cristoph_ Linux - Software 2 03-02-2009 07:12 PM
how to expect with sudo? rabbit2345 Programming 4 04-28-2008 07:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration