In order for sudo
to be able to prompt for a password you need to give it a tty device (a pseudo-tty). ssh
will usually create the pseudo-tty device on the remote end if its input is also from a terminal. However, in this case it isn't - you're using a heredoc (the <<
bit).
To copy your style, you need to add the RequestTTY
option to ssh
, also available implemented as -t
. Confusingly you have explicitly disabled pseudo-tty allocation with your -T
flag, so you need to replace that:
TestHost1="host01 host02 host03"
for host in $TestHost1
do sshpass -p password ssh -t -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no username@$host << EOL;
sudo su -
sed -i 's/10./172./g' /etc/hosts
EOL
done
However, you might want to review your replacement match 10.
; as it stands that matches the two characters 10
plus any one character (the .
is a wildcard), multiple times per line. Perhaps it should be 10\.
(match three literal characters) or even ^10\.
(match three literal characters at the start of the line). Here I've assumed you meant three literal characters 10.
at the start of the line:
TestHost1="host01 host02 host03"
for host in $TestHost1
do sshpass -p password ssh -t -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no username@$host sudo sed -i.bak 's/^10\./172./' /etc/hosts
done