0

Here's what I'm trying to emulate:

Open two PuTTY windows and connect with auth to the same machine. Run one command (we'll call this CHECK) in window 1 that waits for the other command (we'll call this TEST) that I run in window 2. The CHECK evaluates the TEST and after the TEST is done shows that it passed in CHECK. I then have to CTRL+C out of it.

I'm trying to skip those logins and do it via a GUI so I just push a button.

I've tried calling the TEST script via subprocess and it runs but CHECK just sits there waiting forever. I've tried it the otherway around and the same result.

If stdin, stdout, stderr doesn't show anything in CHECK what does?

I suppose I could put a wait then close on CHECK because the TEST runs for a set amount of time but I need the output of CHECK to see whether it passed or not.

By the way I'm using tkinter for the GUI with one button and when I run the CHECK part it freezes and becomes Not Responsive. Is there anyway to fix that?

My code so far:

import subprocess
import tkinter as tk
import paramiko

hostname = "xxx.xxx.xxx.xxx"
username = "xxx"
password = "xxx"

def initiate():
    subprocess.call(["python", "ljustest2.py"])

    client1 = paramiko.SSHClient()

    client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        client1.connect(hostname=hostname, username=username, password=password)
        print('Connect 1')
    except:
        print("[!] Cannot connect to the SSH Server")
        exit()

    commands = ['mosquitto_sub -t "/test/#" -v']

    for command in commands:
        print("="*40, command, "="*40)
        stdin, stdout, stderr = client1.exec_command(command)
        print(stdout.read().decode())
        err = stderr.read().decode()
        if err:
            print(err)


window = tk.Tk()
window.title("MQTT")
window.geometry("300x300")
window.resizable(False, False)

window.rowconfigure([0], minsize=300, weight=0)
window.columnconfigure([0], minsize=300, weight=0)

btn_abl = tk.Button(window, text="Start", command = initiate)
btn_abl.grid(row=0, column=0, sticky="nsew")

window.mainloop()

The code for ljustest2.py:

import paramiko

hostname = "xxx.xxx.xxx.xxx"
username = "xxx"
password = "xxx"

client2 = paramiko.SSHClient()

client2.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
    client2.connect(hostname=hostname, username=username, password=password)
    print('Connect 2')
except:
    print("[!] Cannot connect to the SSH Server")
    exit()
    
commands = ['mosquitto_pub -t "/test/start" -m \'{"type": "phcomtest", "cycles": 3}\'']

for command in commands:
    print("="*40, command, "="*40)
    stdin, stdout, stderr = client2.exec_command(command)
    print(stdout.read().decode())
    err = stderr.read().decode()
    if err:
        print(err)
SeanB
  • 11
  • 1
  • Yeah, I just mentioned that I had to do that before and I was trying to emulate it with python. I thought it might be relavant and how to get an output via paramiko the same as PuTTY. But, yeah, 2 ssh sessions :) – SeanB Nov 10 '21 at 10:29
  • It asks for my password and contiues with the CHECK and outputs the passed part. Which is great but I would like to be able to skip the authentication and that's why I use paramiko. If I could just get the output that it's passed. I've search the paramiko api and I cant find what I'm looking for... I appreciate your help! – SeanB Nov 10 '21 at 11:44

1 Answers1

0

I figured it out finally.

I saw this post: Paramiko and exec_command - killing remote process?

Moddified it a little bit and viola! Works like a charm! Thanks to all replies.

import tkinter as tk
import paramiko
import select

hostname = "xxx.xxx.xxx.xxx"
username = "xxx"
password = "xxx"

def initiate():
    client1 = paramiko.SSHClient()

    client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        client1.connect(hostname=hostname, username=username, password=password)
        print('Connect 1')
    except:
        print("[!] Cannot connect to the SSH Server")
        exit()
        
    command = 'mosquitto_pub -t "/test/start" -m \'{"type": "phcomtest", "cycles": 3}\''

    print("="*40, 'Mosquitto TEST ', "="*40)
    client1.exec_command(command)



    client2 = paramiko.SSHClient()

    client2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        client2.connect(hostname=hostname, username=username, password=password)
        print('Connect 2')
    except:
        print("[!] Cannot connect to the SSH Server")
        exit()
    transport = client2.get_transport()
    channel = transport.open_session()
    channel.get_pty()

    commands = ['mosquitto_sub -t "/test/#" -v']

    for command in commands:
        print("="*40,  'Mosquitto CHECK', "="*40)
        channel.exec_command(command)
        while True:
            try:
                rl, wl, xl = select.select([channel],[],[],0.0)
                if len(rl) > 0:
                    if str(channel.recv(1024))[2:-5] == '/test/complete passed':
                        client1.close()
                        client2.close()
                        channel.close()
                        exit(0)
                    print(str(channel.recv(1024))[2:-5])
            except KeyboardInterrupt:
                print("Caught control-C")
                client1.close()
                client2.close()
                channel.close()
                exit(0)


window = tk.Tk()
window.title("Mosuitto")
window.geometry("300x300")
window.resizable(False, False)


btn_abl = tk.Button(window, text="Start", command = initiate)
btn_abl.place(x=0, y=0, width=300, height=300)

window.mainloop()
SeanB
  • 11
  • 1