0

This question is related to this other one: How to use sockets to send user and password to a devboard using ssh

I want to connect to the devboard in order to execute a script. All the outputs of that script I want to send to a Elasticsearch machine.

I can connect to the devboard (see IMAGE below) using my laptop which happens to have Elasticsearch installed. But, when I want to send data to the devboard, the script shows nothing. What I am doing is:

  • As soon as you find mendel@undefined-eft:~$ , send the command: cd coral/tflite/python/examples/classification/Auto_benchmark\n

What am I doing wrong?

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)

IMAGE

enter image description here

EDIT

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

Aizzaac
  • 3,146
  • 8
  • 29
  • 61

1 Answers1

1

I guess you have to change the

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

to

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

according to your prompt. Please note the space after :~$

JGK
  • 3,710
  • 1
  • 21
  • 26
  • Damn! you are right! how did you notice that? I am having another problem...when I execute 'ls -l' it never stops executting it. Do yuo know how can I improve my code. I will post it. – Aizzaac Jun 17 '20 at 13:45
  • That's true, because the `elif channel_data.endswith('mendel@undefined-eft:~/coral/tflite/python/examples/classification/Auto_benchmark$ '):` statement in the `while True:` loop matches on every iteration. It would be easier if you clarify your problem, i.e. what do you want to do in the ssh session. – JGK Jun 17 '20 at 14:55
  • Well, I want to send the data of the devboard to a server (ELasticsearch). In the devboard I have a script which does image classification (I want tosend that data to the Elasticsearch server). But the code that Ihave posted is out of the devboard (it is in my laptop). So I want to connect to the devboard and execute a script. Is there other way to do it? – Aizzaac Jun 17 '20 at 15:01
  • 1
    Why don't you try something like https://stackoverflow.com/questions/19900754/python-subprocess-run-multiple-shell-commands-over-ssh This is much easier. – JGK Jun 18 '20 at 09:28