Python 通過telnet 配置思科網路裝置

來源:互聯網
上載者:User

標籤:MF   alt   ==   成功   網路裝置   cmd   add   proc   oss   

!!使用了 telnetlib 庫
1 拓撲

 
未運行指令碼的時候,R4、R5是沒有到1.1.1.1和2.2.2.2的路由的:

 
配置文檔放在跟python指令碼同一個目錄下:

 
運行python指令碼之後:
-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=" alt="Python 通過telnet 配置思科網路裝置" />


成功!

 

#conf.py 檔案import timefrom telnetlib import Telnetdef cfg(addr,user,pwd,secret,conf):        tn = Telnet(addr)        tn.write(user+‘\n‘)        tn.write(pwd+‘\n‘)        tn.write(‘enable\n‘)        tn.write(secret+‘\n‘)        tn.write(‘terminal length 0\n‘)        time.sleep(1)        tn.write(‘conf t\n‘)        time.sleep(1)        confp = open(conf,‘r‘)        for cmd in confp:          tn.write(cmd)    #應為讀一行的時候已經有分行符號了,所以這裡就不添加+‘\n‘了          print(cmd)       #用於查看讀取的命令          time.sleep(1)    #建議每條命令都休眠一下,不然可能配置不了        confp.close()if __name__ == "__main__":        fp = open(‘./ip.txt‘,‘r‘)    #如果有多台主機要配置同樣的命令的話,可以將主機IP都放在一個文檔中        for ip in fp:          print("configuring "+ip.strip())          conf = cfg(ip.strip(),‘cisco‘,‘cisco‘,‘cisco‘,‘./conf.txt‘)          print(ip.strip()+‘ was finished!‘)        print(‘done!‘)        fp.close()

 
#ip.txt //按需添加
172.16.1.4
172.16.2.5

 
#conf.txt //按需添加
ip route 1.1.1.1 255.255.255.255 f0/0
ip route 2.2.2.2 255.255.255.255 f0/0
do write

Python 通過telnet 配置思科網路裝置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.