Executing scripts on remote servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing scripts on remote servers
# 1  
Old 10-10-2011
Executing scripts on remote servers

Hello all,

I'm typing away a script that will send a script to a remote host and execute it.

This is what i have at the moment and i was wondering if i can improve that or thats basicly what everybody does.

Using bash on RHEL 5.5

To over simplify it...

Code:
#!/bin/bash
#
start_ () {
    cat > /tmp/toto.tmp <<EOF
su - user1 -c /user1/main -s $_toto 
su - user1 -c /user1/push -n host2   
su - user1 -c /user1/push -n host3    
EOF
    cat /tmp/toto.tmp | ssh -q -l user2 host1 "cat > /tmp/toto.tmp"
    ssh -q -l user2 "chmod 744 /tmp/toto.tmp"
    ssh -q -l user2 host1 "/tmp/toto.tmp"
    echo "INFO-> Done with toto.tmp" 
    ssh -q -l user2 "rm /tmp/toto.tmp"
    rm /tmp/toto.tmp
}

_toto=004

echo "Start"
start_
echo "Done"

Yes the script needs to do an su before executing another script... not clean i know but unfortunatly i cannot change this easily.

Thanks.
# 2  
Old 10-10-2011
If su asks you for a password, your script probably won't work, since the password will be read from the terminal and not merely stdin. If no terminal is present, it will just fail.

Why not just login as user1 in the first place? It may ask for a password, but only once.

You don't need to copy, chmod, execute, and remove a file either, just send the code:
Code:
ssh username@host /bin/sh -s param1 param2 <<"EOF"
line1
line2
line3
EOF

That way you don't need to actually store the file on the server.

Things between "EOF" and EOF won't be substituted locall, if you need to give arguments to the script you can do so with param1, which will become $1, param2, which will become $2,e tc.
# 3  
Old 10-11-2011
Keys are handle and su also. Everything works locally.

Your right about the direct commands but the part i have forgot to include is that i'm chaining ssh's through a proxy:

'ssh -t -q user@proxy -q -t ssh -l user ' host

Still trying to figure out the ProxyCommand with nc

Once i do that i should be able to use your example. Would i be able to use params?

Code:
ssh username@host /bin/sh -s value <<"EOF"
command -f
command -t 
command2 -v
EOF

Thanks.
# 4  
Old 10-13-2011
Quote:
Originally Posted by maverick72
Once i do that i should be able to use your example. Would i be able to use params?

Code:
ssh username@host /bin/sh -s value1 value2 "value3 stillvalue3" value4 <<"EOF"
command -f
command -t 
command2 -v
EOF

Thanks.
Yes. value1 would be in $1, value2 would be in $2, that whole quoted section would be in $3, value4 would be in $4, etc.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing multiple scripts using if condition

I have an if condition. If that condition is true then one script will be run and after that I need to check another condition based on the output value of first script. i tried like below : cd lock if ; then rm exitup if ; then kb_shutdown kb_startup if ; then rm exitup if ;... (3 Replies)
Discussion started by: charanarjun
3 Replies

2. Shell Programming and Scripting

Handling scripts in two different servers

Hi , My Script work as below 1- On server 1 execute script1.sh , through this script one parameter file is generated as file.txt this is to transfer on server 2 2- After reaching on server2 other shell script script2.sh execute using parameter file file.txt This generate file... (1 Reply)
Discussion started by: kaushik02018
1 Replies

3. Red Hat

Need automation script executing on multiple servers from one server

I have a tar file uploaded in a FTP server and it needs to be deployed in some 300 linux machines in a path like /opt/oracle/scripts and untar the file. it needs to be executed with one shell script by executing on all the the servers. this tar needs to deployed by doing pbrun su - user.... (4 Replies)
Discussion started by: appoo4
4 Replies

4. Shell Programming and Scripting

Problems with remotely executing scripts

Hi, in the below command, i export a value to a variable which later is used by the script, however i dont see the exported value is actually been exported. ssh user@host "export var=/path/ ; cd /path/ ; ./script" how can i use the above command with proper value of var remotley (7 Replies)
Discussion started by: suraj.sheikh
7 Replies

5. Shell Programming and Scripting

Executing all scripts in /DIR except one

First i need to find all scripts directly under /DIR that end with ".sh" extension except "noallow.sh". That can be done with: find /DIR -maxdepth 1 -name "*.sh"|grep -v "noallow.sh" Now i want to run all the files output from the previous command. The following code: for filename in... (6 Replies)
Discussion started by: proactiveaditya
6 Replies

6. Shell Programming and Scripting

Executing several bash scripts in succession

Hi, I am new to shell programming. I am trying to automate setting up a network using several scripts. Some of the scripts require to reboot in order to continue with the setup. Is it possible to enter another script as soon as the system reboots. Also, if the last line of the script is bash... (7 Replies)
Discussion started by: fantasyland
7 Replies

7. Shell Programming and Scripting

Executing certain commands on different servers from one server only

hi I wish to fire certain set of commands on different servers using single script on one of the server. The problem is that these servers only allow ssh session. telnet to these systems is blocked. Is there any way i can do this as rsh does not works. Regards Rochit (7 Replies)
Discussion started by: rochitsharma
7 Replies

8. Shell Programming and Scripting

Executing scripts in Parallel

Hi All, I have 3 shell scripts, Script1,Script2 and Script3. Now I want to run Script1 and Script2 in parallel and Script3 should depend on successful completion of both Script1 and Script2. Could you please suggest an approach of acheiving this... Thanks in advance (2 Replies)
Discussion started by: itsme_maverick
2 Replies

9. UNIX for Dummies Questions & Answers

Executing Shell Scripts

Hi, I'm pretty new to Unix and I just have a question concerning making a script executable without putting the "sh" command before it. In case it makes the difference I am on an Apple computer using the Terminal. Anyway here is the little test code I wrote followed by the commands I took to try... (1 Reply)
Discussion started by: BuyoCat
1 Replies

10. UNIX for Advanced & Expert Users

executing perl scripts

Does anybody experiencing this same problem? I am using IRIX64 ver 6.5 at work. I wrote some Perl scripts and to execute it. First I try to put the Perl script at: /$HOME/bin/perlscript then I set the correct executable 755 right to the file I make sure the PATH to the executable... (2 Replies)
Discussion started by: vtran4270
2 Replies
Login or Register to Ask a Question