from fabric import Connection hosts = ["192.168.145.100", "192.168.145.101"] username = "" password = "" # Loop through the hosts and run the command for host in hosts: # Create a connection object conn = Connection(host=host, user=username, connect_kwargs={"password": password}) # Command to add user and set password using sudo command1 = """sudo -S bash -c 'adduser --gecos "" --disabled-password alex && echo "user:pass" | sudo chpasswd'""" result1 = conn.run(f"echo {password} | {command1}", pty=True) print(f"Output from {host}:\n{result1.stdout.strip()}") command2 = "sudo -S adduser user sudo" result2 = conn.run(f"echo {password} | {command2}", pty=True) print(f"Output from {host}:\n{result2.stdout.strip()}") # Close the connection conn.close()