вызов программ awk из скрипта python - PullRequest
0 голосов
/ 28 марта 2019

Я работаю над сценарием.У меня есть кусочки того, что я пытаюсь собрать вместе, однако .... скрипт, обрабатывающий вход в систему на устройствах и собирающий данные, написан на python.

#!/usr/bin/env python
from __future__ import print_function
from netmiko import Netmiko
from getpass import getpass
import sys
import re

# Prompts for username and password
p = getpass("Enter password: ")

# Creates empty list to be used later n script for device dictionaries
devices = []

# Creates list of ip addresses of devices
ips = ["ip1", "ip2"]

# Loop used to generate device dictionaries from ips list and
# stores dictionary entry in devices list
for ip in ips:
    cisco_asa = {
        "device_type": "cisco_asa",
        "host": ip,
        "username": "python",
        "password": p,
    "secret": p,
    }
    devices.append(cisco_asa)

cmd_system = "changeto system"
cmd_nopager = "terminal pager 0"
cmd_failip = "sh failover | in \)\:|host"

for device in devices:
    net_connect = Netmiko(**device)
    net_connect.enable()
    print(net_connect.find_prompt())
    output1 = net_connect.send_command(cmd_system)
    output2 = net_connect.send_command(cmd_nopager)
    print(net_connect.find_prompt()+cmd_name)
    output = net_connect.send_command(cmd_name)
    print(output) 
    net_connect.disconnect()  

Это сгенерирует вывод наподобие ...

TESTASA1#sh failover | in \)\:|host
        This host: Secondary - Active
                  admin Interface dmz (10.149.41.1): Normal (Not-Monitored)
                  admin Interface admin (10.149.41.24): Normal (Not-Monitored)
                  INTER Interface outside (164.54.212.23): Normal (Not-Monitored)
                  INTER Interface inside (10.149.51.77): Normal (Not-Monitored)
        Other host: Primary - Standby Ready
                  admin Interface dmz (10.149.41.2): Normal (Not-Monitored)
                  admin Interface admin (10.149.41.23): Normal (Not-Monitored)
                  INTER Interface inside (10.149.51.78): Normal (Not-Monitored)
                  INTER Interface outside (164.54.212.24): Normal (Not-Monitored)

TESTASA2#sh failover | in \)\:|host
        This host: Secondary - Active
                  admin Interface dmz (10.149.41.1): Normal (Not-Monitored)
                  admin Interface admin (10.149.41.24): Normal (Not-Monitored)
                  INTER Interface outside (164.54.212.23): Normal (Not-Monitored)
                  INTER Interface inside (10.149.51.77): Normal (Not-Monitored)
        Other host: Primary - Standby Ready
                  admin Interface dmz (10.149.41.2): Normal (Not-Monitored)
                  admin Interface admin (10.149.41.23): Normal (Not-Monitored)
                  INTER Interface inside (10.149.51.78): Normal (Not-Monitored)
                  INTER Interface outside (164.54.212.24): Normal (Not-Monitored)

... тогда форматирование данных будет выполнено с помощью awk для python_output

#!/bin/sh
awk  '
BEGIN  {FS="[)#/(:]"; OFS = ",";fw="";dev=""}
{
        if ($0~/#/) fw=$1 ;
        if (($0~/host/)&&($0~/Primary - Active/)) dev=fw"/pri/act";
        if (($0~/host/)&&($0~/Primary - Standby/)) dev=fw"/pri/stdby";
        if (($0~/host/)&&($0~/Secondary - Active/)) dev=fw"/sec/act";
        if (($0~/host/)&&($0~/Secondary - Standby/)) dev=fw"/sec/stdby";
        if ($0~/\)\:/) {
                print $2,dev,substr($1,match($1, "face")+5,length($1))
                }
}' python_output >> datadb.csv

и конечный результат ...

10.149.41.1,TESTASA1/sec/act,dmz 
10.149.41.24,TESTASA1/sec/act,admin 
164.54.212.23,TESTASA1/sec/act,outside

Я пытался исследовать, как соединить awk и python, но общий совет не следует смешивать.У меня есть пара различных модулей awk, и я очень начинающий с python, и все разделение и подстрока для меня более чем сложны.Мой последний скрипт будет больше и будет включать несколько вызовов awk, поэтому, когда awk будет выполнен, управление должно быть передано обратно в скрипт python.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...