Example of how to use Telnet

Rmpf2

Example of how to use Telnet   14 April 2017, 03:09

The new Telnet feature sounds interesting. Can you give an example of how can this can be used. Maybe a sample script that shows the proper way to be implemented.

Thanks!
SoftPerfect Support forum - Andrew avatar image

Re: Example of how to use Telnet   14 April 2017, 09:57

Sure. There's a sample actually, though it's only shown when the default config is loaded.

Here's the sample code:
'Retrieve system info via Telnet
'This example code can be adapted to work
'with any device that supports the protocol

'Create internal Telnet object
set objTelnet = Internal.CreateObject("Telnet")

'Set host name and port
objTelnet.Host = Input.Current
objTelnet.Port = 23

'Make connection
objTelnet.Connect

'Wait for login prompt
objTelnet.WaitFor("login:")
'Then send login name
objTelnet.Send("YourUserName" & vbCrLf)

'Wait for password prompt
objTelnet.WaitFor("password:")
'Then send password
objTelnet.Send("YourPassword" & vbCrLf)

'Wait for shell prompt
objTelnet.WaitFor("xsh>")
'Then send command
objTelnet.Send("show sys versions" & vbCrLf)

'Wait for shell prompt (when command finishes)
data = objTelnet.Recv("xsh>")

'Return data and disconnect
Output.Write data
objTelnet.Disconnect
Timmy

Re: Example of how to use Telnet   01 September 2026, 22:21

Hi, in the latest release changelog it says "Removed: Remote suspend/hibernate, Comment column, uptime option and Telnet scripting object". Is there any other solution or procedure to implement this, especially for the Telnet needs?
SoftPerfect Support forum - Andrew avatar image

Re: Example of how to use Telnet   02 September 2026, 13:56

The Telnet object went away because Python made it redundant. Remote Python arrived in 25.8 and it runs a real CPython installation rather than an interpreter we ship, so socket and anything you install with pip are already available to a script. There is no longer much point in us wrapping a protocol in a custom object.

Set one up under Options → Remote Python. The dialog tells you whether an interpreter was found and where, with a download link and a button to point it at a specific one if you keep it somewhere unusual.

Here is that 2017 sample rewritten. Same login, password, prompt sequence, and the small helper answers telnet option negotiation with a flat no so those bytes never reach your output:

import socket

IAC, DONT, WONT, WILL = 255, 254, 252, 251

def refuse(sock, buf):
    out, data = bytearray(), bytearray()
    i = 0
    while i < len(buf):
        if buf == IAC and i + 2 < len(buf):
            out += bytes([IAC, DONT if buf[i+1] == WILL else WONT, buf[i+2]])
            i += 3
        else:
            data.append(buf); i += 1
    if out:
        sock.sendall(bytes(out))
    return data.decode('ascii', 'ignore')

def wait_for(sock, token):
    seen = ''
    while token not in seen:
        chunk = sock.recv(4096)
        if not chunk:
            break
        seen += refuse(sock, chunk)
    return seen

host = Input.Current()
sock = socket.create_connection((host, 23), timeout=10)
sock.settimeout(10)
try:
    wait_for(sock, 'login:')
    sock.sendall((Input.Username('telnet') + '\r\n').encode())
    wait_for(sock, 'password:')
    sock.sendall((Input.Password('telnet') + '\r\n').encode())
    wait_for(sock, 'xsh>')
    sock.sendall(b'show sys versions\r\n')
    Output.Write(wait_for(sock, 'xsh>'))
finally:
    sock.close()

Two things you get here that the old object could not do. Input.Username and Input.Password read from the stored credentials for that tag, so the login stops living in plain text inside the script, and the timeout is yours to set, so a device that never answers fails its own item instead of holding up the scan.

If you would rather use telnetlib, be aware Python dropped it from the standard library in 3.13, so it only works if the interpreter Network Scanner picks up is 3.12 or older. telnetlib3 from pip is the maintained replacement. The version above needs neither, which is why I would start with it.

One more thing worth knowing: if any of these devices also speak SSH, Remote.SSH("item name") runs a configured SSH item straight from a Python script and saves you the whole exchange.

Reply to this topic

Sometimes you can find a solution faster if you try the forum search, have a look at the knowledge base, or check the software user manual to see if your question has already been answered.

Our forum rules are simple:

  • Be polite.
  • Do not spam.
  • Write in English. If possible, check your spelling and grammar.

Author:

Email:

Subject

A brief and informative title for your message, approximately 4–8 words:

     

Spam prevention: please enter the following code in the input field below.

 **     **  ********   **     **  **    **  **     ** 
 **     **  **     **   **   **   ***   **  **     ** 
 **     **  **     **    ** **    ****  **  **     ** 
 *********  ********      ***     ** ** **  **     ** 
 **     **  **           ** **    **  ****  **     ** 
 **     **  **          **   **   **   ***  **     ** 
 **     **  **         **     **  **    **   *******  

Message: