Reputation: 3
Hope this time it's not a duplicate. I didn't find anything.
My code:
#!/bin/bash
FILE=/home/user/srv.txt
TICKET=task
while read LINE; do
ssh -nT $LINE << 'EOF'
touch info.txt
hostname >> info.txt
ifconfig | grep inet | awk '$3 ~ "cast" {print $2}' >> info.txt
grep -i ^server /etc/zabbix/zabbix_agentd.conf >> info.txt
echo "- Done -" >> info.txt
EOF
ssh -nT $LINE "cat info.txt" >> $TICKET.txt
done < $FILE #End
My issue:
ssh $LINE
it will only ssh to the host on the first line and also display an error Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh -T
, fix the error message above and it will create the file info.txtssh -nT
, fix the error where ssh only read the first line but I get an error message cat: info.txt: No such file or directory
. If I ssh to the hosts, I can confirm that there is no info.txt file in my home folder. and with ssh -T
, I have this file in my home folder.I tried with the option -t, also HERE, EOF without ' ... ' but no luck
Do I miss something? Thanks for your help, Iswaren
Upvotes: 0
Views: 392
Reputation: 16817
You have two problems.
-n
it may consume the $FILE input (it drains its stdin)-n
it won't read its stdin, so none of the commands will be executedHowever, the first ssh has had its input redirected to come from a heredoc, so it does not need -n
.
As stated in the comments, the second ssh call is not needed. Rather than piping into info.txt and then copying that into a local file, just output to the local file directly:
while read LINE; do
ssh -T $LINE >>$TICKET.txt <<'EOF'
hostname
ifconfig | grep inet | awk '$3 ~ "cast" {print $2}'
grep -i ^server /etc/zabbix/zabbix_agentd.conf
echo "- Done -"
EOF
done <$FILE
Upvotes: 1