I need run a command such as ps -ef |grep xxx on a batch of servers, how to write a script to run it without password? don't need go in each server to check?
Thanks
I need run a command such as ps -ef |grep xxx on a batch of servers, how to write a script to run it without password? don't need go in each server to check?
Thanks
Use a PPK Key with ssh -- password-free access to all hosts. The tool 'parallel' can manage the list.
To run remote programs without a password, presumably over ssh, you will need to arrange passwordless logins first.
I usually get it running for 'ssh localhost pwd' first, then copy the entire .ssh* directory to target hosts with 'scp -r'. The localhost entry will be wrong everywhere else, of course.
First thing is to setup rlogin or ssh. I prefer to use ssh.
This is a little script to get you started.
#!/bin/ksh
HOSTLIST="<LIST OF HOST>"
DELAY=1
print -n Enter Command to process on remote Host -
read CMD
print
exec 4>&1
for HOST in $HOSTLIST ; do
echo "running ${CMD} on ${HOST}:"
ssh -t -t $HOST >&4 2>/dev/null |&
sleep $DELAY
print -p /usr/bin/nohup ${CMD}
sleep $DELAY
print -p exit
wait
echo "${CMD} completed on ${HOST} !"
done
exit 0
techy1: Why not just ssh username@host /usr/bin/nohup $CMD &
? That way you don't need to print into it using coprocess facilities...
that would work. I'm not all that great at writing scripts. just know enough to get by =)
You can pile up the host commands in a single line and run them parallel but get as if serial output:
cat <(
host1 stuff >/tmp/xxxx-host1.log 2>&1 &
export p1=$!
( sleep <delay> ; kill -9 $p1 ) & # ensure p1 does not hang
p2=$!
wait $p1
kill -9 $p2 # kill the p1 watchdog
cat /tmp/xxxx-host1.log
) <( ... host2 ... ) <( ... host3 ... ) <( ... host4 ... ) <( ... host5 ... ) . . . .
Or learn to use parallel with ssh and host lists.