diff --git a/ssh_with_sub/main.py b/ssh_with_sub/main.py index 26fad40..e09aec7 100755 --- a/ssh_with_sub/main.py +++ b/ssh_with_sub/main.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3.10 -from subprocess import Popen, PIPE from csv import DictReader -import os, socket +import os, socket, subprocess @@ -18,14 +17,15 @@ def read_csv(csv_file = str): } -def ssh_connection(con_server = str, bash_script = """ls -lisaR /home; apt list --installed"""): - with Popen(['ssh', '-T', str(con_server)], - stdin=PIPE, stdout=PIPE, stderr=PIPE, - universal_newlines=True) as p: - output, error = p.communicate(bash_script) - print(output) - print(error) - print(p.returncode) +def ssh_connection(con_server= str,con_user = str,bash_script = """ls"""): + return subprocess.Popen( + "ssh -T {user}@{host} '{cmd}'".format(user=con_user, host=con_server, cmd=bash_script), + shell=True, + universal_newlines=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ).communicate() + def ssh_check(con_server = str, check_port = 22): response = os.system("ping -c 1 " + con_server + '>& /dev/null') @@ -39,7 +39,11 @@ def ssh_check(con_server = str, check_port = 22): if __name__ == "__main__": script_folder = os.path.dirname(os.path.realpath(__file__)) server_csv = script_folder + os.sep + "test.csv" - + run_script_bash = """ +GREP_PACK="atlas_ibtools" +echo "test" +bash --version +""" for count in range(0,len(read_csv(server_csv)['server'])): servername = read_csv(server_csv)['server'][count] con_username = read_csv(server_csv)['user'][count] @@ -47,7 +51,8 @@ if __name__ == "__main__": print(servername, "verbindng wird geprüft...",sep=' ') if ssh_check(con_server=servername) is True: print(servername, "erreichbar...", sep=' ') - ssh_connection(con_server=user_server_con) + for show_output in list(ssh_connection(con_server=servername, con_user=con_username, bash_script=run_script_bash)): + print(show_output.rstrip()) else: print(servername, "nicht erreichbar oder kein ssh!!", sep=' ') #if ssh_check(con_server=servername) is True: