一、模块简述
延续上篇文档,利用搭建好的FTP服务器进行存储数据,由于内网存在firepower防火墙,但是还没有网管设备自动保存设备配置,所以出此下策
paramiko:实现ssh连接,输入命令
time:设置脚本等待时间
getpass:安全的输入密码
ftp:上传文件到服务器
二、脚本
import paramiko
import time
from getpass import getpass
from ftplib import FTP
username = input('Username:')
password = getpass('password:')
f = open("fw.txt", "r")
def ftpconnect():
ftp = FTP()
ftp.connect("192.168.10.1",21)
ftp.login("user","123456")
return ftp
for line in f.readlines():
ip = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=username, password=password)
print("Successfully connect to ", ip)
remote_connection = ssh_client.invoke_shell()
time.sleep(5)
remote_connection.send("show running-config\n")
time.sleep(2)
output = remote_connection.recv(65535)
full_path = ip + '.txt'
file = open(full_path, 'wb')
file.write(output)
ftp = ftpconnect()
fp = open(full_path,'rb')
ftp.storbinary('STOR %s' %full_path,fp) #%s是将后边的变更替换到引号内
ftp.close()
file.close
f.close()
ssh_client.close
三、脚本介绍
四、验证
登陆服务器验证是否存在
后续继续捣鼓。。