自己搭建自動化巡檢系統(三) 通過telnet實現遠程建立loopback

來源:互聯網
上載者:User

標籤:telnet   loopback   

我們在上一章完成了用代碼操作telnet,實現了遠端控制,分析前面的代碼會發現健壯性太低,需要進行最佳化,後續會通過開發一個互動式介面來完成人工介入操作網路的過程。本次實驗目的:實現自動化建立環回口

首先更新拓撲,拉出新的路由器和一台交換器

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M00/A3/64/wKioL1mkAQDBRfBqAAAffPh0MTQ421.jpg-wh_500x0-wm_3-wmp_4-s_825007326.jpg" title="image002.jpg" alt="wKioL1mkAQDBRfBqAAAffPh0MTQ421.jpg-wh_50" />

import telnetlib

import time

def main():

         Host="192.168.10.100"

         username="yerik"

         password="1111"

         commends=[‘enable‘,‘2222‘,‘conft‘]#相關指令

        

         tn= telnetlib.Telnet(Host,port=23,timeout=10)

         tn.set_debuglevel(2)

 

         #login input username

        

         tn.read_until(‘Username:‘) # 期待回複

         tn.write(username+‘\n‘)

 

         #input password

         tn.read_until(‘Password:‘) # 期待回複

         tn.write(password+‘\n‘)

 

         tn.read_until(‘R1>‘)

         forcommend in commends:

                   tn.write("%s\n"% commend)

         ‘‘‘

         tn.read_until(‘R1#‘)

         tn.write(‘configure‘)

         time.sleep(1)

         tn.write(‘t‘)‘‘‘

 

         tn.read_until(‘R1(config)#‘)

         loopback= ‘int lo‘

         ipaddr= ‘ 255.255.255.255‘

         exit= ‘exit‘

         fori in range(1,5):

                   loopbackn=loopback+str(i)

                   time.sleep(1)

                   tn.write("%s\n"% loopbackn)

                   ipaddrn=‘ipadd ‘+str(i)+‘.‘+str(i)+‘.‘+str(i)+‘.‘+str(i)+ipaddr

                   time.sleep(1)

                   tn.write("%s\n"% ipaddrn)

                   time.sleep(1)

                   tn.write("%s\n"% exit)

         tn.close()

 

if __name__==‘__main__‘:

         main()

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M01/A3/64/wKioL1mkARmD9yt5AAB7NUOojTw674.jpg" title="image004.jpg" alt="wKioL1mkARmD9yt5AAB7NUOojTw674.jpg" />

發現已經建立成功了

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M01/A3/64/wKioL1mkAS7Qo12WAAA4uHXqdeE500.jpg" title="image006.jpg" alt="wKioL1mkAS7Qo12WAAA4uHXqdeE500.jpg" />

由於此時還沒有運行路由協議,請問有什麼辦法可以ping到這些環回口?使用ping中的嚴格來源站點路由技術,通過R2pingR1 在特權介面中,輸入ping

ping

Protocol [ip]:

Target IP address: 2.2.2.2

Repeat count [5]:

Datagram size [100]:

Timeout in seconds [2]:

Extended commands [n]: y

Source address or interface: 192.168.10.102

Type of service [0]:

Set DF bit in IP header? [no]:

Validate reply data? [no]:

Data pattern [0xABCD]:

Loose, Strict, Record, Timestamp, Verbose[none]:Strict

Source route: 192.168.10.101

Loose, Strict, Record, Timestamp,Verbose[SV]:

Sweep range of sizes [n]:

Type escape sequence to abort.

Sending 5, 100-byte ICMP Echos to 2.2.2.2,timeout is 2 seconds:

Packet sent with a source address of192.168.10.102

Packet has IP options:  Total option bytes= 7, padded length=8

 Strict source route: <*>

  (192.168.10.101)

 

Reply to request 0 (20 ms).  Received packet has options

 Total option bytes= 8, padded length=8

 Strict source route:

  (192.168.10.101)

  <*>

 Endof list

 

Reply to request 1 (28 ms).  Received packet has options

 Total option bytes= 8, padded length=8

 Strict source route:

  (192.168.10.101)

  <*>

 Endof list

 

Reply to request 2 (24 ms).  Received packet has options

 Total option bytes= 8, padded length=8

 Strict source route:

  (192.168.10.101)

  <*>

 Endof list

 

Reply to request 3 (16 ms).  Received packet has options

 Total option bytes= 8, padded length=8

 Strict source route:

  (192.168.10.101)

  <*>

 End oflist

 

Reply to request 4 (28 ms).  Received packet has options

 Total option bytes= 8, padded length=8

 Strict source route:

  (192.168.10.101)

  <*>

 Endof list

 

Success rate is 100 percent (5/5),round-trip min/avg/max = 16/23/28 ms

現實可以ping通


本文出自 “yerik” 部落格,轉載請與作者聯絡!

自己搭建自動化巡檢系統(三) 通過telnet實現遠程建立loopback

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.