![]() |
Netmiko - add multiple files, from different folders to mail. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Netmiko - add multiple files, from different folders to mail. (/thread-6496.html) |
Netmiko - add multiple files, from different folders to mail. - tomikovaknin - Nov-25-2017 Hi all im trying to get some reports from my network devices with netmiko. i implement script that log into list of devices and runs commands from list of commands. now, i add all to be export to mail. the issue, its send me just the output from the last directory. this is my code #!/usr/bin/env python #This script log to list of devices, run lists of commands and send all results to mail from __future__ import absolute_import, division, print_function import json import netmiko import os import sys import signal import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEBase import MIMEBase from email import Encoders signal.signal(signal.SIGPIPE, signal.SIG_DFL) # IOError: Broken pipe signal.signal(signal.SIGINT, signal.SIG_DFL) #KeyboardInterrupt: Ctrl-C if len(sys.argv) < 3: print('Usage: cmdrunner.py commands.txt devices.json') exit() netmiko_exceptions = (netmiko.ssh_exception.NetMikoAuthenticationException, netmiko.ssh_exception.NetMikoTimeoutException) with open(sys.argv[1]) as cmd_file: commands = cmd_file.readlines() with open(sys.argv[2]) as dev_file: devices = json.load(dev_file) for device in devices: try: print('~'*40) print('connection to device', device['ip']) connection = netmiko.ConnectHandler(**device) newdir = connection.base_prompt for command in commands: filename = command.replace(' ', '_') + '.txt' filename = '/'.join((newdir, filename)) with open(filename, 'w') as out_file: out_file.write(connection.send_command(command) + '\n') filenames = [os.path.join(newdir, f) for f in os.listdir(newdir)] connection.disconnect() except netmiko_exceptions as e: print('Failed to ', device['ip'], e) fromaddr = "server ip" toaddr = "myip" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "TEST" body = "TEST" msg.attach(MIMEText(body, 'plain')) #files = newdir #filenames = [os.path.join(files, f) for f in os.listdir(files)] for file in filenames: part = MIMEBase('application', 'octet-stream') part.set_payload(open(file, 'rb').read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % file) msg.attach(part) server = smtplib.SMTP('smtp-ip') text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit() RE: Netmiko - add multiple files, from different folders to mail. - heiner55 - Nov-26-2017 We can help you better, if we can try your program on our PCs. So I have a Linux PCs and a printer, router, ... Can you give an sample, how I can use your code ? RE: Netmiko - add multiple files, from different folders to mail. - tomikovaknin - Nov-26-2017 Hi heiner, thanks for your reply. you need to install netmiko: Multi-vendor library to simplify Paramiko SSH connections to network devices. https://github.com/ktbyers/netmiko then, to create 2 files: 1. devices list. 2. commands list. the script will run with the following command: ./results_and_send_mail.py switch_commands.txt switches.json Instead of that, maybe you can look and help on the section of my code that adding the files to the mail? Thanks. RE: Netmiko - add multiple files, from different folders to mail. - heiner55 - Nov-26-2017 If only the mail part is important, you should make us a sample without netmiko. (with fake data) RE: Netmiko - add multiple files, from different folders to mail. - tomikovaknin - Nov-26-2017 Finally i succeed to to solve this. i just add in the top an empty array filenames = [] and filenames.append(filename) thanks. RE: Netmiko - add multiple files, from different folders to mail. - heiner55 - Nov-26-2017 OK, good to hear your program runs well now. |