-1

I'm using Paramiko and Pyqt to make a script manager. When my program starts I connected to my raspberry pi and run the function get_proc() which executes successfully and prints out all the running processes on my pi as evidence. Then I have a button that when pressed executes the function dump_start_click which uses the return of get_proc() for its logic. When pressing the button, I get NameError: 'client is not defined . I never run client.close(). If I had get_proc() multiple times in a row under my main statement they all execute perfectly but calling it from another function fails. Any ideas?

def dump_start_click():
    if get_proc() == 'offline':
        ssh_log("Starting Dump 1090")
        stdin, stdout, stderr = client.exec_command('/home/pi/dump1090/./dump1090 --quiet --net')
        stdin.close()
        stdout.close()
        stderr.close()
        time.sleep(1)
        proc_id = get_proc()
        if proc_id != 'failed':
            ssh_log("Dump 1090 started successfully")
            ssh_log("Process id : " + proc_id)

    else:
        ssh_log('Dump 1090 is already running!')

def get_proc():
    stdin, stdout, stderr = client.exec_command('ps aux')
    x = stdout.read().decode("utf8")
    print(x)
    dump_proc = []
    for line in x.split('\n'):
        if 'dump1090' in line:
            buff = line.split(' ')
            for j in buff:
                if j != '':
                    dump_proc.append(j)
    if len(dump_proc) == 0:
        return 'offline'
    return dump_proc[1]



if __name__ == "__main__":
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy)
    client.load_system_host_keys()
    # Known_host policy
    client.set_missing_host_key_policy(AutoAddPolicy())
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = control_ui(MainWindow)
    MainWindow.show()
    try:
        ssh_log("Connecting to Pi...")
        client.connect('192.168.0.29', username='pi', password='redacted')
    except:
        ssh_log("SSH Connection Failed")
    ssh_log("Connected!")
    get_proc()
    win32api.SetConsoleCtrlHandler(on_exit, True)
    try:
        pretty.install()
        ssh_connect()
    except KeyboardInterrupt:
        on_exit(True)
    app.exec_()
LeonJones
  • 29
  • 1
  • 6

1 Answers1

0

I think the problem and the difference are that you run get_proc() by executing this script. And you are trying to run dump_start_click() from another module where you import it. The problem is when you import dump_start_click somewhere else the code inside the if __name__ == "__main__": is not executed, hence the client is not defined. If you extract the client creation lines from if __name__ == "__main__": it should work.

And check this question for more info.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
  • I tried taking the client creation lines out of if __name__ == "__main__":before when trying to solve this issue but was getting the Attribute Error: NoneType object has no attribute 'open session' on button click. Seems like the same issue but it seems like pyqt needs if __name__ == "__main__": to run. I'll have to read more about how if __name__ == "__main__": works... – LeonJones Mar 04 '21 at 15:07