Search code examples
bashtelnet

Variables in Telnet


I created a bash script which logs' into a server via telnet. Now I want to cat $var >> /path/on/server/a.log. $var get's specified earlier in the bash script before entering the telnet session. somehow the output "sh: ---------------------------: not found" is created in the telnet session.

I guess that happens because my file.log has multiple lines. the second line is "---------------".

here is my script:

#!/bin/bash
var=$(cat /path/to/local/file.log)
sleep 1
sh << EOF | telnet 192.168.178.72
sleep 2
echo user
sleep 1
echo pass
sleep 5
# echo "$var" >> /path/on/server/file.log (Can't Test because saving the var is not working)
sleep 5
EOF

my final goal is to get file.log onto the server. the only way to connect is via telnet. the client doesn't support ssh or rsync or anything.

edit: more information:

the script doesn'T HAVE to run on the client. I could run it on the server as well. i prefer running the script on the client tho.


Solution

  • I would recommend an approach such as this:

    #!/bin/bash
    (
    echo "telnet 192.168.178.72"
    sleep 2
    echo user
    sleep 1
    echo pass
    sleep 5
    echo "cat >> /path/on/server/file.log"
    sleep 5
    cat /path/to/local/file.log
    ) | sh
    

    Note that there is some potential for this to wind up executing the contents of /path/to/local/file.log on the remote server. I would strongly recommend that you consider the security implications of this and perhaps find some better way.