1、採用python2.7
2、保存思路是使用python的ftp.lib庫,對交換機發起連接,通過主動方式,使用FTP下載.zip格式的配置文件
3、簡單可行,保存華爲交換機配置
#!/usr /bin/python
#-*- coding:utf-8 -*-
from ftplib import FTP
import time,os
Today = time.strftime("%Y-%m-%d", time.localtime())#獲取當天時間
hosts_tongjiju = ['10.42.243.1',
'10.42.243.2',
'10.42.243.3',
'10.42.243.4',
'10.42.243.5',
'10.42.243.6',
'10.42.243.7',
'10.42.243.8',
'10.42.243.9',
'10.42.243.10',
'10.42.243.11',
'10.42.243.12',
'10.42.243.13',
'10.42.243.14',
'10.42.243.22',
'10.42.243.23',
'10.42.243.24',
'10.42.243.27',
'10.42.243.31',
'10.42.243.32',
'10.42.243.34',
'10.42.243.50']
port = '21'
user = 'xxxxxx' #交換機登錄賬戶
pwd = 'xxxxxxxxxx' #交換機登錄密碼
for host in hosts_tongjiju:#統計局業務主機
os.chdir('/root/Desktop/backup/tongjiju')#改變當前路徑至當前文件夾(已經在該目錄下創建了這個文件夾)
#if host not in os.listdir('./'):
if os.path.exists(host) is False:#os.path.exists(path) 判斷一個目錄是否存在
os.mkdir(host)#os.mkdir(path) 創建目錄
os.chdir(host)#os.mkdir(path) 改變當前目錄
else:
os.chdir(host)
ftp = FTP()#設置變量
ftp.set_debuglevel(2) #打開調試級別2,顯示詳細信息
ftp.set_pasv(0)#0主動模式 1 #被動模式
ftp.connect(host,port)#連接的ftp sever和端口
ftp.login(user,pwd)#連接的用戶名,密碼
print "login "+ host#顯示主機
print ftp.getwelcome()#打印歡迎信息
bufsize = 1024#設置的緩衝區大小
filename = "{}.zip".format(Today)#設置下載.zip格式的文件。格式爲今天
file_handle = open(filename, "wb").write#以寫模式在本地打開文件
ftp.retrbinary("RETR vrpcfg.zip", file_handle, bufsize)#接收服務器上文件並寫入本地文件
ftp.set_debuglevel(0)#關閉調試模式
ftp.quit()#退出FTP
print "ftp down OK"