Table of Contents

Telnet Tips

Tee Telnet Output to a Unique File

Note the use of $@ vs “$*” in the next function that automatically saves an archive of a telnet session. Also note that I remove spaces and colons. (Colons because they screw with opening files directly at line numbers).

telnet_log() {
    curtime=$(date -Iseconds | tr : .)
    args=$(echo "$*" | tr ' ' '_')
    telnet $@ | tee $HOME/telnetlog/$args\_${curtime::-5}.log
}
 
last_telnet_log() {
    ls -d1t $HOME/telnetlog/* | head -n 1
}

Of course if you do that, you'll want to occasionally (via cronjob?) delete old archives.

find $HOME/telnetlog/ -type f -mtime +6 -delete

Transfer a file by issuing a Remote Telnet Command

Netcat (nc) send the file locally, and nc receive the file at the remote device.

function flash-roku
{
    target=$1
    filename=$2
    port="8081"
    nc -w 5 $target $port < $filename &
    { echo \
        "cd /tmp && nc -l -p $port > tempfile.txt && " \
        "tail tempfile.txt"; \
      sleep 10; } | telnet $target
}

Branching in Expect on whether a remote file exists

send "ls --color=never $FNAME\r"
expect {
     -re "\n$FNAME" {
        expect ":/ #"
        send "stat $FNAME\r"
        expect ":/ #"
        send "echo $FNAME was already there.\r"
     }
     "No such file or directory" {
        expect ":/ #"
        send "ls ~\r"
        expect ":/ #"
        send "echo $FNAME was not there.\r"
     }
}
expect ":/ #"

Keywords: telnet, linux, nc, netcat