LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-01-2007, 11:21 AM   #1
daggulasreedhar
LQ Newbie
 
Registered: Mar 2007
Posts: 5

Rep: Reputation: 0
shell script to automate TELNET


hi dudes,
Can anyone suggest me how to write a script for the automation of TELNET using shelll script.
I will appriciate if i got a sample code.


Thanks
Sreedhar Reddy
 
Old 08-01-2007, 11:49 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I recommend using C-Kermit, instead. It has a nice scripting language built in. The distribution comes with some good examples, as I recall. Most versions of telnet are not very scriptable, and require the use of something like 'expect'.
--- rod.
 
Old 08-01-2007, 11:51 AM   #3
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 96

Rep: Reputation: 17
This script telnets to my router, and restarts it by using commands "24", "4" and "21".
Just change the commands (cmd[123]) to whatever you need...).

An alternative is to use expect (google it) if you need to wait for a particular response. This script is not very dynamic (it can only execute a command, wait for x seconds then execute the next command), but expect can wait for a specific word/sentence (i.e. "username:" or "password:") then respond with the right command/code. Unfortunately I don't have an example of expect around, but I bet someone else has one

Code:
#!/bin/sh

host=10.0.0.1
port=23
pass=xxxx
cmd1=24
cmd2=4
cmd3=21

( echo open ${host} ${port}
  sleep 1
  echo -e "\r"
  sleep 1
  echo ${pass}
  sleep 1
  echo -e "\r"
  sleep 1
  echo ${cmd1}
  sleep 1
  echo -e "\r"
  sleep 1
  echo ${cmd2}
  sleep 1
  echo -e "\r"
  sleep 1
  echo ${cmd3}
  sleep 1
  echo -e "\r"
  sleep 1
  echo exit ) | telnet
 
Old 08-01-2007, 11:55 AM   #4
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
You should probably clarify what it is you are trying to do.

As an aside, I hope you realize that telnet is an insecure protocol which should opnly be used on a secure network (i.e. definitely not the Internet). For this reason telnet has been largely replaced with more secure protocols suchs as ssh.
 
Old 08-02-2007, 02:00 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
See here for example
 
Old 08-02-2007, 01:13 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi!
This is very popular question, I see

You can use following bash script as an example:
http://tiocu.svn.sourceforge.net/vie...il?revision=58. Function `wait4ack()' and lines below `echo connecting...' are of interest for you.

Shortly speaking we run telnet process in background and associate file descriptors 3 and 4 with its stdin and stdout using something like this:
Code:
mkfifo $FIFO
exec 4< <(telnet $SERVER 25  < $FIFO 2> /dev/null &)
exec 3>$FIFO
Now we can send commands to telnet through 3'rd fd and recieve responses through 4'th one:
Code:
echo $comm        >&3
wait4ack "$optn"  <&4
wait4ack() reads its stdin (i.e. 4'th file descriptor) until something like '+OK' in POP3 protocol or 250 in SMTP (see corresponding RFC document).

Hope, this helps.

firstfire == agre.

By the way, `telnet-ssl' can solve some security problems.

Last edited by firstfire; 08-02-2007 at 01:15 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to program shell script to automate mass user account creation? EsAsher Linux - General 2 06-30-2007 09:41 AM
Automate FTP session using Shell Script kalyanofb Programming 2 04-07-2007 09:44 AM
how to automate telnet login, script or something kiwibird Linux - Networking 2 11-09-2005 03:02 PM
telnet login via shell script lethe Linux - General 8 05-13-2002 10:31 AM


All times are GMT -5. The time now is 04:50 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration