1. 程式人生 > >使用python腳本利用SSH協議通過TFTP備份華為交換機配置

使用python腳本利用SSH協議通過TFTP備份華為交換機配置

python ssh 交換機

前提工作

python中默認沒有安裝SSH模塊,SSH功能依靠paramiko 模塊實現,需要自己獨立安裝,具體安裝步驟自行百度。

主要腳本,和之前一樣,通過TFTP備份配置

#!/usr/bin/python

#-*- coding: utf-8 -*-

import re

import paramiko #引入ssh模塊,該模塊需要單獨安裝。

import time

LogTime = time.strftime(‘%Y-%m-%d_%H-%M-%S‘)

tftp = raw_input(‘Please Enter TFTP Sever IP:‘)

temp = open(‘config.txt‘,‘w‘)

hostname = ‘192.168.202.254‘

port = 22

username = ‘admin‘

password = ‘passw0rd‘

client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect(hostname, port, username, password, timeout=5)

remote_conn = client.invoke_shell()

remote_conn.send(‘display version\n‘)

time.sleep(1)

remote_conn.send(‘\n‘)

out = remote_conn.recv(temp)

DeviceName = (re.findall(str(".*<(.*)>.*"),out))[0]

save = "save %s-%s.cfg " %(DeviceName,LogTime)

remote_conn.send(save+‘\n‘)

time.sleep(1)

remote_conn.send(‘y‘+‘\n‘)

time.sleep(2)

tftp_cli = "tftp %s put %s-%s.cfg" %(tftp,DeviceName,LogTime)

remote_conn.send(tftp_cli+‘\n‘)

time.sleep(2)

print hostname,‘Backup Success !!‘


本文出自 “阿建” 博客,請務必保留此出處http://hardwork.blog.51cto.com/2529098/1944534

使用python腳本利用SSH協議通過TFTP備份華為交換機配置