[lug] Telnet with here document

rm at fabula.de rm at fabula.de
Wed Apr 6 07:20:07 MDT 2005


On Wed, Apr 06, 2005 at 06:55:11AM -0600, Stephen Queen wrote:
> I've been trying to write a script that accesses a wireless
> access point/modem. The interface uses telnet. I thought a
> simple here document should do the trick. 

No way :-) Telnet doesn't read from standard in, it always tries
to read from the controlling terminal. This is a known nuissance,
fortunately eased by the existence of handy script libraries (for
Perl that would be Net::Telnet).
Of course, any serious old-time unixer will now suggest 'expect' [1]
writtn exactly do do what you want.

#--------x snip x-----------------------------------------------
#!/usr/bin/expect 

spawn telnet your.host.here
sleep 5

# wait for the ogin prompt
expect login

#send username
send username\r

#wait for password prompt
expect passw
send password\r

send command\r

#--------x snip x-----------------------------------------------

[1] man 'expect' will give a good intro and there should be a rather
good set of examples in the documentation folder of your distro.

 HTH Ralf Mattes


> The form of the



> here document I've tried is
> 
> ***********************************************
> #!/bin/bash
> 
> telnet <<ENDofDATA
> open XXX.XXX.XXX.XXX
> username
> password
> command
> command
> ...
> bye
> ENDofDATA
> 
> *************************************************
> 
> I've tried several variations of this, but telnet opens the
> connection, then appears to break the connection before it
> queries for the username. If I use the  form
> 
> open XXX.XXX.XXX.XXX -l username
> 
> it doesn't appear to query for a password before it breaks
> the connection. If I enter the commands from the command
> line, it works fine. If I replace the command telnet with
> cat, it performs as expected. I tried to google on telnet
> and here documents, and found an article that said it was
> possible, and showed how to do it with ftp, but had no
> telnet examples.
> 
> Has anyone here had any success with this?
> 
> Stephen Queen
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug



More information about the LUG mailing list