paramiko出现问题:将命令发送到终端,执行该命令,然后终端通过新命令send

时间:2019-09-11 13:25:52

标签: python paramiko

早上好

我是 Paramiko 的新手,并且正在使用它。我有问题,我找不到解决办法。

问题是我必须连续执行多个命令,然后执行第一个命令,一切都很好。我得到了数据并进行了处理,但是碰巧当我执行第二条命令时,通道继续执行我的第一条命令,就像它存储在缓存中一样(这是我能提供的最好的描述),在下面:

我执行第一个命令:

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>

执行第二条命令时可以看到,我以某种方式重新执行了第一条命令,只有多次执行完第一条命令后,我才执行最后一条命令。

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

我一直在寻找Internet上的信息,但没有找到太多,而且我在这个 paramiko 主题中很新

1 个答案:

答案 0 :(得分:0)

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

使用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_ready并为终端的响应安排相应的等待时间。

我希望这对其他人有帮助。