从python脚本调用awk程序

时间:2019-03-28 05:10:09

标签: awk scripting python database

我正在编写脚本。我有些零散的东西,但是我试图将它们组合在一起。...处理登录到设备并收集数据的脚本是用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脚本。

0 个答案:

没有答案