LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-05-2012, 09:41 AM   #1
ms1
LQ Newbie
 
Registered: Jul 2012
Location: Ireland
Posts: 6

Rep: Reputation: Disabled
How do I use "expect" to supply details after 5 line intro appears, e.g. password for


Hello,

I've a query here to do with 'expect' that I was hoping for some help with. I'd be rather new to scripting. In short I suspect I have difficulties in getting the expect to expect five lines of an intro first rather than the usual word or two like "username" or "password".

I want to configure my CentOS 5.8 so that when the machine reboots apache will start automatically. As it stands I currently must run the apache manually and then enter a password, say "blah". I had understood that I ought to be able to get apache started automatically with 'expect' and an rc.local. With that in mind I did a 'yum install expect' and created a directory called /usr/local/scripts. In here I created a file called start_httpd.sh and made it executable with a chmod 755. With apache off I did a trial run on starting apache with this file. On running the file with a ./start_httpd.sh the following appeared:

[root@]# ./start_httpd.sh
spawn /usr/local/apache2/bin/apachectl start
Apache/2.2.22 mod_ssl/2.2.22 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server xxx.yyyy.com:443 (RSA)
Enter pass phrase:

and then it hung. Using a "ps -elf | grep httpd | grep -v gr" on another putty session I can see that the apache has started but is waiting for a password.

So how do I get my expect to expect:
"
Apache/2.2.22 mod_ssl/2.2.22 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server xxx.yyyy.com:443 (RSA)
Enter pass phrase:
"
before sending the password?

My own script looks as follows:

#!/usr/bin/expect
#
# expect script to interactively provide the password to the apache start
# script. this runs at startup so that the apache is available automatically
# after each system boot.
#
# xxxxxx, July 2012
#

set timeout 30

# start the actual start script itself
spawn /usr/local/apache2/bin/apachectl start

# look for an indication that the Apache server is already running or not
expect {
# apache reverse proxy is running, so just exit
"httpd (pid*" {exit;}
}

# wait for the password prompt
expect {
"Apach*phrase:" {send "blah"\r";}
# "*Enter pass phrase:" {send "blah\r";}
# "*Enter pass phrase:" {send "blah\r"; exp_continue}
}
exit;

In passing if the apache is already running the script works normally in that it acknowledges httpd is running and exits fine. So the query anyhow is how do I get the expect to be able to deal with five lines before the 'Enter pass phrase:'? when the apache is not running.

Kind regards,
M
 
Old 07-05-2012, 10:00 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
You don't need an expect script to start Apache, you can do that with the regular init system (actually this is one of its main purposes). You can use chkconfig to tell CentOS that you want to have httpd (which is Apache) started on boot: http://www.centos.org/docs/5/html/De...chkconfig.html
 
Old 07-05-2012, 10:06 AM   #3
ms1
LQ Newbie
 
Registered: Jul 2012
Location: Ireland
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the comment TobiSGD. How do I supply the password then? To start the apache I enter "./apachectl start" from the /usr/local/apache2/bin/ directory and then enter a password. With chkconfig apache/httpd could be started but where would I specify the password?
 
Old 07-05-2012, 10:08 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
I never heard that anyone needs a password to start Apache, but I am not an expert in this matter. I would just give it a try and see if it works. If not I hope a member with more Apache experience steps in.
 
Old 07-05-2012, 10:10 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
Lightbulb

Quote:
Originally Posted by ms1 View Post
Thanks for the comment TobiSGD. How do I supply the password then? To start the apache I enter "./apachectl start" from the /usr/local/apache2/bin/ directory and then enter a password. With chkconfig apache/httpd could be started but where would I specify the password?

It is not always necessary to use expect, you can use something like this (assuming password is asked once).

Code:
./apachectl start << 'EOF'
your_password
EOF
 
Old 07-05-2012, 10:21 AM   #6
ms1
LQ Newbie
 
Registered: Jul 2012
Location: Ireland
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by vikas027 View Post
It is not always necessary to use expect, you can use something like this (assuming password is asked once).

Code:
./apachectl start << 'EOF'
your_password
EOF
Thanks vikas027 but unfortunately with the above and variations of it I still have to manually enter the password after being requested for it. I feel just having the password entered automatically would be the key as I could probably go from there.
 
Old 07-05-2012, 12:09 PM   #7
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Ok,

If you're posting the code use the [code] tags around the text for better reading

In your script I may see a typo, so this might be the case:
Code:
"
Apache/2.2.22 mod_ssl/2.2.22 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server xxx.yyyy.com:443 (RSA)
Enter pass phrase:
"

...
# wait for the password prompt
expect {
"Apach*phrase:" {send "blah"\r";}
# "*Enter pass phrase:" {send "blah\r";}
# "*Enter pass phrase:" {send "blah\r"; exp_continue}
}
exit;
the red marked text doesn't seem the same to me, so expect probably doesn't recognize it neither.
 
Old 07-05-2012, 12:25 PM   #8
ms1
LQ Newbie
 
Registered: Jul 2012
Location: Ireland
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks Lithos and also the tip about the [code]. I would agree that you are right that this is the location of the solution. This
Code:
"Apach*phrase:"{send "blah"\r";}
was the last variation of many I tried.

Code:
"*Enter pass phrase:" {send "blah\r";}
and
Code:
"*Enter pass phrase:" {send "blah\r"; exp_continue}
were also tried with no luck. The wildcard of * worked well with the following

Code:
expect {
# apache reverse proxy is running, so just exit
"httpd (pid*" {exit;}
}
so in principle * works. Just to get an 'expect' that matches with

Apache/2.2.22 mod_ssl/2.2.22 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server xxx.yyyy.com:443 (RSA)
Enter pass phrase:


would be 90% of the way to the solution methinks.
 
Old 07-05-2012, 01:23 PM   #9
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by ms1 View Post
Thanks vikas027 but unfortunately with the above and variations of it I still have to manually enter the password after being requested for it. I feel just having the password entered automatically would be the key as I could probably go from there.
Can you please send me a sample output of a command where you provide passwords ?

Ideally this should work, similar to below.

Code:
root@box1:~# passwd vikas << 'EOF'
> abc123
> abc123
> EOF
Changing password for user vikas.
New password: BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: passwd: all authentication tokens updated successfully.
root@box1:~#
 
1 members found this post helpful.
Old 07-05-2012, 07:18 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Actually, you just need to change the Apache SSL setup to still use Certs, but NOT require a passwd at startup eg http://ubuntuforums.org/showthread.php?t=1112664

Here's Centos specific HOWTO; see especially Section 5.10.2 and the preceding sections.
http://www.dedoimedo.com/computers/w...-server-lm.pdf
 
2 members found this post helpful.
Old 07-06-2012, 06:05 AM   #11
ms1
LQ Newbie
 
Registered: Jul 2012
Location: Ireland
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks vikas027. Yes, I'll remember that as a handy way to change passwords and possibly other scipts but it doesn't seem to work here for automatically starting apache. Here is the output on restarting apache.

------------------------------------------
# ./apachectl start
Apache/2.2.22 mod_ssl/2.2.22 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server huller.gosnetworks.com:443 (RSA)
Enter pass phrase:

------------------------------------------

chrism01 seems to have definite answer too (thank you) especially with the CentOS link but at the moment I'd prefer not to have to recompile the setup as someone else made the certs first day. Might have to do it though.
 
Old 07-06-2012, 06:50 AM   #12
ms1
LQ Newbie
 
Registered: Jul 2012
Location: Ireland
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks vikas027. Yes, I'll remember that as a handy way to change passwords and possibly other scipts but it doesn't seem to work here for automatically starting apache. Here is the output on restarting apache.

------------------------------------------
# ./apachectl start
Apache/2.2.22 mod_ssl/2.2.22 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server huller.gosnetworks.com:443 (RSA)
Enter pass phrase:

------------------------------------------

chrism01 seems to have definite answer too (thank you) especially with the CentOS link but at the moment I'd prefer not to have to recompile the setup as someone else made the certs first day. Might have to do it though.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
unique password for "update" and "synaptic" and "apt-get" SaintDanBert Linux - Security 1 09-17-2010 05:53 AM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 03:55 AM
My "expect" script is not working... Won't "send" commands... edomingox Programming 4 04-02-2009 04:25 PM
How to supply extra commands along with PHP header("Location: http://www.abc.com")? alphaque Programming 3 05-26-2006 05:28 AM
"Quiet" power supply buzzes speakers ghight General 4 11-11-2004 12:48 PM


All times are GMT -5. The time now is 02:51 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration