Как выполнять команды на удаленном сервере, используя python? - PullRequest
0 голосов
/ 17 июня 2020

Этот вопрос связан с другим: Как использовать сокеты для отправки пользователя и пароля на плату разработчика с помощью s sh

Я хочу подключиться к панели разработки по порядку выполнить сценарий. Все выходные данные этого сценария я хочу отправить на машину Elasticsearch.

Я могу подключиться к devboard (см. ИЗОБРАЖЕНИЕ ниже), используя свой ноутбук, на котором установлен Elasticsearch. Но когда я хочу отправить данные на доску разработки, скрипт ничего не показывает. Я делаю:

  • Как только вы найдете mendel@undefined-eft: ~ $ , отправьте команду: cd coral / tflite / python / examples / classification. / Auto_benchmark \ n

Что я делаю не так?

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)

ИЗОБРАЖЕНИЕ

enter image description here

ИЗМЕНИТЬ

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)

enter image description here

1 Ответ

1 голос
/ 17 июня 2020

Думаю, вам нужно изменить

if channel_data.endswith('mendel@undefined-eft:~$'):

на

if channel_data.endswith('mendel@undefined-eft:~$ '):

в соответствии с вашим запросом. Обратите внимание на пробел после :~$

...