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_()