How to Redirect the Telnet Console Logs to a File Linux

How to redirect the telnet console logs to a file Linux

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

pipe telnet output to logfile with tee command using Java process exec

Yes, exec sounds like a better solution since you want to control both input and outpud data. And do you really need tee in when you use exec, I don't understand why...

Also take a look at netcat instead of telnet - telnet has some special character handling that could give you trouble if you plan to send binary data.

how to feed a file to telnet

telnet is not the right tool for this job. Try :

 netcat localhost 80 < telnet.txt

btw, if you don't have the tool installed on your machine, you
can get it here:

http://netcat.sourceforge.net/

Automating telnet session using bash scripts

Write an expect script.

Here is an example:

#!/usr/bin/expect

#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
#This spawns the telnet program and connects it to the variable name
spawn telnet $name
#The script expects login
expect "login:"
#The script sends the user variable
send "$user "
#The script expects Password
expect "Password:"
#The script sends the password variable
send "$password "
#This hands control of the keyboard over to you (Nice expect feature!)
interact

To run:

./myscript.expect name user password

How to capture the output of telnet command in a variable in Shell script

Try this :

telnet_output="$({ sleep 1; echo $'\e'; } | telnet $server $port 2>&1)"
printf "Output is\n%s\n" "$telnet_output"

echo $'\e' sends an escape character to telnet to terminate it.

Spawning an interactive telnet session from a shell script

You need to redirect the Terminal input to the telnet process. This should be /dev/tty. So your script will look something like:

#!/bin/bash

for HOST in `cat`
do
echo Connecting to $HOST...
telnet $HOST </dev/tty
done

Linux terminal output redirection

Redirect STDERR to STDOUT:

script.sh >> temp.txt  2>&1

Or if using bash 4.0:

$ script.sh &>> temp.txt

(Thanks for the second form go to commenter ephemient. I can't verify as I have an earlier bash.)


My tests were surprising:

$ time sleep 1 > /dev/null 2>&1

real 0m1.036s
user 0m0.002s
sys 0m0.032s

The problem is the redirection was included as part of the command to be timed. Here was the solution for this test:

$ (time sleep 1) > /dev/null 2>&1

I don't think this is part of your problem, but it seemed worth a mention.



Related Topics



Leave a reply



Submit