沉迷了好久的手游。。。差不多是时候务一下正业了。
目标
自动化网络机器配置
工具
python
VBA
必须要吐槽一下我走过的那些弯路
由于完全不知道该用什么模块,边查边学,每次都是撞一鼻子灰再换个方向,鼻子都快给撞平了。。
最开始查到的是ConfigParser和paramiko
ConfigParser读取登录信息(用户名,密码,接口番号,ip地址),paramiko建立链接并send show命令。。。到此为止都很美好,直到发现paramiko不识别set,delete,show |compare,commit....
然后发现juniper有个自制的PyEZ,都带EZ了肯定很好用对吗!!
import的时候发现依存模块lxml的etree加载不了。。。至今仍是悬案。。。
也不能总在死路里打转。。。所以又开始找别的代替模块。。
皇天不负有心人。。。
让我找到了pexpect。。。就直接发送文字列 简单粗暴。。。
虽然感觉写出来的东西毫无优雅可言。。。
总之。。。能用就成。。。。(自暴自弃)
先写好python的部分
※[ ]中的部分可以任意修改为自己需要的数值
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#=============================
import sys
import pexpect
#=============================
def Fnc_MAIN():
print 'starting'
#ssh链接,logfile=sys.stdout用于返回命令执行结果
ipaddr=sys.argv[1]
command=pexpect.spawn('ssh [用户名]@'+ipaddr,logfile=sys.stdout)
command.expect('Password')
command.sendline('[密码]')
command.expect('[host名]>')
#事前log
command.sendline('[随便写点show命令]')
command.expect('[host名]>')
command.sendline('[随便写点show命令]')
command.expect('[host名]>')
command.sendline('[随便写点show命令]')
command.expect('[host名]>')
command.sendline('[随便写点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
(和事前log一样,懒得写了。。)
#Return ---------------------------
return
# ================================================
# === Main =======================================
# --------------------------------------
if (__name__ == '__main__'):
Fnc_MAIN()
print ('\nFinish Script.')
# --------------------------------------
# ================================================
不要问我为什么不用loop。。。
你试试看。。。
我也不知道为什么pexpect好像看不懂loop。。。
VBA
由于这个脚本并不是那么自动。。所以决定用VBA来创建脚本
在EXCEL中输入用户名,密码,命令行等信息,然后依据输入值创建.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
終わりに
看别人教程的时候最讨厌代码一贴完事的那种。。
轮到自己了也是多一行都不想写
感恩那些写了很详细的说明的博主。。。