Python 스크립트에서 이상한 출력이 나타납니다.
Paramiko와 관련된 실제 실행을 위해 루프에서 코드를 실행할 때 출력 시작 부분을 덮어 쓰면서 인쇄 할 변수의 순서를 반대로하는 것처럼 보입니다.
처리 할 모든 소스 텍스트는 둘 다 동일합니다. 하나는 라우터에서 ssh를 통해 Paramiko를 통해 가져오고 다른 하나는 수동으로 가져 와서 로컬 변수로 붙여 넣었습니다.
전류 출력 :
with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
정적 로컬 변수가있는 원하는 출력 및 출력 :
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
나는 이것이 왜 일어나고 있는지 세상적인 아이디어가 없습니다. 자세한 정보 및 실행은 아래를 참조하십시오. 도움?
암호:
import paramiko
router_channel.send('sh ip bgp 10.23.24.32\n')
buff = ''
best_test =()
while not buff.endswith('#'):
resp = router_channel.recv(99999)
buff += resp
#DMVPN variable Declaration.
bgp_show = buff
bgp_list = bgp_show.split('Local')
bgp_list.pop(0)
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
best_test = best_list[1]
print '********'
print buff
print '********'
print "Best Path is " + best_test + " with " + "test"
print '********'
코드 실행 :
dirp@localhost Test Python Scripts]$ python splice.py
********
sh ip bgp 10.23.24.32
BGP routing table entry for 10.23.24.32/28, version 367886
Paths: (6 available, best #5, table default)
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 3
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
router#
********
With testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
********
정적 코드 :
bgp_list = bgp.split('Local')
bgp_list.pop(0)
#pprint.pprint( bgp_list )
#print bgp_list[2]
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
print '********'
print bgp
print '********'
print "Best Path is " + best_list[1] + ' with' + local_pref
print '********'
루프에 넣지 않고 로컬로 변수를 할당 할 때 출력됩니다.
derp@localhost Test Python Scripts]$ python splitty1.py
********
sh ip bgp 10.23.24.32 | exclude local
BGP routing table entry for 10.23.24.32/28, version 122378
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.72 from 10.8.111.72 (10.111.72.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 2
Local
10.8.11.73 from 10.9.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local
10.8.11.73 from 10.8.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 115, valid, internal
rx pathid: 0, tx pathid: 0
********
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
********
[derp@localhost Test Python Scripts]$
router_channel.recv
Windows 형식의 텍스트를 반환하며 각 줄은 '\r\n'
. 하지만 x.split('\n')
. 즉, 모든 줄 (마지막 줄 제외)이 '\r'
.
Windows가 아닌 시스템에서를 인쇄 '\r'
하면 커서가 행을 진행하지 않고 열 1로 다시 이동합니다. 따라서 이렇게하면 :
print "Best Path is " + best_test + " with " + "test"
인쇄 중입니다.
Best Path is blah blah blah\r with test
다음과 같이 나옵니다.
with testis blah blah blah
가장 간단한 해결책은 str.splitlines()
대신 방법 을 사용하는 것입니다 str.split('\n')
. 자동으로 Windows 줄 끝을 고려합니다.
줄 경계에서 분리 된 문자열의 줄 목록을 반환합니다. 이 방법은 줄을 분할하기 위해 보편적 인 개행 방식을 사용합니다 .
그리고 링크를 따라 가면 범용 개행을 설명합니다.
다음 모두가 줄의 끝으로 인식되는 텍스트 스트림을 해석하는 방법 : Unix 줄 끝 규칙
'\n'
, Windows 규칙'\r\n'
및 이전 Macintosh 규칙'\r'
…
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다