目前devops是整個運維發展的方向,network的運維也一樣。使用程式控制底層的路由器是最基本的要求之一。
本文簡單解釋如何用python控制路由器,對網路裝置進行配置。
一、安裝paramiko
paramiko可以通過標準的pip install安裝,也可以通過整合的安裝包anaconda2種的conda安裝。具體如下:
1. pip install
linux自帶paramiko模組,不需要安裝。如果沒有,可以通過一下命令安裝:
yum install python-pip2. conda installpip install paramiko
c:\program files\anaconda2\scripts>conda.exe install paramiko安裝完成後,開始編寫我們的**。fetching package metadata ...........
solving package specifications: .
package plan for installation in environment c:\program files\anaconda2:
the following new packages will be installed:
paramiko: 2.0.2-py27_0
the following packages will be updated:
conda: 4.3.11-py27_0 --> 4.3.14-py27_1
proceed ([y]/n)? y
paramiko-2.0.2 100% |###############################| time: 0:00:00 703.11 kb/s
conda-4.3.14-p 100% |###############################| time: 0:00:00 2.33 mb/s
二、**
**分幾塊
1. import
需要用到如下的包
#!/usr/bin/env python2. 定義變數import paramiko
import os,platform
import time
# 檢查節點的資訊3.定義ssh# ssh引數ssh=paramiko.sshclient()pinghost="www.sina.com.cn"
# 路由器相關資訊
host='42.159.x.x'
user='azureuser'
password="***x"
ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())try:
ssh.connect(host,username=user,password=password,look_for_keys=false,allow_agent=false)
except valueerror:
print "can't connect to router"
ssh_con=ssh.invoke_shell()
output=ssh_con.recv(500)
# 路由器引數初始化
ssh_con.send("\n")
ssh_con.send("show ip int brie\n")
ssh_con.send("conf t\n")
ssh_con.send("int gi 2\n")
ssh_con.send("no shutdown\n")
ssh_con.send("end\n")
至此,做好了準備工作。
4. 探測、判斷、控制
while true:# 判斷節點是否通
response=os.system("ping " + ("-n 1 " if platform.system().lower()=="windows" else "-c 1 ") + pinghost)
if response == 0:
pingstatus = "network active"
else:
# 如果不通,對路由器進行修改
pingstatus = "network down"
ssh_con.send("conf t\n")
ssh_con.send("int gi 2\n")
ssh_con.send("shutdown\n")
ssh_con.send("end\n")
time.sleep(2)
output = ssh_con.recv(500)
print output
print pingstatus
# 每10s探測一次
time.sleep(10)
通過ping進行檢測,並且可以判斷是否windows機器,並相應的引數不同。如果能ping通,不做動作,如果ping不通,修改路由器埠shutdown。
Cisco 鏈路封裝技術
區域網封裝 ethent 2 ieee 802.3 廣域網封裝 hdlc ppp 幀中繼 fr atm 1 hdlc 高階資料鏈路控制協議 cisco 廣域網鏈路的預設封裝 cisco私有hdlc,加入控制字段 可以描述上層使用協議 工業標準hdlc 僅僅支援上層使用ip協議 定義介面封裝為hdlc...
BGP十三條選路原則 Cisco
bgp最優路徑選擇 在預設情況下,到達同一目的地,bgp只走單條路徑,並不希望在多條路徑之間執行負載均衡。1.最高weight值 選擇最高weight值的路由,weight值為cisco私有,並且只有本地有效,預設weight值為0,本地發起路由為32768。2.最高local pref值 如果we...
Cisco交換機 鏈路聚合
我們在交換機之間提供多條鏈路,這種設計具有冗餘性和彈性 如果兩台交換機之間有多條鏈路,那麼stp會阻斷其中一條鏈路 為了充分利用交換機之間的多條鏈路,可使用埠通道化技術,ether channel就是一種,cisco開發它的初衷是用於在交換機之間將多條快速乙太網或吉位元乙太網鏈路組合成一條邏輯通道 ...