Remote execution of a local script on multiple servers


 
Thread Tools Search this Thread
Operating Systems Solaris Remote execution of a local script on multiple servers
# 1  
Old 02-27-2013
Oracle Remote execution of a local script on multiple servers

So I have a scriptlet called solaris_command:

Code:
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:

Code:
solaris_command "cat /etc/hosts">>hosts.txt

this works for most instances but I am trying to do something a little more advanced and I can't seem to bend it to my will.

what I would like to do is something like:
Code:
 sed -e 's/^/$HOSTNAME /' /etc/vfstab|egrep -v -e $HOSTNAME' #'|expand|egrep -e '(svr|ldm|zone)'|cut -d ' ' -f 1-2,4|sed -e 's/ /,/'

basically reading in /etc/vfstab|manipulating it a couple times with sed, grep and cut to get the data I want in the format I want and then save it to a .txt file on the server I ran the command from.

however with respect to
Code:
$HOSTNAME

it outputs the hostname of the server the command was run on, not the name of the remote server. I'm stumped. I've tried it with
Code:
\$HOSTNAME

,
Code:
$HOSTNAME

,
Code:
`echo $HOSTNAME`

.
Code:
`hostname`

, and even
Code:
`echo /etc/nodename`

to no avail.
# 2  
Old 03-01-2013
There were a few similar posts in forum
Shell Programming and Scripting - The UNIX and Linux Forums
I propose two separate scripts, this avoids the quoting chaos with remote execution.
Just make a simple "remote_script", that will run on the remote host:
Code:
#!/bin/bash
sed -e 's/^/$HOSTNAME /' /etc/vfstab|egrep -v -e $HOSTNAME' #'|expand|egrep -e '(svr|ldm|zone)'|cut -d ' ' -f 1-2,4|sed -e 's/ /,/'

Then you pass it by this modified "solaris_command" script:
Code:
for i in \
server1 server2 server3
do 
 echo $i
 ssh -x $i /bin/bash < remote_script
 echo ""
done

For efficiency I added -x
# 3  
Old 03-01-2013
This is exactly what I am attempting to do, the problem is when I use
Code:
$hostname

the variable is populated with the hostname of the server running the script not the remote system.
# 4  
Old 03-01-2013
You had one script,
and passed the second script as an argument to ssh - problem.

While I suggest two separate scripts.
ssh directly reads the "remote_script" via stdin - the current shell does not see it and cannot do substitutions.
So the /bin/bash on the remote system first sees and substitutes variables in the "remote_script".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep remote multiple hosts output to local server

Hello all, i'm trying to create a report by greping a pattern on multiple remote hosts and creta a simple report, actually i did this, is ther any better way to do this. #!/bin/bash for host in `cat RemoteHosts` do ssh $host -C 'hostname 2>&1; grep ERROR /var/log/WebServer.log.2019-09-21... (0 Replies)
Discussion started by: charli1
0 Replies

2. Homework & Coursework Questions

Parallel execution on multiple servers in UNIX

I have a requirement (in a shell script) to connect to several servers at once and execute a series of commands. I am aware that ssh can be used for sequential execution. But since most of the commands that I need to execute take a long time, I have to go for the parallel option. Is there... (2 Replies)
Discussion started by: sneha1887
2 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Execution of local commands for remote site.

Hi all, I have a problem with ftp execution within unix environment. I'd like to get files on remote and delete them later, but here is too crowd so I can accidentally delete some files. Can I delete only the files I can get to the local folder? I can ask this question with a different... (14 Replies)
Discussion started by: attillam
14 Replies

5. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

6. Shell Programming and Scripting

Local script to trigger multiple remote scripts

Hi All, I am facing problem running a script which triggers another script in multiple remote servers. my script in my local server looks like below ssh server1 "sudo -u uname /usr/local/script/start.sh &2>&1 >/dev/null " ssh server2 "sudo -u uname /usr/local/script/start.sh &2>&1 >/dev/null "... (7 Replies)
Discussion started by: sain
7 Replies

7. 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

8. 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

9. UNIX for Dummies Questions & Answers

Running the same remote script on multiple servers

Experts, 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... (2 Replies)
Discussion started by: OMLEELA
2 Replies

10. HP-UX

Automatic execution of commands in multiple servers using single script.

Hi, I've to do a simple job many times whenever it has been asked, just i've to log in to all of fourtien HP servers and i've to execute ps -fu user > temp cat temp|sendmail "xyz@z.com" commands to send the statics of all of 14 servers over the mail to particular user id.. Though logging... (4 Replies)
Discussion started by: vickramshetty
4 Replies
Login or Register to Ask a Question