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

2024-06-28 19:10:02 发布

您现在位置:Python中文网/ 问答频道 /正文

早上好

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

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

我执行我的第一个命令:

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>

正如你所看到的,当我执行第二个命令时,不知何故我重新执行了第一个命令,只有在多次执行第一个命令之后,我才执行最后一个命令。你知道吗

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

我在互联网上寻找信息,但没有找到太多,我对帕拉米科这个话题非常陌生


Tags: 命令includedisplayphydescriptionshellinterfacedown
1条回答
网友
1楼 · 发布于 2024-06-28 19:10:02

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

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

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

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)

如果count==1,再次发送命令的部分是因为虽然我在第一个实例中发送了命令,但终端似乎在某些时刻没有响应,因此我必须再次执行它,但实际上它只是针对我的情况的代码

为了得到我的第二个答案也是同样的过程,执行recv\u ready并为终端的响应设置相应的等待时间。你知道吗

我希望这能帮助别人。你知道吗

相关问题 更多 >