11

I want to redirect the telnet console logs to a file in Linux.

For example:

telnet $someIp > someFile
#ls
#exit

I want the console logs to be saved in the filename someFile. I am using tcl for automating this. As of now, I am doing spawn telnet $someIp in tcl.

This will not capture the console logs and save it to a file.

Can this be done?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Saran
  • 246
  • 1
  • 5
  • 15

2 Answers2

26

You can do it by using the tee command:

telnet $someIp | tee -a -i someFile
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Laser
  • 6,652
  • 8
  • 54
  • 85
  • Thanks.It worked. But i could see some junk characters getting displayed like ^[[1;34m. Any idea how to remove this? – Saran Feb 03 '15 at 06:52
  • 2
    @Saran Those are just codes for rendering colors. Refer to http://unix.stackexchange.com/questions/58982/disable-colours-on-terminal-and-ssh on how to get rid of them – R.D. Oct 21 '16 at 18:35
  • 1
    Exactly what I needed - damn telnet in 2020 and those ISP router/modems who still use telnet and not SSH :) – stamster May 13 '20 at 16:16
  • this is useful, I'm going to backup memcached data – 高欣平 Mar 30 '23 at 02:38
3

Expect has a programmatic means to start and stop recording (or logginig). Given a file name argument, the log_file command opens the file and begins recording to it. If a log file is already open, the old file is closed first.

Recording is done by appending to the file, so if anything has previously been stored in the file, it remains. To start over, use the -noappend flag.

You can save space by turning off logging when it is not necessary. This is accomplished by calling log_file with no arguments. For example, the following fragment starts recording, does some I/O, stops recording, does some more I/O, and then starts recording again.

expect . . . ; send
# start recording
log_file telnetlog
expect . . . ; send
# stop recording
log_file
expect . . . ; send
# start recording
log_file telnetlog
expect . . . ; send

By default, log_file records only what the user sees. If the log_user command has been invoked to suppress output from a spawned program, the suppressed output is not recorded by log_file since the user is not seeing it either. The log_file can record the suppressed output by using the -a flag (for "all output").

log_file -a log

As before, this logging can be disabled by issuing log_file with no arguments. To return to logging just what the user sees, invoke log_file without the -a.

log_file -a log
expect . . . ; send . . .
log_file log

Reference : Exploring Expect

Dinesh
  • 16,014
  • 23
  • 80
  • 122