sam sam - 1 year ago 92
Python Question

Telnet session to ESG Signal Generator

I am trying to control a signal generator using python. I just need to change the amplitude and frequency.`my code is below:

import telnetlib
HOST = ""
timeout = 5
PORT = 5023
tn = telnetlib.Telnet(HOST, PORT , timeout)
tn.read_until("SCPI>", timeout)
tn.write('FREQ 3ghz\r\n')
tn.read_until("SCPI>", timeout)
tn.write('AMPL 10dbm\r\n')
tn.read_until("SCPI>", timeout)

when I open a telnet session directly on port 5023 i am able to change the Freq and Amp but not using the script above. the debug session shows the telnet connection is successful and it did send the commands but nothing changes on the sig Gen. thank you

Answer Source


I've run your code against a listening nc, and Python 2.7 complained that the call to telnetlib.Telnet(HOST, PORT , timeout, Debug) was wrong - the parameter Debug should have been omitted.

Actually, in the documentation the Debug parameter isn't present:[, port[, timeout]])

Connect to a host. The optional second argument is the port number, which defaults to the standard Telnet port (23). The optional

timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).

Do not try to reopen an already connected instance.

Changed in version 2.6: timeout was added.

Once fixed that, however, the commands were sent identical by telnet in raw mode and by your code. It could be some weird timing thing, but apart from that it should work.

Just to be safe I'd still run a tcpdump if I were you.

Old answer

You aren't terminating the commands, so they are not getting executed. .write() does not auto-terminate commands.

tn.write('FREQ 1.92ghz' + "\n")

Also, wait for an answer before closing, just in case.

tn.read_until("some answer", timeout)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download