Python中的连接顺序错误并覆盖了String的一部分

时间:2014-09-29 21:18:41

标签: python loops concatenation paramiko overwrite

我从Python脚本获得了一个奇怪的输出

当在循环中运行代码以实现涉及Paramiko的实际执行时,它似乎颠倒了要打印的变量的顺序,同时覆盖了输出的beguinning ..

要处理的所有源文本都是相同的。一个是通过路由器从sram上获取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]$ 

2 个答案:

答案 0 :(得分:4)

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行结尾:

  

返回字符串中的行列表,在行边界处断开。此方法使用universal newlines方法分割线...

如果您点击该链接,它会解释通用换行符:

  

一种解释文本流的方式,其中所有以下内容都被识别为一行:Unix行尾约定'\n',Windows约定'\r\n'和旧的Macintosh约定'\r' ...

答案 1 :(得分:0)

这些线条让我感到不安 -

for x in best_list:
        best_list = [x.strip(' ') for x in best_list]

您显然通过best_list中的值修改了循环内best_list的值。即使你认为你知道那是做什么的,我也不会。

尝试为"内部"使用不同的变量名称。 best_list,看看是否有任何修复。