LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 10: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, 10:49 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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
TylerD75
Member
 
Registered: Aug 2004
Location: Norway
Distribution: Gentoo
Posts: 96

Rep: Reputation: 18
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
 
1 members found this post helpful.
Old 08-01-2007, 10: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, 01:00 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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 12:15 PM.
 
  


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
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 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