Running the same remote script on multiple servers

Thread Tools Search this Thread
# 1  
Running the same remote script on multiple servers


Im trying to remote into a server, run a script that resides on that server and capture the information displayed & store in a local file.

I struggled with this yesterday & finally that script is working now.

Now, here is a scope creep and the script that I wrote for 1 remote server now, should run on more than one server and then capture the log from the server and store it on my local desktop to be sent to mgt for further decision making.
However I tried this in more than one way, but none of the approaches that I took are yielding any results. The script when invoked individually work fine for each remote server but when I combine them, the second one fails to run.

Here is what I have so far. The highlight BOLD lines are working well now.

Please advise !


VAR=$(expect -c "
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
echo "==============="
echo "$VAR" >> /Users/leemc/Results.txt

sleep 5

####### W O R K S T I L L H E R E #### 

VAR=$(expect -c "
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
echo "==============="
echo "$VAR" >> /Users/leemc/Results.txt

sleep 5

Here is what Results.txt Looks after first write -
3434 RICHARD CB ELLIS Properties Mgt
1033 CB Gundaker Property Mgt ^M
1151 KELLER WILLIAMS Property Mgt^M

spawn ssh -o StrictHostKeyChecking=no ./^M

Moderator's Comments:
Mod Comment Use code tags, please...

Last edited by Scott; 02-27-2011 at 09:48 AM.. Reason: Removed excessive formatting, added code tags
# 2  
First, do not use variables already in use by UNIX or your shell, use MY_NAMES_FOR_SURE !

Using a var as a file seems silly for this situation, and might not take a big output robustly, but why store when you can flow; why not just put around all "( ... ) >>log_file 2>&1" and let them inherit stdout/stderr ? "2>&1" is a good idea, so the outputs stay together in the log in original time order.

Using PPK passwordless login, and you would not need expect. That is more secure than passwords in scripts, as well as simpler to invoke.
# 3  
Using expect is bound to occasionally fail in weird ways when ssh does something you didn't, well, expect. It's very unsecure too. Just set up keys, it's much less hair pull and lets you securely use ssh the way it was designed.

When you do logins properly instead of faking them with expect, you can redirect entire scripts into ssh and have them run on the remote server letter for letter, like so:

for HOST in A B C D E
        ssh username@${HOST} exec /bin/sh < /path/to/

The exec /bin/sh is to make sure you get a predictable shell on the remote server and not csh or something else weird. Anything should have some sort of bourne shell...

There's mountains of instructions all over the internet for passwordless ssh. First you create a key, then ssh-copy-id username@host which amounts to the same thing as adding your key to the remote host's allowed keys. You should be able to login noninteractively thereafter.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #433
Difficulty: Easy
JSON, or JavaScript Object Notation, is a general-purpose data interchange format that is defined as a subset of JavaScript's object literal syntax.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error running grep on Remote Servers

Im running the below command sshpass -p mypassword ssh -t user1@server2 /bin/bash -c 'echo "mypassword" | sudo -S -l; echo "$?#`grep -iE "user66|dbuser|tomcat|splunk|stash|jira|user2|docadmin" /etc/passwd`"; exit' Below is the error I get: Output: I run this command across a... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

A webservice running on multiple servers. A patch has been deployed to them and it got successful o

A webservice running on multiple servers. A patch has been deployed to them and it got successful only in some set of servers. The change is in format of "logontime" from " hh24:mi:ss" to "dd/mm/yyyy hh24:mi:ss". A script has already generated the output from all servers with request and... (2 Replies)
Discussion started by: Chandan_Bose
2 Replies

3. Shell Programming and Scripting

Shell script for remote servers

Hi , I have written a small script : set -x #!/bin/ksh for i in `cat /tmp/list` ( list contains remove servers ) do ssh -t $i << EOF uname -a cd ~user echo "Enter the dir >" read dir path=`ll -ld /home/user/"$dir"` if ; then echo "Dir exists " read rm $path else echo "no such... (9 Replies)
Discussion started by: kpatel786
9 Replies

4. Shell Programming and Scripting

Running set of commands in remote servers in shell script

Hi Wishing to all. I am very new joined in an organization as a unix system administrator. I need a help in preparing a script for a report. i have a file contains all of the linux/ubuntu servers line by line around 140 servers. vi servers.txt nh01 nh02 nh03 bh01 bh04 - - :wq (3 Replies)
Discussion started by: kumaraswamy
3 Replies

5. Shell Programming and Scripting

Remote login and running a script on multiple servers

Hi all, I am baffled on this. Solaris Irix system.:confused: I have 4 servers all connected to one another, :b: I need to write a script line that would login on to server 1-3 ($HOST) start a script in the back ground and log off while the back ground script runs over a length of time.:eek: ... (10 Replies)
Discussion started by: weddy
10 Replies

6. Solaris

Remote execution of a local script on multiple servers

So I have a scriptlet called solaris_command: for i in \ server1 server2 server3 do echo $i ssh $i $1 echo "" done I then use that as a command in multiple scripts to allow for data gathering for all virtual hosts in the environment thusly: solaris_command "cat... (3 Replies)
Discussion started by: os2mac
3 Replies

7. Shell Programming and Scripting

Logging in to multiple Linux servers and running the command.

Hi, I am trying to write a script to run a command on multiple linux based servers and get the o/p. I am using ssh to login. It is a celerra box and EMC NAS product. I am able login but i am not able to run nas command nas_pool -size -all the NAS server. I am getting the following error. ... (2 Replies)
Discussion started by: jpkumar10
2 Replies

8. Shell Programming and Scripting

Running a script on multiple remote hosts at once

I have a script on about 15 hosts that I need to run for each host whenever I want (not crontab). Problem is, this script takes 5-10 mins to run for each host. Is there a way I can run the script in parallel for all the hosts instead of 1 at a time? Also, I'm remotely running the script on the... (3 Replies)
Discussion started by: mrskittles99
3 Replies

9. UNIX for Advanced & Expert Users

Move folders from Multiple remote Servers to my local computer

I have 20 Servers (They are Windows 2003) that I remote every day using names or IP address and type in my username & Password then copy folders manually to my local computer. I'm wondering if i can just run script(s) from my local computer to do this job without using FTP(because of security... (5 Replies)
Discussion started by: idiazza
5 Replies

10. Shell Programming and Scripting

Prevent wrong user from using shell script for multiple remote servers

Hi, I am running a shell script from a central server to multiple remote servers using the following code: application_check() { # Linux/UNIX box with ssh key based login SERVERS=`cat /tmp/server-details` # SSH User name USR="user" # create new file > /tmp/abc.log # connect... (2 Replies)
Discussion started by: mystition
2 Replies

Featured Tech Videos