Example of how to use the new Telnet feature

4 posts

Rmpf2 Guest
13 Apr 2017

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

Thanks!

Andrew SoftPerfect
13 Apr 2017

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 Guest
1 Sep

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?

Andrew SoftPerfect
2 Sep

The Telnet object went away because Python made it redundant. Remote Python arrived in v25.8 and it runs a real CPython installation rather than an interpreter, 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 the above 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[i] == 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]); 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 that 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. The telnetlib3 from pip is the maintained replacement. The version above needs neither, which is why it's better to 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
Shown with your post. Sign in to keep the same name every time.
(optional, never shown)
Only needed if you want notifications about replies.
Drag files here, paste a screenshot, or . Images up to 10 MB. Text, log and zip files up to 2 MB. Up to 5 files per post.
You can edit your post until someone replies.