Python Forum
Sending Advanced Commands with Netmiko
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending Advanced Commands with Netmiko
#1
I think that I may be in way over my head with this project. I am pretty new at python coding but I am having a lot of fun with it. In this automation project I am trying to ssh (using netmiko) into a list of IP addresses, open and read configuration files, post the results to a csv in the devices I am SSH'ing from, then close the ssh connection. I have gotten the csv portion working on a single computer:

from datetime import datetime
import csv
#import os - for future use
import socket

# provide a list of IP addresses to scan
# Will add ssh capabilities later...commenting out
# ip_addresses = [10.252.247.141, 10.252.247.142]

hostname = (socket.gethostname())

# create the output file int the tmp directory for the results of the checks and open so it can be written to
with open('tmp/config_check_' + str(datetime.now().strftime('%Y_%m_%d_%H_%M')) + '.csv', 'w') as output_file:
    fieldnames = ['Hostname', 'List', 'Result']
    writer = csv.DictWriter(output_file, fieldnames=fieldnames)
    writer.writeheader()

    # check for martian packet setting
    martian_setting = ('martians = 0')
    sysctl_file = open('/etc/sysctl.conf', 'r')

    if martian_setting in sysctl_file.read():
        result = 'yes'
    else:
        result = 'no'

    martian_results = (hostname, martian_setting, result)
    results_writer = csv.writer(output_file, delimiter=',', lineterminator='\n')
    results_writer.writerow(martian_results)

    sysctl_file.close()

    # check for message size settings
    message_settings = ('discoveryagent.message.maxMessageSize=1572864', 'discoveryagent.message.maxTimeBetweenMessageSend=180',
    'discoveryagent.target.queue.maxSize=30000')
    messagesize_file = open('/usr/local/lumeta/discovery-agent/discovery-agent.properties', 'r')

    for message in message_settings:
        if message in messagesize_file.read():
            result = 'yes'
        else:
            result = 'no'

        message_results = (hostname, martian_setting, result)
        results_writer = csv.writer(output_file, delimiter=',', lineterminator='\n')
        results_writer.writerow(message_results)

    messagesize_file.close()

    # Check fips setting
    fips_settings = ('fips=0')
    fips_file = open('/boot/grub/grub.conf', 'r')

    if fips_settings in fips_file.read():
        result = 'yes'
    else:
        result = 'no'

    fips_results = (hostname, martian_setting, result)
    results_writer = csv.writer(output_file, delimiter=',', lineterminator='\n')
    results_writer.writerow(fips_results)

    fips_file.close()

    # check utc setting
    utc_setting = ('UTC')
    utc_file = open('/etc/sysconfig/clock', 'r')

    if utc_setting in utc_file.read():
        result = 'yes'
    else:
        result = 'no'

    utc_results = (hostname, martian_setting, result)
    results_writer = csv.writer(output_file, delimiter=',', lineterminator='\n')
    results_writer.writerow(utc_results)

    utc_file.close()
I can also get netmiko working and send simple one line commands but I dont know how to open the csv on the original device, send the commands via netmiko, and then write the results back to the csv. I am not sure that this can even be done using netmiko. Thanks in advance for the help!

-Josh
Reply
#2
I figured out how to do this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have an issue with Netmiko Error reading SSH protocol banner omarhegazy 2 3,510 May-16-2022, 06:05 PM
Last Post: omarhegazy
  Sending string commands from Python to a bluetooth device Rovelin 13 9,272 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  Netmiko Loop question sc00ter 2 3,260 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  Netmiko - add multiple files, from different folders to mail. tomikovaknin 5 5,299 Nov-26-2017, 12:55 PM
Last Post: heiner55
  where to find advanced level guides Skaperen 3 4,780 Dec-06-2016, 08:10 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020