在 String.format() 从文件中读取值后,结果字符串不包含插入值之后的部分
After String.format() with a value read from a file the resulting string does not contain the part after the inserted value
我有一个脚本,它通过 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
(or str.rstrip
) 删除换行符:
command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i.strip()))
我有一个脚本,它通过 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
(or str.rstrip
) 删除换行符:
command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i.strip()))