3

estoy realizando una conexión ssh con python 2.7 el cual ejecuto unos comandos y deseo obtener la respuesta de cada uno de ellos

COMANDOS

snmpwalk -v2c -cpublic localhost .111.2.25.1
snmpwalk -v2c -cpublic localhost .111.2.25.18
snmpwalk -v2c -cpublic localhost .111.2.25.23
snmpwalk -v2c -cpublic localhost .111.2.25.16
snmpwalk -v2c -cpublic localhost .111.2.25.1

EJEMPLO RESPUESTA COMANDOS introducir la descripción de la imagen aquí

NOTA: Lo que esta en amarillo no se debe tomar en cuenta, solo los running y up y lo contrario de cada uno de ellos

Ej.

running = failed

up = down

He intentado hacer esto:

import paramiko
import re
import datetime

fecha= datetime.date.today()
hoy= fecha.strftime("%Y%m%d")

mi_path = "/usr/local/apache2/htdocs/web/sistema/uploads/comandos/SUB_PROCESO_"+hoy+".txt"

contador    = 0
arreglo     = []

comandos   = ['snmpwalk -v2c -cpublic localhost .111.2.25.1',
                    'snmpwalk -v2c -cpublic localhost .111.2.25.18',
                    'snmpwalk -v2c -cpublic localhost .111.2.25.23',
                    'snmpwalk -v2c -cpublic localhost .111.2.25.16',
                    'snmpwalk -v2c -cpublic localhost .111.2.25.1']

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("123.369.3.1", username="root", password="")

for j in comandos:
    stdin, stdout, stderr = client.exec_command(format(j))
    stdin.flush()
    salida = stdout.read()
    for trozo in salida.strip().split("SIPWISE-NGCP-MONITOR-MIB::"):
        running_match   = re.findall("running", trozo)
        up_match        = re.findall("up", trozo)
        failed_match    = re.findall("failed", trozo)
        down_match      = re.findall("down", trozo)
        if running_match:
            running = running_match[0]
            print j
        elif up_match:
            up = up_match[0]
        elif failed_match:
            failed = failed_match[0]
        elif down_match:
            down = down_match[0]
    contador += 1

with open(mi_path, "w") as f:
    for linea in arreglo:
        f.writelines(";".join(linea))
        f.write("\n")

Deseo obtener en el SUB_PROCESO_.txt los resultados separados por (;)

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.