Solved

How to send input to telnet

Posted on 2004-10-23
19,492 Views
Last Modified: 2013-12-26
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?
0
Question by:matjc
    18 Comments
     
    LVL 20

    Assisted Solution

    by:tfewster
    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
     

    Author Comment

    by:matjc
    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
     
    LVL 51

    Expert Comment

    by:ahoffmann
    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
     
    LVL 20

    Assisted Solution

    by:tfewster
    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
     

    Author Comment

    by:matjc
    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
     

    Author Comment

    by:matjc
    Maybe I should have pointed out that I am running this script from the console not another telnet session?
    0
     
    LVL 20

    Expert Comment

    by:tfewster
    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
     

    Author Comment

    by:matjc
    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
     
    LVL 51

    Expert Comment

    by:ahoffmann
    hmm if not feel comphy with expect, how about using perl's Net::Telnet, or even IO::Socket
    0
     
    LVL 38

    Assisted Solution

    by:yuzh
    "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
     

    Author Comment

    by:matjc
    #! /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
     
    LVL 51

    Expert Comment

    by:ahoffmann
    why adding "into" bash when it works without that overhead?
    0
     

    Author Comment

    by:matjc
    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
     
    LVL 51

    Expert Comment

    by:ahoffmann
    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
     

    Author Comment

    by:matjc
    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
     
    LVL 51

    Expert Comment

    by:ahoffmann
    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
     

    Author Comment

    by:matjc
    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
     
    LVL 51

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now