4

Так работает:

    try:
        connector()
        stdin, stdout, stderr = client.exec_command('ifconfig')
        data = stdout.read() + stderr.read()
        print(data)

    except Exception as e:
        error_log = str(e)
        print('error_log')
file.close()

Так тоже работает:

    if a1 in str(data):
        stdin, stdout, stderr = client.exec_command('ifconfig')
        data = stdout.read() + stderr.read()
        print(data)
    if a2 in str(data):
        stdin, stdout, stderr = client.exec_command('ls')
        data = stdout.read() + stderr.read()
        print(data)
file.close()

А как их совместить? Что бы if был внутри try.. except.

1
  • 2
    Просто вставить if'ы внутрь try-блока. 22 июл 2018 в 4:57

1 ответ 1

1

попройте так:

def cli_exec(client, cmd):
    try:
        #connector()
        stdin, stdout, stderr = client.exec_command(cmd)
        data = stdout.read() + stderr.read()
        return data
    except Exception as e:
        error_log = str(e)
        print('error_log')
    finally:
        return None

if a1 in str(data):
    data = cli_exec(client, 'ifconfig')
if a2 in str(data):
    data = cli_exec(client, 'ls')
2
  • можно один вызов: data = get_output(client, command) где command в if-ветках определяется.
    – jfs
    22 июл 2018 в 13:01
  • @jfs, можно и так... 22 июл 2018 в 13:02

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.