0

I'm using Python 3.5. I need to execute a command over ssh and the result I'm getting back is often not what I see in terminal if I executed same command. Often it seems to be cutting it short after first '\n' but other times it works fine. Why is it doing this?

def simple_ssh(zone, cmd):

    # get zone ip address
    machine = socket.getaddrinfo(zone, 22, socket.AF_INET)
    ip_address = machine[0][4][0]
    # print(ip_address)

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip_address, port=22, username='xxxxxxxxx', password='xxxxxxxxx', look_for_keys=False)

    data = [None, None]
    _, stdout, stderr = client.exec_command(cmd, timeout=10)
    stdout.channel.recv_exit_status()  # wait till finished
    data[0] = stdout.read().decode("utf-8")
    data[1] = stderr.read().decode("utf-8")
    client.close()

    return data

print(simple_ssh('fetish', "cd /usr/fl/log/ws-fsdb_vol_fetish-images ; grep 'first pass' console.txt"))

Output:

['2019-07-18 11:22:58 fsdb (0 sec, 10.00 ms, 139696900073312): main: first pass: started\n', '']

When same run in terminal:

['2019-07-18 11:22:58 fsdb (0 sec, 10.00 ms, 139696900073312): main: first pass: started\n2019-07-18 11:24:11 fsdb (73 sec, 14050.00 ms, 139696900073312): main: first pass status:complete time:73\n', '']
michal-ko
  • 397
  • 4
  • 12

0 Answers0