calling awk programs from python script












0















I am working on a script. I have bits and pieces what I am trying to bring them together, however....the script handling the login into devices and collecting the data is written in 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()


This will generate an output like...



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)


...then formatting of the data is done with awk against the 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


and the end result...



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


I tried to research how to bring awk and python together but general advise it should not be mixed. I have a couple of different awk modules and being very beginner with python, the whole splitting and sub-string is more then challenging for me. My final script will be bigger incorporating multiple awk calls, so once awk is done the control should be passed back to python script.



Thanks for the help!









share



























    0















    I am working on a script. I have bits and pieces what I am trying to bring them together, however....the script handling the login into devices and collecting the data is written in 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()


    This will generate an output like...



    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)


    ...then formatting of the data is done with awk against the 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


    and the end result...



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


    I tried to research how to bring awk and python together but general advise it should not be mixed. I have a couple of different awk modules and being very beginner with python, the whole splitting and sub-string is more then challenging for me. My final script will be bigger incorporating multiple awk calls, so once awk is done the control should be passed back to python script.



    Thanks for the help!









    share

























      0












      0








      0








      I am working on a script. I have bits and pieces what I am trying to bring them together, however....the script handling the login into devices and collecting the data is written in 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()


      This will generate an output like...



      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)


      ...then formatting of the data is done with awk against the 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


      and the end result...



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


      I tried to research how to bring awk and python together but general advise it should not be mixed. I have a couple of different awk modules and being very beginner with python, the whole splitting and sub-string is more then challenging for me. My final script will be bigger incorporating multiple awk calls, so once awk is done the control should be passed back to python script.



      Thanks for the help!









      share














      I am working on a script. I have bits and pieces what I am trying to bring them together, however....the script handling the login into devices and collecting the data is written in 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()


      This will generate an output like...



      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)


      ...then formatting of the data is done with awk against the 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


      and the end result...



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


      I tried to research how to bring awk and python together but general advise it should not be mixed. I have a couple of different awk modules and being very beginner with python, the whole splitting and sub-string is more then challenging for me. My final script will be bigger incorporating multiple awk calls, so once awk is done the control should be passed back to python script.



      Thanks for the help!







      awk scripting python database





      share












      share










      share



      share










      asked 4 mins ago









      peti27peti27

      62




      62






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509132%2fcalling-awk-programs-from-python-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509132%2fcalling-awk-programs-from-python-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          CARDNET

          Boot-repair Failure: Unable to locate package grub-common:i386

          濃尾地震