LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-03-2006, 05:23 AM   #1
anil3
LQ Newbie
 
Registered: Mar 2006
Location: Norway
Distribution: Fedora
Posts: 14

Rep: Reputation: 0
Running Telnet in a script, want to save Telnet output, howtodothis???


Hello everybody.

I'm working in a company where we have lots of electronics we remotely control by telnet sessions.

I've made an shell script in Linux where I manage to run Telnet on a list of IP addresses, and thereby alter the configuration of the boxes. This is a very nice tool since I managed to do a complete reconfiguration of more than 700 boxes in one evening.

The problem is that I cannot grab the output from the telnet sessions and save to file. I need this feature to be able to query the result of the configuration.

If I do a redirection of the stdoutput to file by > or >> I only get the command saved.
This means that if I run the command "switch show port lan1" I get this:

Login: ---------

Password: *********



Login successful



--> switch show port lan1 (My command to query the status of port 1)

Switch Port information
--------------------------------------------------------------------------------
Port: lan1
Status Enabled
Link state Up
UpTime 018 days, 15:13:30
Port media type ISO8802-3 CSMACD
Configured speed/duplex Autonegotiate
Acceptable frame type packet sizes up to 1536 bytes (inclusive)
Broadcast limit Disabled
Multicast limit Disabled
Multicast/Broadcast rate lim -
Receive rate limit 6176 kbps
Transmit rate limit 6176 kbps
Current learned, lock state 0, not locked
Enabled flow control(s) Jamming
Pause

Send tagged pkts for VLAN(s) -
Port based VLAN vidar (1045)
Ingress filtering ON
802.1p Default Priority 0
802.1p Priority Disabled

--------------------------------------------------------------------------------


--> user logout


Logging out.

I get this on the screen, but I cannot save the output to file since I'm witin a Telnet session.

I managed to cature this screenshot by using PUTTY in Windows and logging it to file.

Is this possible in Linux within a script or is the another way to pick up the stdout stream.

I hope I managed to explain the challenge, and any reply will be highly appreciated.

Tnx in advance.
anil3
 
Old 03-03-2006, 06:24 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

I can think of two ways to do this.

1) Use the 'script' command. If you run 'script </path/to/file>' before runnning telnet, all text that gets written to the terminal also gets written to /path/to/file. You'll have to do 'exit' or Ctrl-D to actually write to the file.
Have a look at 'man script' to be enlightened.

2) Replace telnet with netcat. netcat outputs to standard out (and inputs from standard in), so you can redirect to a file in the normal way. This might be better suited to mass telnet type sessions anyway.
See 'man nc' for further joy.

Dave

P.S. How are you running telnet on several hosts at the same time?
 
Old 03-08-2006, 04:01 PM   #3
anil3
LQ Newbie
 
Registered: Mar 2006
Location: Norway
Distribution: Fedora
Posts: 14

Original Poster
Rep: Reputation: 0
Smile Hello, thanks for your reply to my problem..

I really appreciated it.

However I found out just after posting this that I had the solution already. I didn't test it properly.

The solution is very simple:

I use 2 scripts for this, the first is calling the second - the second is the one doing the Telnet.

The start of my first script goes like this (check the highlighted line):

something in the very beginning.........then

cat iplistetot_sort | grep -v '^#' | while read LINE
do
ADDR=`awk '{print $1}' `
for MASKIN in $ADDR
do
echo "Pinger $MASKIN" >> runlog_rg_query
ping -c3 $MASKIN
if [ "$?" -eq "0" ]
then
echo "Ping ok, kjører telenet på $MASKIN" >> runlog_rg_query
sh rg_query_1_2 $MASKIN | telnet > ipinfo/$MASKIN
ok=`expr $ok + 1`
egrep "Login failed" ipinfo/$MASKIN
if [ "$?" -eq "0" ]
then
echo "$MASKIN" >> restliste_rg_query
rm ipinfo/$MASKIN
ok=`expr $ok - 1`
error=`expr $error + 1`
fi
totalt=`expr $ok + $error`
echo -e "\n\n"

....some more stuff here.......

What I'm doing here is that I use a file holding all my IP adresses (iplistetot_sort) and then use a while loop to iterate all the IP adresses in that file. Every IP adress is used as an argument when calling script 2 (rg_query_1_2). I pipe this into a Telnet session and save every Telnet call as a separate file in a directory called ipinfo. When I'm finished with my list of IP adresses I have the same number of files in that directory as I had lines in my file. Every file contains the information I'm searching for the box on that Ip address. Now I can run other scripts to search all files for the exact information I'm looking for.

Again - thanks for taking time to help me.

Below is the second script:

#!/bin/sh
host=$1
port=23
login="********\r\n" (I'm not telling you )
passwd="********\r\n" (I'm not telling you)
logout="user logout \r\n"#!/bin/sh
host=$1
port=23
logout="user logout \r\n"
cmd="switch show port lan1\r\n"
send="switch set port lan1 trslimit 4200\n\r"
receive="switch set port lan1 rcvlimit 4200\n\r"
mcfg="system config create boot.cfg\r\n"
scfg="system config set boot.cfg\r\n"

echo open ${host} ${port}
sleep 5

echo -e ${login}
sleep 2

echo -e ${passwd}
sleep 2

echo -e ${cmd}
sleep 3

echo -e ${receive}
sleep 3

echo -e ${send}
sleep 3

echo -e ${mcfg}
sleep 15

echo -e ${scfg}
sleep 2

echo -e ${cmd}
sleep 3

echo -e ${logout}
sleep 2
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Telnet script pirra Programming 1 12-29-2004 03:04 PM
How do I setup telnet on Fedorac1 so I can telnet to it from winxp? mman49 Fedora 6 05-02-2004 12:40 PM
can not telnet localhost 25 but telnet dowell.exper.dynserv.com 25 exper Linux - Software 0 02-25-2004 05:13 AM
Pipe telnet session output to text file joshlamerritt Linux - Software 3 02-10-2004 08:42 PM
Output vanishes using TELNET or SSH pcardout Linux - General 2 06-25-2003 10:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration