How to send input to telnet

I have the following script:

#!/bin/bash
telnet console 7020 <telnet.in

telnet.in contains the following:

^] send brk

boot net -v - install

^] quit

However this isn't quite working as im expecting. console is a console server.

I want to telnet to a port on this (serial port on a solaris machine) and send a break and then boot net -v - install so the machine reinstalls.

Ideas?
matjcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tfewsterCommented:
Fooling a telnet client to accept input from a script (Does not work on all telnet clients!)!
http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_10126049.html
http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_20371033.html

Using "expect" (which is reliable and ideal for this sort of task):
http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_10241151.html


However, Ctrl-] is usually the interrupt character for the telnet client, to the break won't get sent to the system; Check your man page for `telnet` - The Solaris telnet client allows this escape character to be redefined, e.g.
`telnet -e ^B`
0
matjcAuthor Commented:
ok getting there:

#!/bin/bash
set -x
telnet -e x console 7020 << %%
x send break
boot disk
%%

If I do this I see:

bash-2.05# ./installfarm.sh
+ telnet -e x console 7020
Telnet escape character is 'x'.
Trying 192.168.1.44...
Connected to console.testnet.
Escape character is 'x'.

telnet>  send break
Connection to console.testnet closed by foreign host.

However this does not make a difference and it appears that nothing happens, ie the machine doesn't boot from disk.

If I do this manually I see:

bash-2.05# telnet -e x console 7020
Telnet escape character is 'x'.
Trying 192.168.1.44...
Connected to console.testnet.
Escape character is 'x'.

telnet> send break
Type  'go' to resume
ok boot disk

etc etc ..

It appears that the 'send break' for some reason causes the bash script to exit straight out?
0
ahoffmannCommented:
most telnet do not accept any input from stdin (as already stated), for obvious reasons.
You have to use expect or another program instead of telnet.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

tfewsterCommented:
My point about redefining the telnet escape character was that you _don't_ want to interrrupt the telnet session - You want the Solaris break character to be passed to the server. So I'd expect the script to look something like:

#!/bin/bash
set -x
telnet -e ^x console 7020 << %%    # Make the telnet break Ctrl-X just in case you use an "x" in the script
^]           # Note, control characters are inserted in a script by typing Ctrl-V Ctrl-<Char>
boot net -v - install
%%

0
matjcAuthor Commented:
tfewster,

Apologies if the following is too simple, but the following might help understanding of the problem:

A Sunfire V120 has something called a LOM port, however this is serial only, and is connected to a Console port. However this networked console server allows me to send breaks to the Sun Machine which should (and does) give me an OK? Prompt.

I telnet from Workstation -> Serial Port on Server (Via Console Server)

I then WANT to interrupt my telnet session to the serial port to send a break:

hence you see the following (## are my comments about what is happenning):

bash-2.05# telnet -e x console 7020
##Me telnetting to the serial port.
Telnet escape character is 'x'.
Trying 192.168.1.44...
Connected to console.testnet.
Escape character is 'x'.
## I pressed x to break my telnet session from workstation to serial port.
telnet> send break
## I send the break to the serial port and goet an OK? prompt:
Type  'go' to resume
ok boot net -v - install

I can then send the boot command to the server.

This is what I want todo, I will investigate expect this morning and see if that helps at all.

Cheers.
0
matjcAuthor Commented:
Maybe I should have pointed out that I am running this script from the console not another telnet session?
0
tfewsterCommented:
I think I see - You're using telnet command mode to send the "break" sequence to the Console (which relays it to the Solaris box).  I'll test this later, but an extra <CR> in your script between the "send break" and "boot" lines should (hopefully) resume the telnet connection so you're communicating with the Console server again.  Otherwise, after entering command mode, "data" strings won't be sent to the Console server.

My suggestion was trying to avoid suspending the telnet session and sending the Console break directly;
0
matjcAuthor Commented:
Arh I see, you have to susepend the session, just sending the break even manually while connected through telnet does not work you have to break the telnet session and then type "send break" or "send brk"

0
ahoffmannCommented:
hmm if not feel comphy with expect, how about using perl's Net::Telnet, or even IO::Socket
0
yuzhCommented:
"expect" script has problem with  handling special chars!

you can try:

#!/bin/sh
{
   echo "Ctrl-] "  # you need to type in "Ctrl-] " Esc code;
  sleep 3           # wail 3 seconds
  echo "go"
  sleep 2
  echo "boot net -v - install"
} | telnet console 7020

#or telnet -e x console 7020  
exit
 
0
matjcAuthor Commented:
#! /usr/local/bin/expect
spawn telnet -e x console 7012
send "\r"
expect "netservices console login:"
send "x"
expect "telnet>"
send "send break"
send "\r"
expect "ok"
send "boot net -v - install"
exit

Appears to work as I would expect, How would I then add this into a bash script eg:

Is there a better way of doing this?

#!/bin/bash

server=server1
serverport=7012

/usr/local/bin/expect < %%%%%%%%%%
spawn telnet -e x console $serverport
send "\r"
expect "$server console login:"
send "x"
expect "telnet>"
send "send break"
send "\r"
expect "ok"
send "boot net -v - install"
exit
%%%%%%%%%%
0
ahoffmannCommented:
why adding "into" bash when it works without that overhead?
0
matjcAuthor Commented:
because if I have 20 servers that I want to run this on how else but to read in all 20 from a file and the loop round a while loop?
0
ahoffmannCommented:
how about using tcl's argument list?

set server1 [lindex $argv 1]
set port1    [lindex $argv 2]
...

you even can open the file in tcl itself and then read the lines
0
matjcAuthor Commented:
ahoffman, I do not know tcl, and although probably similar to other scripting langages it has the one LARGE disadvantage that it is not installed by default on many machines, I would pressume this possible with bash and therefor unless necessary would like to stick with it.
0
ahoffmannCommented:
if you have expect, then you have tcl to, 'cause expect is based on tcl
well, tcl syntax is strang (for most people), but if get used to it it's straight forward.
Using a shell to generate a tcl script is tricky, 'cause you need a couple of special chars, which needs sophisticated shell usage. Best is to have a template file and then substitute what you want.
0
matjcAuthor Commented:
fair enough regarding tcl, however  all I need is to loop round a list of 20 machines running the expect script above. the machien names are sequential as are the ports eg:

machine 1 7001
machine 2 7002
machien 3 7003

etc etc
0
ahoffmannCommented:
use following loop arround your expect script (right after the hashbang line):

while {[llength $argv] > 0} {
   set server [lindex $argv 0]
   set port   [lindex $argv 1]
   set argv   [lreplace $argv 0 1]
   # your other code here
}

then assuming your file with hostnames and ports, call the expect script as follows:

expect.tcl `cat file-with-hostname_and_port-as-2-words-per-line`

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.