首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python在远程服务器上执行命令?

如何使用python在远程服务器上执行命令?
EN

Stack Overflow用户
提问于 2020-06-16 23:34:32
回答 1查看 854关注 0票数 0

这个问题与另一个问题有关:How to use sockets to send user and password to a devboard using ssh

为了执行脚本,我想连接到开发板。我想要发送给Elasticsearch机器的脚本的所有输出。

我可以使用我的笔记本电脑连接到开发板(见下图),它恰好安装了Elasticsearch。但是,当我想向开发板发送数据时,脚本没有显示任何内容。我所做的是:

找到mendel@undefined-eft:~$后立即发送命令:cd coral/tflite/python/examples/classification/Auto_benchmark\n

我做错了什么?

代码语言:javascript
复制
import paramiko
import os

#Server's data
IP = '172.16.2.47'
PORT = 22
USER = 'mendel'
PASSWORD = 'mendel'


ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = IP, port=PORT, username = USER, password = PASSWORD)

channel = ssh.invoke_shell() #to get a dedicated channel

channel_data = str()
host = str()

while True:
    if channel.recv_ready(): #is there data to be read?
       channel_data += channel.recv(9999).decode("utf-8")
       os.system('clear')
       print(channel_data)

#ONLY WORKS UNTIL HERE!!!

    else:
        continue

    if channel_data.endswith('mendel@undefined-eft:~$'):
        channel.send('cd coral/tflite/python/examples/classification/Auto_benchmark\n')
        channel_data += channel.recv(9999).decode("utf-8")
        print(channel_data)

图像

编辑

代码语言:javascript
复制
channel = ssh.invoke_shell() #to get a dedicated channel

channel_data = str()
host = str()

while True:
    if channel.recv_ready(): #is there data to be read?
       channel_data += channel.recv(9999).decode("utf-8")
       os.system('clear')
       print(channel_data)

    else:
        continue

    if channel_data.endswith('mendel@undefined-eft:~$ '):#it is good to send commands
       channel.send('cd coral/tflite/python/examples/classification/Auto_benchmark\n')
       #channel_data += channel.recv(9999).decode("utf-8")
       #print(channel_data)
    elif channel_data.endswith('mendel@undefined-eft:~/coral/tflite/python/examples/classification/Auto_benchmark$ '):
         channel.send('ls -l\n') #python3 auto_benchmark.py')
         channel_data += channel.recv(9999).decode("utf-8")
         print(channel_data)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-17 06:12:22

我想你得改变一下

代码语言:javascript
复制
if channel_data.endswith('mendel@undefined-eft:~$'):

代码语言:javascript
复制
if channel_data.endswith('mendel@undefined-eft:~$ '):

根据您的提示。请注意:~$后面的空格

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62419187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档