Bil*_*iAm 2 python loops concatenation overwrite paramiko
我从 Python 脚本中得到一个奇怪的输出
当在循环中运行代码以进行涉及 Paramiko 的实际执行时,它似乎颠倒了要打印的变量的顺序,同时覆盖了输出的开始。
要处理的所有源文本对于两者都是相同的。一个是通过 Paramiko 通过 ssh 从路由器抓取的,另一个是我手动抓取并粘贴为局部变量。
电流输出:
with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
Run Code Online (Sandbox Code Playgroud)
具有静态局部变量的所需输出和输出:
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会这样。请参阅下文了解更多信息和执行。帮助?
代码:
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 '********'
Run Code Online (Sandbox Code Playgroud)
代码的执行:
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)
********
Run Code Online (Sandbox Code Playgroud)
静态代码:
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 '********'
Run Code Online (Sandbox Code Playgroud)
不将其放入循环而是在本地分配变量时的输出:
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]$
Run Code Online (Sandbox Code Playgroud)
router_channel.recv正在返回 Windows 格式的文本,其中每一行都以'\r\n'. 但是您使用x.split('\n'). 这意味着每一行(可能除了最后一行)都将以'\r'.
在非 Windows 系统上,如果您打印出 ,则会'\r'将光标移回第 1 列而不前进该行。所以,当你这样做时:
print "Best Path is " + best_test + " with " + "test"
Run Code Online (Sandbox Code Playgroud)
您正在打印:
Best Path is blah blah blah\r with test
Run Code Online (Sandbox Code Playgroud)
出来的结果是:
with testis blah blah blah
Run Code Online (Sandbox Code Playgroud)
最简单的解决方案是使用str.splitlines()方法而不是str.split('\n')。这会自动考虑 Windows 行结尾:
返回字符串中的行列表,在行边界处断开。这种方法使用通用换行方法来分割行......
如果您点击链接,它会解释通用换行符:
一种解释文本流的方式,其中将以下所有内容识别为行结束:Unix 行尾约定
'\n'、Windows 约定'\r\n'和旧的 Macintosh 约定'\r'……