Running multiple saf-ssh-agent commands from one STDIN

Discussion of the COZBATCH utility for z/OS
Post Reply
gngrossi
Posts: 36
Joined: Sat Mar 06, 2010 6:10 pm

Running multiple saf-ssh-agent commands from one STDIN

Post by gngrossi »

I am looking to run multiple remote commands from one COZBATCH step.
In the first example, the job step ends after processing one saf-ssh-agent command. Is there additional setup I need? Thanks.

This doesn't work
saf-ssh-agent -c KEYRING ssh user@$i "cmd"
saf-ssh-agent -c KEYRING ssh user@$i "cmd"

This works
for i in server1 server2
do
echo $i
saf-ssh-agent -c KEYRING ssh user@$i "cmd"
done
gngrossi
Posts: 36
Joined: Sat Mar 06, 2010 6:10 pm

Post by gngrossi »

This also works using one STDIN:

//STDIN DD *
echo Test 1
cat << EOF | saf-ssh-agent -c KEYRING ssh user@server "bash -ls"
hostname;uname -a
ps -ef | grep abc
sar
EOF
echo Test 2
cat << EOF | saf-ssh-agent -c KEYRING ssh user@server "bash -ls"
hostname;uname -a
ps -ef | grep abc
sar
EOF
coz
Posts: 391
Joined: Fri Jul 30, 2004 5:29 pm

Post by coz »

COZBATCH spawns a child process to run /bin/sh and pipes it the script to be executed from //STDIN.

When you run a command that attempts to read from stdin/fd0 (ssh in your case), the pipe gets clipped and the streaming of the script stops.

To avoid this, you can either have COZBATCH get its script from a file rather than using STDIN, or just make sure that the commands you run don't try to read from stdin. The following change to your original example should fix the problem.

Code: Select all

saf-ssh-agent -c KEYRING ssh user@$i "cmd" < /dev/null
saf-ssh-agent -c KEYRING ssh user@$i "cmd" < /dev/null
gngrossi
Posts: 36
Joined: Sat Mar 06, 2010 6:10 pm

Post by gngrossi »

Can you explain why this works?

for i in server1 server2
do
echo $i
saf-ssh-agent -c KEYRING ssh user@$i "cmd"
done

Should I add < /dev/null at the end of the saf-ssh-agent?
coz
Posts: 391
Joined: Fri Jul 30, 2004 5:29 pm

Post by coz »

Yes I can :)

The shell has to read the entire for construct before issuing any of the embedded commands. By the time the first ssh command is run, the loop is set up. If you tried to put anything after the loop, it would fail to run.

If you put the < /dev/null stdin redirect on the ssh command, it prevents him from competing for the stdin stream. You should put it on even inside the loop.
gngrossi
Posts: 36
Joined: Sat Mar 06, 2010 6:10 pm

Post by gngrossi »

After running several tests with STDIN as well as invoking a script from STDIN, it all works as documented.
Thanks!
Post Reply