User Tools

Site Tools


telnet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

telnet [2019/12/24 10:28]
telnet [2021/05/21 00:04] (current)
Line 1: Line 1:
 +====== 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).
 +
 +<code bash>
 +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
 +}
 +</code>
 +
 +Of course if you do that, you'll want to occasionally (via cronjob?) delete old archives.
 +
 +<code>
 +find $HOME/telnetlog/ -type f -mtime +6 -delete
 +</code>
 +
 +===== Transfer a file by issuing a Remote Telnet Command =====
 +
 +Netcat (nc) send the file locally, and nc receive the file at the remote device.
 +
 +<code bash>
 +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
 +}
 +</code>
 +
 +===== Branching in Expect on whether a remote file exists =====
 +
 +<code expect>
 +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 ":/ #"
 +</code>
 +
 +Keywords: telnet, linux, nc, netcat