Iswaren
Iswaren

Reputation: 3

Loop EOF ssh -n can't create file

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:

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

Answers (1)

jhnc
jhnc

Reputation: 16817

You have two problems.

  • If you invoke ssh without -n it may consume the $FILE input (it drains its stdin)
  • If you invoke ssh with -n it won't read its stdin, so none of the commands will be executed

However, 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

Related Questions