0
votes

I want to communicate with a data-logger via Telnet. Therefore, I wrote the following python-script:

import subprocess

command ='plink.exe -telnet -P 23 12.17.46.06'

p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1, shell=False)

answer =  p.communicate('command')[0]
print answer

By running the script, a plink-windows pops up. The python script seems to wait for some action to be done inside the plink command window. By closing the window manually, the desired "answer" shows up inside python.

I am looking for a command / procedure to close plink directly out of python. It seems not to be sufficient to just close the subprocess, as in this case only the communication between python and plink gets closed and not the program plink.exe itself.

Any help is appreciated! Regards, Phil

2

2 Answers

2
votes

The documentation for the communicate() function says: Wait for process to terminate. Thus the function does not return until plink.exe exits and thus your program doesn't get the output until then.

You should add to your 'command' something that will close the telnet connection. When the far end closes the telnet connection plink.exe will exit and its window will close. If your telnet session runs a unix shell you could add '; exit' to your command.

0
votes

You can check if your task within plink tunnel is complete and then execute taskkill within your script

something like,
killProg=taskkill /f /fi "imagename eq plink.exe"
p.communicate('killProg')[0]

That will kill plink while keeping the tunnel open to execute other commands.