Paramiko 批量连接主机
来自CloudWiki
主机信息表
ip.txt:
10.0.0.30:22:root:000000 10.0.0.32:22:root:000000 10.0.0.33:22:root:000000
代码
基本版
固定命令,一次输出:
from paramiko.ssh_exception import NoValidConnectionsError from paramiko.ssh_exception import AuthenticationException def connect(cmd,hostname,port=22,username='root',passwd='000000'): import paramiko ##1.创建一个ssh对象 client = paramiko.SSHClient() #2.解决问题:如果之前没有,连接过的ip,会出现选择yes或者no的操作, ##自动选择yes client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #3.连接服务器 try: client.connect(hostname=hostname, port=port, username=username, password=passwd) print('正在连接主机%s......'%(hostname)) except NoValidConnectionsError as e: ###用户不存在时的报错 print("连接失败") except AuthenticationException as t: ##密码错误的报错 print('密码错误') else: #4.执行操作 stdin,stdout, stderr = client.exec_command(cmd) #5.获取命令执行的结果 result=stdout.read().decode('utf-8') print(result) #6.关闭连接 finally: client.close() with open('ip.txt') as f: #ip.txt为本地局域网内的一些用户信息 for line in f: line = line.strip() ##去掉换行符 hostname,port,username,passwd= line.split(':') print(hostname.center(50,'*')) connect('uname', hostname, port,username,passwd)
改进版
任意命令,无限输出
from paramiko.ssh_exception import NoValidConnectionsError from paramiko.ssh_exception import AuthenticationException def connect(cmd,hostname,port=22,username='root',passwd='000000'): import paramiko ##1.创建一个ssh对象 client = paramiko.SSHClient() result = "" #2.解决问题:如果之前没有,连接过的ip,会出现选择yes或者no的操作, ##自动选择yes client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #3.连接服务器 try: import paramiko ##1.创建一个ssh对象 client = paramiko.SSHClient() result = "" #2.解决问题:如果之前没有,连接过的ip,会出现选择yes或者no的操作, ##自动选择yes client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #3.连接服务器 try: client.connect(hostname=hostname, from paramiko.ssh_exception import NoValidConnectionsError from paramiko.ssh_exception import AuthenticationException def connect(cmd,hostname,port=22,username='root',passwd='000000'): import paramiko ##1.创建一个ssh对象 client = paramiko.SSHClient() result = "" #2.解决问题:如果之前没有,连接过的ip,会出现选择yes或者no的操作, ##自动选择yes client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #3.连接服务器 try: client.connect(hostname=hostname, port=port, username=username, password=passwd) print('正在连接主机%s......'%(hostname)) print('正在完成任务.......') except NoValidConnectionsError as e: ###用户不存在时的报错 s = "连接失败" result = s except AuthenticationException as t: ##密码错误的报错 s = '密码错误' result = s else: #4.执行操作 stdin,stdout, stderr = client.exec_command(cmd) #5.获取命令执行的结果 result=stdout.read().decode('utf-8') #6.关闭连接 finally: client.close() return result if __name__ == '__main__': with open('ip.txt') as f: #ip.txt为本地局域网内的一些用户信息 for line in f: line = line.strip() ##去掉换行符 hostname,port,username,passwd= line.split(':') print(hostname.center(50,'*')) while(True): s = input("请输入linux命令(退出请输入q):") if s== 'q': break; else: result = connect(s, hostname, port,username,passwd) print(result)
结果验证
python3 command.py
输入linux命令(退出请输入q):mi^Hk^H^H^H^H 正在连接主机120.46.218.138...... 正在完成任务....... 请输入linux命令(退出请输入q):mkdir hello2^H2 正在连接主机120.46.218.138...... 正在完成任务....... 请输入linux命令(退出请输入q):ls 正在连接主机120.46.218.138...... 正在完成任务....... 3.py ansible.cfg ansible_hosts api0803.py Desktop Documents Downloads elasticsearch-7.17.1-linux-x86_64.tar.gz es_test2.py es_test.py hello hello2 mail-py Music Pictures Public Python-3.7.5.tgz Templates Videos wmtools 请输入linux命令(退出请输入q):