[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-06-13
3
Medium Priority
?
587 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 20

Accepted Solution

by:
simon3270 earned 2000 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 20

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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

650 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