Topic
  • 3 replies
  • Latest Post - ‏2013-08-13T13:07:22Z by styerd
SystemAdmin
SystemAdmin
549 Posts

Pinned topic Telnet using script

‏2012-04-26T09:22:39Z |
hi,
i m trying to run the below script and store the output of this script in the text file.
#!/bin/sh
telnet 10.161.240.2 8100
<?xml version=\"1.0\"?> <login_command><sequence id=\"1\" /> <access id=\"1\"/> <user name = \"ttmlcnms\" password = \"cnms#123\"/> </login_command>
<?xml version=\"1.0\"?> <RegistrationDescriptor><sequence id=\"1\" /> <access id=\"1\"/> <attributeDescriptors><action>Register</action> </attributeDescriptors> </RegistrationDescriptor>

but error is coming as connection closed by foreign host.
Updated on 2012-04-26T09:26:48Z at 2012-04-26T09:26:48Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    549 Posts

    Re: Telnet using script

    ‏2012-04-26T09:26:48Z  
    i have run this script as
    sh myscript.sh >> mydata.txt
  • GarlandJoseph
    GarlandJoseph
    13 Posts

    Re: Telnet using script

    ‏2013-07-29T14:44:58Z  

    I don't think you can run telnet like that.  What type of system is the telnet server you're trying to connect? The traditional method of automating telnet sessions is to use a .netrc file or an expect script.  The connection is closed because after the telnet command.

  • styerd
    styerd
    14 Posts

    Re: Telnet using script

    ‏2013-08-13T13:07:22Z  

    I've used this silly method on occasion to keep the session open but as Joe says I think use "expect".  I've also tried "empty.sourceforge.net" which perhaps has a smaller foot print than "expect".

    mknod pipe p    # create a named pipe

    # sleep to keep the session open
    exec 1> trace_file
    ( sleep 300 > pipe;
      print -u2 "Exceeded time limit.\r;
      kill -9 $$" ) &
    PIPE_MASTER_PID=$!

    # telnet in the background
    telnet -e vt100 hostname < pipe 2> error_file &
    TELNET_PID=$!

    while :; do
      sleep 1
      egrep -q "login:" trace_file
      if [[ $? -eq 0 ]]; then
        print us > pipe
      fi
      egrep -q "password:" trace_file
      if [[ $? -eq 0 ]]; then
        print password > pipe
      fi

      # When you're done
      # kill -9 $PIPE_MASTER_PID
      # kill -9 $TELNET_PID
    done