Link to home
Start Free TrialLog in
Avatar of matjc
matjc

asked on

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?
SOLUTION
Avatar of tfewster
tfewster
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of matjc
matjc

ASKER

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?
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of matjc

ASKER

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.
Avatar of matjc

ASKER

Maybe I should have pointed out that I am running this script from the console not another telnet session?
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;
Avatar of matjc

ASKER

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"

hmm if not feel comphy with expect, how about using perl's Net::Telnet, or even IO::Socket
SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of matjc

ASKER

#! /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
%%%%%%%%%%
why adding "into" bash when it works without that overhead?
Avatar of matjc

ASKER

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?
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
Avatar of matjc

ASKER

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.
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.
Avatar of matjc

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial