Solved

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

Posted on 2013-06-13
3
585 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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

A procedure for exporting installed hotfix details of remote computers using powershell
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
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…

728 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