Python paramiko的问题:向终端发送一个命令,它被执行,终端在一个新的命令Send中再次执行它

Python paramiko的问题:向终端发送一个命令,它被执行,终端在一个新的命令Send中再次执行它,python,paramiko,Python,Paramiko,早上好 我是新来的帕拉米科,我正在和它一起工作。我有个问题,我找不到解决办法 问题是我必须在一行中执行几个命令,我执行我的第一个命令,一切都很好。我获取数据并进行处理,但当我执行第二个命令时,通道会继续执行我的第一个命令,就像它存储在缓存中一样(这是我能给出的最佳描述),我将其显示在下面: 我执行我的第一个命令: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Si no

早上好

我是新来的帕拉米科,我正在和它一起工作。我有个问题,我找不到解决办法

问题是我必须在一行中执行几个命令,我执行我的第一个命令,一切都很好。我获取数据并进行处理,但当我执行第二个命令时,通道会继续执行我的第一个命令,就像它存储在缓存中一样(这是我能给出的最佳描述),我将其显示在下面:

我执行我的第一个命令:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Si no encuentra el host, lo agegra automaticamente
ssh.connect('xxxxxxxxxxx', username='xxxxxxxx', password='xxxxxxx.',port='22',timeout=500) #Iniciamos la conexion
shell=ssh.invoke_shell() 

shell.send('display interface description | include IP7'+'\n\r') #Envio mi comando

while count < 2:

    resp = shell.recv(9999)
#   print(resp)
    buff_string += str(resp,'utf-8',errors='ignore') 
#   print(buff_string)
    patron=re.compile(f'<{equipo}>') 
    result=patron.findall(buff_string)  
    count=len(result)  
#   print(count)

    if(count==1):
#         shell.send('screen-length 0 temporary'+'\n\r') 
        shell.send('display interface description | include IP7'+'\n\r') 

print('\n'+'La Primera Salida es:')
print(buff_string +'\n' )

<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>

res=''
count2=0;
cadena='';
shell.send('display ospf peer GigabitEthernet1/2/7 | i State'+'\n\r') #Envio mi comando

while count2 < 10:
    res = shell.recv(9999) 
#    print(resp)
    cadena += str(res,'utf-8',errors='ignore')
#   print(buff_string)
    patron=re.compile(f'<{equipo}>') 
    result2=patron.findall(cadena)  
    count2=len(result2)  
#   print(count)

    if(count2==1):  
           shell.send('display ospf peer GigabitEthernet1/2/7 

print(cadena)

<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>
<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>
<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>
<PNE5MCY01>display ospf peer GigabitEthernet1/2/7 | i State

          OSPF Process 100 with Router ID 10.18.219.39
                Neighbors

 Area 0.0.0.0 interface 10.53.209.13 (GE1/2/7)'s neighbors
   State: Full      Mode:Nbr is Master   Priority: 1
<PNE5MCY01>

ssh=paramiko.SSHClient()
ssh.set_-missing_-host_-key_-policy(paramiko.AutoAddPolicy())#Si-no-encumentra-el-host,lo-agegra-automaticamente
ssh.connect('xxxxxxxxxx',用户名='xxxxxxxxx',密码='xxxxxxx',端口='22',超时=500)#
shell=ssh.invoke_shell()
shell.send('display interface description | include IP7'+'\n\r')#Envio mi comando
当计数小于2时:
resp=外壳回收率(9999)
#打印(resp)
buff_string+=str(resp,'utf-8',errors='ignore')
#打印(浅黄色字符串)
用户=重新编译(f“”)
结果=用户findall(buff_字符串)
计数=len(结果)
#打印(计数)
如果(计数=1):
#shell.send('屏幕长度为0的临时'+'\n\r')
shell.send('显示接口说明|包括IP7'+'\n\r')
打印('\n'+'La Primera Salida es:')
打印(buff_字符串+'\n')
显示界面说明|包括IP7
物理物理
*向下:管理上向下
^停机:待命
(l) :环回
(s) :欺骗
(E) :E-Trunk向下
(b) :BFD向下
(B) :位错误检测关闭
(e) :我下来了
(d) :抑制阻尼
(p) :端口警报关闭
(dl):DLDP下降
(左):链路心跳下降
接口物理层协议描述
GE1/2/7上行连接a YMTSOMCY7750SR702 1/1/5(服务1711 1Gbps)IP7BTO01 Gi7/0/0
GE1/2/8上行连接a IP7MCY01 Gi1/0/1
GE1/2/9上行连接a YMTSOMCY7750SR702/1/6(服务608 1Gbps)IP7VAL01 Gi4/0/1
现在我将执行第二个命令,您将得到以下结果:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Si no encuentra el host, lo agegra automaticamente
ssh.connect('xxxxxxxxxxx', username='xxxxxxxx', password='xxxxxxx.',port='22',timeout=500) #Iniciamos la conexion
shell=ssh.invoke_shell() 

shell.send('display interface description | include IP7'+'\n\r') #Envio mi comando

while count < 2:

    resp = shell.recv(9999)
#   print(resp)
    buff_string += str(resp,'utf-8',errors='ignore') 
#   print(buff_string)
    patron=re.compile(f'<{equipo}>') 
    result=patron.findall(buff_string)  
    count=len(result)  
#   print(count)

    if(count==1):
#         shell.send('screen-length 0 temporary'+'\n\r') 
        shell.send('display interface description | include IP7'+'\n\r') 

print('\n'+'La Primera Salida es:')
print(buff_string +'\n' )

<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>

res=''
count2=0;
cadena='';
shell.send('display ospf peer GigabitEthernet1/2/7 | i State'+'\n\r') #Envio mi comando

while count2 < 10:
    res = shell.recv(9999) 
#    print(resp)
    cadena += str(res,'utf-8',errors='ignore')
#   print(buff_string)
    patron=re.compile(f'<{equipo}>') 
    result2=patron.findall(cadena)  
    count2=len(result2)  
#   print(count)

    if(count2==1):  
           shell.send('display ospf peer GigabitEthernet1/2/7 

print(cadena)

<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>
<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>
<PNE5MCY01>display interface description | include IP7
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(E): E-Trunk down
(b): BFD down
(B): Bit-error-detection down
(e): ETHOAM down
(d): Dampening Suppressed
(p): port alarm down
(dl): DLDP down
(lh): link heartbeat down
Interface                     PHY     Protocol Description                     
GE1/2/7                       up      up       Conexion a YMTSOMCY7750SR702 1/1/5 (Servicio 1711 1Gbps) IP7BTO01 Gi7/0/0
GE1/2/8                       up      up       Conexion a IP7MCY01 Gi1/0/1     
GE1/2/9                       up      up       Conexion a YMTSOMCY7750SR702 2/1/6 (Servicio 608 1Gbps) IP7VAL01 Gi4/0/1
<PNE5MCY01>
<PNE5MCY01>display ospf peer GigabitEthernet1/2/7 | i State

          OSPF Process 100 with Router ID 10.18.219.39
                Neighbors

 Area 0.0.0.0 interface 10.53.209.13 (GE1/2/7)'s neighbors
   State: Full      Mode:Nbr is Master   Priority: 1
<PNE5MCY01>

res=''
count2=0;
卡德纳='';
shell.send('显示ospf对等千兆以太网1/2/7 | i State'+'\n\r')#Envio mi comando
当count2<10时:
res=shell.recv(9999)
#打印(resp)
cadena+=str(res,'utf-8',errors='ignore')
#打印(浅黄色字符串)
用户=重新编译(f“”)
结果2=芬德尔赞助人(卡德纳)
count2=len(结果2)
#打印(计数)
如果(count2==1):
shell.send('显示ospf对等千兆以太网1/2/7
印刷品(卡德纳)
显示界面说明|包括IP7
物理物理
*向下:管理上向下
^停机:待命
(l) :环回
(s) :欺骗
(E) :E-Trunk向下
(b) :BFD向下
(B) :位错误检测关闭
(e) :我下来了
(d) :抑制阻尼
(p) :端口警报关闭
(dl):DLDP下降
(左):链路心跳下降
接口物理层协议描述
GE1/2/7上行连接a YMTSOMCY7750SR702 1/1/5(服务1711 1Gbps)IP7BTO01 Gi7/0/0
GE1/2/8上行连接a IP7MCY01 Gi1/0/1
GE1/2/9上行连接a YMTSOMCY7750SR702/1/6(服务608 1Gbps)IP7VAL01 Gi4/0/1
显示界面说明|包括IP7
物理物理
*向下:管理上向下
^停机:待命
(l) :环回
(s) :欺骗
(E) :E-Trunk向下
(b) :BFD向下
(B) :位错误检测关闭
(e) :我下来了
(d) :抑制阻尼
(p) :端口警报关闭
(dl):DLDP下降
(左):链路心跳下降
接口物理层协议描述
GE1/2/7上行连接a YMTSOMCY7750SR702 1/1/5(服务1711 1Gbps)IP7BTO01 Gi7/0/0
GE1/2/8上行连接a IP7MCY01 Gi1/0/1
GE1/2/9上行连接a YMTSOMCY7750SR702/1/6(服务608 1Gbps)IP7VAL01 Gi4/0/1
显示界面说明|包括IP7
物理物理
*向下:管理上向下
^停机:待命
(l) :环回
(s) :欺骗
(E) :E-Trunk向下
(b) :BFD向下
(B) :位错误检测关闭
(e) :我下来了
(d) :抑制阻尼
(p) :端口警报关闭
(dl):DLDP下降
(左):链路心跳下降
接口物理层协议描述
GE1/2/7上行连接a YMTSOMCY7750SR702 1/1/5(服务1711 1Gbps)IP7BTO01 Gi7/0/0
GE1/2/8上行连接a IP7MCY01 Gi1/0/1
GE1/2/9上行连接a YMTSOMCY7750SR702/1/6(服务608 1Gbps)IP7VAL01 Gi4/0/1
显示ospf对等千兆以太网1/2/7|i状态
路由器ID为10.18.219.39的OSPF进程100
邻居
区域0.0.0.0接口10.53.209.13(GE1/2/7)的邻居
状态:完全模式:Nbr为主优先级:1
正如您在执行第二个命令时所看到的,不知何故,我重新执行了第一个命令,并且只有在多次执行第一个命令之后,我才执行最后一个命令

在那里看到的循环(where)用于存储数据,但我不知道这是否会产生影响,因为在执行第二个命令时,第一个命令不应该出现


我在互联网上寻找信息,但没有找到太多,我对这个paramiko主题非常陌生

我通过更改代码以接收数据来解决问题,这有助于提高代码的响应,并且命令的执行不再重复出现

用于存储我的响应,考虑到在执行命令后,应该给它一个

我存储第一个响应的代码是:

shell.send('display interface description | include PED'+'\r\n') 
sleep(1)                                        
 while shell.recv_ready() == True:                
       buff_string += str(shell.recv(9999),'utf-8',errors='ignore') 
       patron=re.compile(f'<{equipo}>')
       result=patron.findall(buff_string)
       count=len(result)                    
 #     print(count)                      
       if(count==1):                     
          shell.send('screen-length 0 temporary'+'\n\r')                          
          shell.send('display interface description | include PED'+'\r\n')  
          sleep(1)
shell.s