问题
I am trying to patch a bunch of CENT OS machines with the latest fix pack. I have the below bash script that takes csv file as a input which has the ip address and password for those machines.
The code works fine however, it would only work for the first row it does not seem to be working for the rest of the list as my output.txt only has the entry only for the first row host .
patch.sh
INPUT=hosts_test.cvs
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read privateip password
do
sshpass -p$password ssh -t -o "StrictHostKeyChecking no" user123@$privateip "
hostname
hostname -I --all-ip-addresses
sudo yum -y update bash
env x='() { :;}; echo vulnerable' bash -c \"echo If you see the word vulnerable above, then you are vulnerable to shellshock\"
echo ""
exit
" >> output.txt
done < $INPUT
IFS=$OLDIFS
hosts_test.cvs
10.xxx.xx.219,abcd~qY1
10.xxx.xx.226,l4~abcdefg
10.xxx.xx.221,l4@abcdefgh
Terminal Output
Pseudo-terminal will not be allocated because stdin is not a terminal.
回答1:
Add at the end of your sshpass command </dev/null
.
回答2:
- Add
Defaults:username !requiretty
to your/etc/sudoers
config - Get rid of
-t
from your ssh command - Optional, but recommended: set up public key auth so you don't have your passwords lying around in text files.
回答3:
You can pass ssh another -t
to force pty allocation:
ssh -t -t
来源:https://stackoverflow.com/questions/26103962/bash-script-does-not-ssh-all-the-entries-of-a-csv-file