pip3 install paramiko import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='192.168.1.100', port=22, username='root', password='123456') # 执行命令 stdin, stdout, stderr = ssh.exec_command('df -h') # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close() print(result.decode('utf-8'))
re模块是python独有的匹配字符串的模块,该模块种提供功能基于正则表达式实现的,对于字符串进行模糊匹配找到想要的内容信息. 常用方法 findAll(): 匹配所有的字符串,把匹配结果作为一个列表返回 match(): 匹配字符串的开始位置,如果开始位置没有,则返回None search():在字符串中搜索,返回搜索到的第一个 finditer():匹配所有的字符串,返回迭代器