Paramiko, exec_command get the output stream continuously [duplicate]

2024/9/20 8:11:21

I dry on a Python script. I create a python script for a given IP will connect via Paramiko to a server to execute another Python script.

Here is some code:

self._client = paramiko.SSHClient()
self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self._client.connect(self._ip, username=self._server['username'], password=self._server['password'])
channel = self._client.get_transport().open_session()
channel.exec_command('python3 /tmp/scrap.py /tmp/' + self._ip + '.txt 0 1')

The script "scrap.py" returns every X seconds a line in the console of the remote machine, but I can not recover as and when these lines in the script above (at the exit of exec_command (. ..)).

Is this possible, and if so have you any idea how?

Thank you in advance.

Answer

This should do the trick:

stdin, stdout, stderr = channel.exec_command("python myScript.py")
stdin.close()
for line in iter(lambda: stdout.readline(2048), ""):print(line, end="")

As specified in the read([size]) documentation, if you don't specify a size, it reads until EOF, that makes the script wait until the command ends before returning from read() and printing any output.

https://en.xdnf.cn/q/72369.html

Related Q&A

pdfminer3k has no method named create_pages in PDFPage

Since I want to move from python 2 to 3, I tried to work with pdfmine.3kr in python 3.4. It seems like they have edited everything. Their change logs do not reflect the changes they have done but I had…

curve fitting zipf distribution matplotlib python

I tried to fit the following plot(red dot) with the Zipf distribution PDF in Python, F~x^(-a). I simply chose a=0.56 and plotted y = x^(-0.56), and I got the curve shown below. The curve is obviously …

Running python/ruby script on iPhone?

From the recent news from the Apple, I learned that one has to use C/C++/Objective-C for iPhone App. Accordingly, its not possible to use MacPython or similar to make iPhone App. But as the python/ruby…

Unexpected behavior of universal newline mode with StringIO and csv modules

Consider the following (Python 3.2 under Windows):>>> import io >>> import csv >>> output = io.StringIO() # default parameter newline=None >>> csvdata = [1, …

logger chain in python

Im writing python package/module and would like the logging messages mention what module/class/function they come from. I.e. if I run this code:import mymodule.utils.worker as workerw = worker.Worker()…

How to make data to be shown in tabular form in discord.py?

Hi I am creating a bot that makes points table/leaderboard , below is the code which works really nice. def check(ctx):return lambda m: m.author == ctx.author and m.channel == ctx.channelasync def get_…

Getting Python version using Go

Im trying to get my Python version using Go:import ("log""os/exec""strings" )func verifyPythonVersion() {_, err := exec.LookPath("python")if err != nil {log.Fata…

Python shutil.copytree() is there away to track the status of the copying

I have a lot of raster files (600+) in directories that I need copy into a new location (including their directory structure). Is there a way to track the status of the copying using shutil.copytree()?…

Py2exe error: [Errno 2] No such file or directory

C:\Users\Shalia\Desktop\accuadmin>python setup_py2exe.py py2exe running py2exe10 missing Modules------------------ ? PIL._imagingagg imported from PIL.ImageDraw ? PyQt4 …

pandas rolling window mean in the future

I would like to use the pandas.DataFrame.rolling method on a data frame with datetime to aggregate future values. It looks it can be done only in the past, is it correct?