gpt4 book ai didi

python - 在使用从文件读取的值的 String.format() 之后,生成的字符串不包含插入值之后的部分

转载 作者:太空宇宙 更新时间:2023-11-03 20:35:39 25 4
gpt4 key购买 nike

我有一个脚本,它通过 Paramiko 使用 SSH 登录交换机并启动启动配置的 TFTP 副本。这几乎像我想要的那样工作,只是文件名不包含 .txt 文件类型。

我正在使用的具体线路是:

command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i))

尝试查看 Google 上的示例。

#!/usr/bin/env python3
###############################################
# THIS SCRIPT SAVES THE RUNNING-CONFIG TO #
# STARTUP-CONFIG AND THEN COPIES THE #
# STARTUP-CONFIG TO THE SPECIFIED IP VIA TFTP #
###############################################
#
import paramiko
import sys, time
#
username = 'user'
password = 'pass'
#
with open('Advantech_Test_IPs', 'r') as host_list:
for i in host_list:
str(i)
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(i, username=username, password=password)
command = ssh_client.invoke_shell()
command.send("copy running-config startup-config \n")
time.sleep(1)
command.send("copy startup-config tftp://.../Backup-{}.txt \n".format(i))
time.sleep(1)
ssh_client.close
print(i, "BACKUP COMPLETE")
except paramiko.ssh_exception.NoValidConnectionsError as e:
pass
print(i, "No Valid Connections")
except paramiko.ssh_exception.AuthenticationException as ea:
print(i, "Authentication Failed")
except paramiko.ssh_exception.BadHostKeyException as eb:
print(i, "Bad Host Key")
except Exception as ex:
print(i, "SOMETHING WENT WRONG!")
print("All done.")

文件被复制,但未附加 .txt 文件扩展名,因此我最终得到与 IP 地址最后部分匹配的文件类型。

最佳答案

i 末尾包含一个新行。因此,您实际上向服务器发送了两行:

copy startup-config tftp://192.168.0.1/Backup-IP
.txt

您可以使用str.strip (或 str.rstrip )删除换行符:

command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i.strip()))

关于python - 在使用从文件读取的值的 String.format() 之后,生成的字符串不包含插入值之后的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57188352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com