0
我想使用Python/paramiko來備份路由器配置。我寫了一個可以工作的函數,但我想讀取一個CSV文件,分割行,然後將列表中的項作爲參數傳遞給函數。使用列表項作爲函數參數
這是代碼:
import datetime
import paramiko
def tftpbackup(tftphost,netdevice,hostname):
date = datetime.date.today().strftime("%Y%m%d")
sshuser = 'autobackup'
sshpass = 'nmol#567'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(netdevice, username= sshuser, password= sshpass)
stdin, stdout, stderr = ssh.exec_command("copy run tftp")
stdin.write(tftphost+'\n')
stdin.flush()
stdin.write(hostname + '-cfg-' + date + '\n')
print(hostname + '-cfg-' + date + '\n')
netdevices = open ("network devices.csv","r")
for line in netdevices:
device = line.split(",")
hostname = device[0]
ipaddr = device[1]
ipaddr.strip()
hostname.strip()
tftpbackup('10.20.17.21',ipaddr,hostname)
print (ipaddr, hostname)
netdevices.close()
這是CSV:
cclcoresw,10.200.17.2
ccl1stflrmdfsw01,10.200.17.3
ccl1stflrmdfsw02,10.200.17.4
ccl1stflrmdfsw03,10.200.17.5
ccl1stflrmdfsw04,10.200.17.14
ccl3rdflridfsw01,10.200.17.8
cclphdidfsw01,10.200.17.9
它失敗,出現以下錯誤:
Traceback (most recent call last):
File "C:\Python34\Lib\SITE-P~1\PYTHON~2\pywin\framework\scriptutils.py",
line 326, in RunScript
exec(codeObject, __main__.__dict__)
File "C:\Users\*redacted*\Desktop\tftp backup.py", line 27, in <module>
tftpbackup('10.20.17.21',ipaddr,hostname)
File "C:\Users\*redacted*\Desktop\tftp backup.py", line 10, in tftpbackup
ssh.connect(netdevice, username= sshuser, password= sshpass)
File "C:\Users\*redacted*\AppData\Roaming\Python\Python34\site-
packages\paramiko\client.py", line 237, in connect
for (family, socktype, proto, canonname, sockaddr) in
socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
File "C:\Python34\lib\socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
任何人都能夠看到它失敗?
OP提到該功能起作用,但不是當它來自CSV文件時 – asiviero
這是字符串格式問題,asiviero的答案起作用。 –