python连接ssh时,通过获取到存放在execl中的ip,用户名,密码时,会认证失败,不管是否通过转义,都无法连接成功,这到底是什么问题,通过xshell可以连接成功
# 选择工作表
sheet = workbook.active
# 遍历每一行,连接到每个主机
for row in sheet.iter_rows():
ip_address = row[0].value
username = row[1].value
password = row[2].value
# 对特殊字符进行转义
password = re.escape(password)
# 建立 SSH 连接
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect(hostname=ip_address, username=username, password=password)
print(f"连接到 {ip_address} 成功")
# 执行命令
stdin, stdout, stderr = ssh_client.exec_command('ls')
# 获取命令执行结果
output = stdout.read().decode()
error = stderr.read().decode()
if output:
print(f"命令执行结果:\n{output}")
if error:
print(f"命令执行错误:\n{error}")
except paramiko.AuthenticationException:
print(f"连接到 {ip_address} 失败:认证失败")
except paramiko.SSHException as e:
print(f"连接到 {ip_address} 失败:{str(e)}")
except Exception as e:
print(f"连接到 {ip_address} 失败:{str(e)}")
finally:
# 关闭 SSH 连接
ssh_client.close()
# 关闭 Excel 文件
workbook.close()
怎么都会认证失败
下面的是用户名密码