Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-01-2007, 10:21 AM   #1
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.

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

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
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, 10:51 AM   #3
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



( 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, 10:55 AM   #4
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, 01:00 AM   #5
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 242Reputation: 242Reputation: 242
See here for example
Old 08-02-2007, 12:13 PM   #6
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 682

Rep: Reputation: 409Reputation: 409Reputation: 409Reputation: 409Reputation: 409
This is very popular question, I see

You can use following bash script as an example: 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:
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:
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 12:15 PM.


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 08:41 AM
Automate FTP session using Shell Script kalyanofb Programming 2 04-07-2007 08:44 AM
how to automate telnet login, script or something kiwibird Linux - Networking 2 11-09-2005 02:02 PM
telnet login via shell script lethe Linux - General 8 05-13-2002 09:31 AM

All times are GMT -5. The time now is 01:48 AM.

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