Python 使用从文件读取的值在String.format()之后生成的字符串不包含插入值之后的部分
我有一个脚本,它通过Paramiko使用SSH登录到交换机并启动启动配置的TFTP副本。除了文件名不包含.txt文件类型之外,这几乎和我想要的一样 我正在使用的具体产品线是:Python 使用从文件读取的值在String.format()之后生成的字符串不包含插入值之后的部分,python,string.format,Python,String.format,我有一个脚本,它通过Paramiko使用SSH登录到交换机并启动启动配置的TFTP副本。除了文件名不包含.txt文件类型之外,这几乎和我想要的一样 我正在使用的具体产品线是: command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i)) 试着看一下谷歌上的例子 #!/usr/bin/env python3 ############################################
command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i))
试着看一下谷歌上的例子
#!/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末尾包含一行新行。因此,实际上您向服务器发送了两行代码:
复制启动配置tftp://192.168.0.1/Backup-IP
.txt
您可以使用(或)删除换行符:
command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i.strip()))
i
末尾包含新行。因此,实际上您向服务器发送了两行代码:
复制启动配置tftp://192.168.0.1/Backup-IP
.txt
您可以使用(或)删除换行符:
command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i.strip()))
成功了。非常感谢。就我个人的理解,我之后的新线在哪里?它是自动存在的吗?您的文件包含
“IP1\nIP2\nIP3\n…”
–当您逐行迭代它时,您会得到“IP1\n”
,“IP2\n”
,“IP3\n”
。非常感谢。就我个人的理解,我之后的新线在哪里?它是自动存在的吗?您的文件包含“IP1\nIP2\nIP3\n…”
–当您逐行迭代它时,您会得到“IP1\n”
,“IP2\n”
,“IP3\n”
。。。