Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell to query AD 3 35
AWK: Pytthagoras bp script Part deux 22 33
Append data to an XML file 3 16
Exchange Online Management Scope Syntax 15 17
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…

829 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