代码:
import paramiko import time def get_connection(host, username, password, port): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=host,username=username, password=password, port=port, look_for_keys=False, allow_agent=False) return ssh devices = { 'linux1': { 'ip': 'xxx', 'username': 'xxx', 'password': 'xxx', 'port': '22' }, 'linux2': { 'ip': 'xxx', 'username': 'xxx', 'password': 'xxx', 'port': '22' } } command = 'uname -a \n' for device in devices.keys(): print(f"Executing on device: {devices[device]['ip']}\n\n") ssh = get_connection(host=devices[device]['ip'], username=devices[device]['username'], password=devices[device]['password'], port=devices[device]['port']) stdin, stdout, stderr = ssh.exec_command(command) output = stdout.readlines() print(' '.join(map(str, output)))