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

#!/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.

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:

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.

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?

ssh username@host /bin/sh -s value <<"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.

1 Like