none
how to send command to remote telnet srvr?

    Question

  • I want to script access to a remote telnet server.
    I've made a script to send te data, but it's not working.

    I've used wireshark to search for a reason, because sending the same commands manual in telnet works like a charm. The only difference i've found is that telnet issues a command "will terminal type"

    I want to send the same command, but i can only send the commands as data, not as a command.

    I used this reference about telnet, http://support.microsoft.com/kb/231866/en-us
    It says that:

    These commands are transmitted within the data stream. The commands are distinguished from the data by setting the most significant bit to 1. (Remember that data is transmitted as 7-bits with the eighth bit set to 0) Commands are always introduced by the Interpret as command (IAC) character.

    I just can't figure out how to do this

    Here are some examples of doing it the wrong way :-)

        $socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
        write-host "Open stream"
        Start-Sleep -m 500
        $stream = $socket.GetStream()
        Start-Sleep -m 500
        $writer = new-object System.IO.StreamWriter($stream)
        Start-Sleep -m 500
     
    # Create a buffer to receive the response
        $buffer = new-object System.Byte[] 1024
        $encoding = new-object System.Text.AsciiEncoding

    # Receive the output that has buffered so far
        $SCRIPT:output += GetOutput

    # Set terminal mode to ANSI
    #    $writer.WriteLine("IAC WILL Terminal Type")
        $writer.WriteLine("255")
        $writer.Writeline("251")
        $Writer.WriteLine("24")
        $writer.Flush()
        $SCRIPT:output += GetOutput
        $writer.WriteLine("IAC SB Terminal Type 1 IAC SE")
        $writer.Flush()

     

    I'm new to powershell, so it might be completely wrong ;-)

    Any help appriciated

     

     

     

     

    Monday, January 17, 2011 2:34 PM

All replies

  • I've looked in to this as well and what I learned is this.... unless you are willing to write a full telnet client (look for C# code) then its not really worth it. come to find out telnet is very odd and picky and just sending commands via TCP just doesn’t work out right.

    I searched on this for a while and there is just no way to do a simple telnet client.

    I ended up wrapping a telnet scripting tool in PS
    http://jerrymannel.com/blog/2008/11/11/telnet-scripting-tool-aka-tst10exe/

    I know there are some pay PS libraries that include telnet but I have yet to find any free ones.

    Monday, January 17, 2011 2:40 PM
  • thanx for the advise...

    I only need to send some simple commands, seems like it should be no big deal. If someone knows how to program this in .net it would be fine either. Powershell is around for so long. At least someone must have figured out how to do this?? If someone has .net code I'll figure out how to make my own cmdlet. I planned to do this in the near future anyway.

    Anyone please?

     

    Monday, January 17, 2011 3:59 PM
  • if you search for C# telnet client you can find a few. there were a little longer than I felt like converting and it seemed like they only worked on certain telnet servers.

    Monday, January 17, 2011 4:10 PM
  • I'll Look into that.

    Unfortunatly it's not as straight forward as I had hoped. Nice start for my first PowerShell project
    I'm afraid it will become a "spare time project" :-)

    Might take a little longer, but I'm determent to figure this one out..

    So any advise is still welcome.

    Tuesday, January 18, 2011 7:51 AM
  • well if you get it, then please share it.

    I would say, if you are going to pursue this then I'd focus on one type of telnet server (sounds like you are looking to do an MS telnet server?)

    when I needed to do it I had to do a few flavors of *nix and windows so I wasn’t about to figure them all out.

    on the plus side, I believe that the MS telnet wasn’t as picky as some of the others.

    I might suggest getting a c# one working first and then port it to PS. easier to figure it out id think plus there is plenty of code on the net to start with. the trick might be some of the class that may need to be used not being friendly with PS.

    keep us updated for sure though!

    Tuesday, January 18, 2011 1:20 PM
  • I use a script as part of my boot-up process to open a telnet session, login, issue telnet commands to my router, and then exit the telnet session. It requires two files, one a batch file that opens a telnet window and calls a VBS file to send keystroke commands to the telnet window. Maybe this approach will help you accomplish your objective. I cribbed it from something I found on the internet and would give credit to the author if I knew where I found it.

    Batch File (named Script.bat ):

    :: Open a Telnet window
    start telnet.exe 192.168.1.1

    :: Run the script
    cscript SendKeys.vbs

    Command File (named SendKeys.vbs ):

    set OBJECT=WScript.CreateObject("WScript.Shell")
    WScript.sleep 50
    OBJECT.SendKeys "mylogin{ENTER}"
    WScript.sleep 50
    OBJECT.SendKeys "mypassword{ENTER}"
    WScript.sleep 50
    OBJECT.SendKeys " cd /var/tmp{ENTER}"
    WScript.sleep 50
    OBJECT.SendKeys " rm log_web_activity{ENTER}"
    WScript.sleep 50
    OBJECT.SendKeys " ln -s /dev/null log_web_activity{ENTER}"
    WScript.sleep 50
    OBJECT.SendKeys " exit{ENTER}"
    WScript.sleep 50
    OBJECT.SendKeys " "

    Thursday, January 27, 2011 10:32 PM
  • Fishhook1,
    Thank you for the post. I am not a programmer and have spent most of the day looking for something to make it easy to backup the running config on my routers and switches. I made the changes that I needed and it works.

    Thank you :)
    Wednesday, March 02, 2011 1:01 AM