Solved

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

Posted on 2013-06-13
3
577 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
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
Comment Utility
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
Comment Utility
thank you!  How do I name and call an Expect script from within RHEL?
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now