0
theIp=""
#1
echo $theIp | while read ip; do
    ssh -tt root@$ip
    exit
done
#2
while read ip; do
    ssh root@$ip
    exit
done < <(echo $theIp)
#3
while true; do
    ssh root@$theIp
    exit
done

the above 3 way about connect any host in a while statement, but only the last one succeeded, why do the first two do nothing?

1 Answer 1

2

ssh was eating up your loop's input. Probably in this case your ssh session exits when it gets EOF from it. That's the likely reason but some input may also cause it to exit. You have to redirect its input by specifying < /dev/null or use -n:

ssh -n "root@$ip"
ssh "root@$ip" < /dev/null

That may also apply with -tt since somehow it's independent. Just try.

If you're using Bash or similar shell that supports read -u, you can also specify a differ fd for your file.

while read -u 4 ip; do
    ssh root@$ip
    exit
done 4< <(echo $theIp)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot! use -n or redirect to /dev/null didn't help, but with file descriptor it works very well..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.