Solved

bash for loop server list from file1, stdOut to file2.

Posted on 2013-06-13
3
583 Views
Last Modified: 2013-06-13
I have a list of servers which I need to install a remote RPM on. I also need to trap stdOut to a text file so I know which ones failed or already had the RPM installed. Can you advise me how to approach this using a for loop in bash or running it as an .sh and calling the contents of the input servers file and the output echo file.?
I'm happy to even do it with Powershell from a Win Host if necesary.

Something like:

For each
                $SERVERNAME in FILE1;
Do
             StdOut to File2  (I want to see if it is already installed or errors or sudo fails..)
             use the following to stdIn:
                ssh username@domain@$NextSERVERNAME
                password
                yes
                sudo su –
                password
                “rpm -Uvh http://FQDN.packagename.noarch.rpm” 
            Until EOF
END

You guys Rock, thanks in advance!!
0
Comment
Question by:SaffronThePuppy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 39245833
I think you'll need to use something like "expect" to respond to the password prompts from ssh and sudo, so this could be something like:
while read servername; do
  expect  >> file2 <<EOF
set timeout 5
spawn ssh user@${servername}.domain
expect "sword"
send "password\r"
expect "continue"
send "yes\r"
exopect "\$"
send "sudo su –\r"
expect "sword"
send "password\r"
expect "#"
send "rpm -Uvh http://FQDN.packagename.noarch.rpm\r"
expect "#"
send "exit\r"
expect eof
EOF
done < file1

Open in new window

I've set the timeout short because you may or may not get the "do you want to continue" prompt, depending on your ssh settings, and you may not get the "sudo" password prompt if you run the script twice within a short time.  A short timeout means it doesn't hang around for long waiting for a prompt it never gets!

The script assumes that your main prompt has a "$" in it and your root prompt a "#".

You'll also have passwords strewn around your script and output file - not good.  It woudl be better to set up ssh keys for the servers, and to allow the user to run the rpm command through sudo without a password being required.
0
 

Author Comment

by:SaffronThePuppy
ID: 39245975
thank you!  How do I name and call an Expect script from within RHEL?
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39246057
The expect script here is running on your main server. I've written it as a shell script, with the expect scrip embedded in it. You can call it what you like, the "chmod +x script_name".
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question