沉迷了好久的手游握截。拐邪。。差不多是時(shí)候務(wù)一下正業(yè)了氮帐。
目標(biāo)
自動(dòng)化網(wǎng)絡(luò)機(jī)器配置
工具
python
VBA
必須要吐槽一下我走過(guò)的那些彎路
由于完全不知道該用什么模塊嗅虏,邊查邊學(xué),每次都是撞一鼻子灰再換個(gè)方向上沐,鼻子都快給撞平了皮服。。
最開(kāi)始查到的是ConfigParser和paramiko
ConfigParser讀取登錄信息(用戶(hù)名参咙,密碼龄广,接口番號(hào),ip地址)蕴侧,paramiko建立鏈接并send show命令择同。。净宵。到此為止都很美好敲才,直到發(fā)現(xiàn)paramiko不識(shí)別set,delete塘娶,show |compare归斤,commit....
然后發(fā)現(xiàn)juniper有個(gè)自制的PyEZ,都帶EZ了肯定很好用對(duì)嗎5蟀丁脏里!
import的時(shí)候發(fā)現(xiàn)依存模塊lxml的etree加載不了。虹曙。迫横。至今仍是懸案番舆。。矾踱。
也不能總在死路里打轉(zhuǎn)恨狈。。呛讲。所以又開(kāi)始找別的代替模塊禾怠。。
皇天不負(fù)有心人贝搁。吗氏。。
讓我找到了pexpect雷逆。弦讽。。就直接發(fā)送文字列 簡(jiǎn)單粗暴膀哲。往产。。
雖然感覺(jué)寫(xiě)出來(lái)的東西毫無(wú)優(yōu)雅可言某宪。仿村。。
總之缩抡。奠宜。包颁。能用就成瞻想。。娩嚼。蘑险。(自暴自棄)
先寫(xiě)好python的部分
※[ ]中的部分可以任意修改為自己需要的數(shù)值
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#=============================
import sys
import pexpect
#=============================
def Fnc_MAIN():
print 'starting'
#ssh鏈接,logfile=sys.stdout用于返回命令執(zhí)行結(jié)果
ipaddr=sys.argv[1]
command=pexpect.spawn('ssh [用戶(hù)名]@'+ipaddr,logfile=sys.stdout)
command.expect('Password')
command.sendline('[密碼]')
command.expect('[host名]>')
#事前l(fā)og
command.sendline('[隨便寫(xiě)點(diǎn)show命令]')
command.expect('[host名]>')
command.sendline('[隨便寫(xiě)點(diǎn)show命令]')
command.expect('[host名]>')
command.sendline('[隨便寫(xiě)點(diǎn)show命令]')
command.expect('[host名]>')
command.sendline('[隨便寫(xiě)點(diǎn)show命令]')
command.expect('[host名]>')
command.sendline('configure')
#command------------------
command.expect('[host名]#')
command.sendline('set interfaces ge-0/0/1 description qwer')
command.expect('[host名]#')
command.sendline('set interfaces ge-0/0/2 description qwer')
command.expect('[host名]#')
command.sendline('set interfaces ge-0/0/3 description qwer')
command.expect('[host名]#')
command.sendline('set interfaces ge-0/0/4 description qwer')
command.expect('[host名]#')
command.sendline('show |compare')
command.expect('[host名]#')
command.sendline('commit check')
command.expect('[host名]#')
command.sendline('commit')
command.expect('[host名]#')
command.sendline('exit')
#事后log
(和事前l(fā)og一樣岳悟,懶得寫(xiě)了佃迄。。)
#Return ---------------------------
return
# ================================================
# === Main =======================================
# --------------------------------------
if (__name__ == '__main__'):
Fnc_MAIN()
print ('\nFinish Script.')
# --------------------------------------
# ================================================
不要問(wèn)我為什么不用loop贵少。呵俏。。
你試試看滔灶。普碎。。
我也不知道為什么pexpect好像看不懂loop录平。麻车。缀皱。
VBA
由于這個(gè)腳本并不是那么自動(dòng)。动猬。所以決定用VBA來(lái)創(chuàng)建腳本
在EXCEL中輸入用戶(hù)名啤斗,密碼,命令行等信息赁咙,然后依據(jù)輸入值創(chuàng)建.py文本
Option Explicit
Sub makeText()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
Dim py As String
py = ActiveWorkbook.Path & "\config.py"
Open py For Output As #1
'ssh connect
Print #1, "#!/usr/bin/env python"
Print #1, "# -*- coding: utf-8 -*-"
Print #1, "#============================="
Print #1, "import sys"
Print #1, "import pexpect"
Print #1, "#============================="
Print #1, "def Fnc_MAIN():"
Print #1, " print 'starting'"
Print #1, " #login--------------------"
Print #1, " ipaddr=sys.argv[1]"
Print #1, " command=pexpect.spawn('ssh " & ws.Cells(2, 1).Value & "@'+ipaddr,logfile=sys.stdout)"
Print #1, " command.expect('Password')"
Print #1, " command.sendline('" & ws.Cells(4, 1).Value & "')"
Print #1, " command.expect('" & ws.Cells(7, 1).Value & "')"
'log before send command
Print #1, " #log----------------------"
Dim a As Long
a = 2
Do While ws.Cells(a, 2).Value <> ""
Print #1, " command.sendline('" & ws.Cells(a, 2).Value & "')"
Print #1, " command.expect('" & ws.Cells(7, 1).Value & "')"
a = a + 1
Loop
Print #1, " command.sendline('configure')"
'execute command
Print #1, " #command------------------"
Dim b As Long
b = 2
Do While ws.Cells(b, 3).Value <> ""
Print #1, " command.expect('" & ws.Cells(8, 1).Value & "')"
Print #1, " command.sendline('" & ws.Cells(b, 3).Value & "')"
b = b + 1
Loop
'log after commit
Print #1, " #log----------------------"
Dim c As Long
c = 2
Do While ws.Cells(c, 4).Value <> ""
Print #1, " command.expect('" & ws.Cells(7, 1).Value & "')"
Print #1, " command.sendline('" & ws.Cells(c, 4).Value & "')"
c = c + 1
Loop
Print #1, " command.expect('" & ws.Cells(7, 1).Value & "')"
Print #1, " #Return ---------------------------"
Print #1, " return"
Print #1, "# ================================================"
Print #1, "# === Main ======================================="
Print #1, "# --------------------------------------"
Print #1, "if (__name__ == '__main__'):"
Print #1, " Fnc_MAIN()"
Print #1, " print ('\nFinish Script.')"
Print #1, "# --------------------------------------"
Print #1, "# ================================================"
Close #1
MsgBox "Successfully completed"
End Sub
終わりに
看別人教程的時(shí)候最討厭代碼一貼完事的那種钮莲。。
輪到自己了也是多一行都不想寫(xiě)
感恩那些寫(xiě)了很詳細(xì)的說(shuō)明的博主彼水。臂痕。。