Read over netcat returning unexpected things, including ls of the directory it's running in

92 Views Asked by At

I'm making a simple irc bot with netcat. The first part is:

#! /bin/bash
# nc -c ./connect.sh irc.rizon.net 6667
tee /dev/stderr | {
        sleep 5
        echo "USER testbot * * :test"
        echo "NICK testbot"
        read line ; echo $line | sed  -e "s/^PING/PONG/"
        sleep 3
        echo "JOIN #channel"
} | tee /dev/stderr

Running this on a nc instance connecting to my own nc server works exactly as expected, however connecting to irc.rizon.net gives:

~/programs/testbot# nc -c ./connect.sh irc.rizon.net 6667
:irc.mufff.in NOTICE * :*** Looking up your hostname...
:irc.mufff.in NOTICE * :*** Checking Ident
:irc.mufff.in NOTICE * :*** Couldn't look up your hostname
:irc.mufff.in NOTICE * :*** No Ident response
USER testbot * * :test
NICK testbot
:irc.mufff.in NOTICE connect.sh file1.txt file2.png file3.py :*** Looking up your hostname...
PING :29785872
:irc.mufff.in 451 testbot :You have not registered
JOIN #channel
:irc.mufff.in 451 testbot :You have not registered

...where connect.sh file1.txt file2.png file3.py is the list of files in the directory I ran the commmand in.

1

There are 1 best solutions below

4
is_trout On

There were two issues. One, I thought read read from the last stdin entry, but it starts at the beginning, meaning that the $line was actually :irc.mufff.in NOTICE * :*** Looking up your hostname..., not PING :29785872.

As pointed out in a comment there were no quotation marks around my variable meaning that bash interpreted stuff in it. I don't know exactly how that lead to listing directory contents but that was the issue. The new code goes:

#! /bin/bash
# nc -c ./connect.sh irc.rizon.net 6667
tee /dev/stderr | {
        sleep 5
        echo "USER testbot * * :test"
        echo "NICK testbot"
        sleep 3
        echo "JOIN #channel"


        while read line; do
                case "$line" in
                        PING*)
                        echo "$line" | sed  -e "s/^PING/PONG/"
                        ;;
                esac
        done&
} | tee /dev/stderr

Which works