How to save the output of your telnet session
There's a topic always asked about on forums and the answers are typically wrong, unuseful, doesnt work, etc.. I had the same problem, I figured it out so I thought about posting it here. How to save the infamous telnet output automatically to a file on your pc.
Install expect by doing:
sudo apt-get install expect
then, do an expect script of your telnet session, the way you want it. (internet is full of articles about expect scripting, and believe me it's real easy!)
then in terminal launch the script using expect and use the program "tee" to redirect the output to a file on ur pc.
we have an expect script called fetch.
in terminal we do:
expect fetch | tee example.txt
this command will launch connect to telnet, automatically do the commands u put in the fetch script, and send all the output till the end to a file in ur current directory named example.txt
if the operation is a routine (fetching data for example regularly) u can add the command to cron to schedule it to run anytime u want automatically and save u the output data.
to do that enter crontab -e in terminal and add the job there, or alternatively do:
sudo apt-get install gnome-schedule
and edit cron graphically, but needless to say, the terminal gives u a wider set of control and options on cron.
Install the “script” command. Then first run script from a terminal, followed by the telnet command. When your telnet session is over press ^D, and — voila! — input and output from your telnet session can be found in the file “typescript” in the current directory. No need for expect. 🙂
well, that’s partially true. I tried script and it has the following problems:
1- script cuts the output saving if it was too large
2- script saves the output spaced by a line break between the lines, so it’s not the same as the original output (that’s a prob to me)
and waiting there to stop script from doing further recording isnt that nice of an idea to me..
(1) I didn’t know that script cuts the output file, I guess the output from my sessions have never been that big…
(2) The lines are separated by CRLF (ASCII 13 + ASCII 10 = ‘\r\n’), which might actually be what telnet returns. Remove the ‘\r’, and you have what you expect.
But of course, if you prefer expect there’s no need to use script. 🙂
Use this one, it is easy.
#telnet [IP] > telnetlog | tail -f telnetlog
After you logout from the telnet session and kill tail, type “more telnetlog”.