将命令名附加到文件python3

时间:2018-04-10 07:45:31

标签: python-3.x

在通过python学习网络自动化的过程中,我创建了一个脚本,用一些命令捕获一些网络交换机细节。我已明确地将命令保存在commands.txt中并以jason格式在文件devices.json中登录凭证以从中选择它,当我运行它时,它通过创建一个带有命令输出详细信息的文件按预期工作邮票。

我想要的:因为我有commands.txt文件有不同的命令,我想知道为commands.txt中的每个命令创建一个单独的文件。截至目前,一切都被捕获到一个文件,所需的例子..

prod-stvdx-sw_vcs_details.txt-Apr-10-2018:12:53
prod-stvdx-sw_vcs.txt-Apr-10-2018:12:53
prod-stvdx-sw_fabric_trunk.txt-Apr-10-2018:12:53

以下是脚本版本:

from __future__ import absolute_import, division, print_function

import json
import netmiko
import signal
import sys
import time

timestamp = time.strftime('%b-%d-%Y:%H:%M')

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.NetMikoTimeoutException,
                      netmiko.ssh_exception.NetMikoAuthenticationException)

#username, password = getPass.get_credentials()

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:
    #device['username'] = username
    #device['password'] = password
    try:
        print('~' * 79)
        print('Connecting to device:', device['ip'])
        connection = netmiko.ConnectHandler(**device)
        filename = connection.base_prompt + '.txt' + '-' + timestamp
        with open(filename, 'w') as out_file:
            for command in commands:
                out_file.write('++++ Output of ' + command + '\n\n')
                out_file.write(connection.send_command(command) + '\n\n')
        connection.disconnect()
    except netmiko_exceptions as e:
        print('Failed to ', device['ip'], e)

我的commands.txt文件:

$ cat commands.txt
show vcs details
show vcs
show fabric islports
show fabric isl
show fabric trunk
show logging raslog
show version
show ip int brief

运行

$ ./brocade_2.py commands.txt devices.json
-rw-r--r-- 1 moli moli 286K Apr 10 12:53 prod-stvdx-sw.txt-Apr-10-2018:12:53

1 个答案:

答案 0 :(得分:0)

我想如果我理解正确,你想在一个单独的文件中捕获每个命令的输出:

from __future__ import absolute_import, division, print_function

import json
import netmiko
import signal
import sys
import time

timestamp = time.strftime('%b-%d-%Y:%H:%M')

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.NetMikoTimeoutException,
                      netmiko.ssh_exception.NetMikoAuthenticationException)

#username, password = getPass.get_credentials()

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:
    #device['username'] = username
    #device['password'] = password
    try:
        print('~' * 79)
        print('Connecting to device:', device['ip'])
        connection = netmiko.ConnectHandler(**device)
        for command in commands:
            filename = connection.base_prompt +'-'+ command+'.txt' + '-' + timestamp
            with open(filename, 'w') as out_file:
                out_file.write('++++ Output of ' + command + '\n\n')
                out_file.write(connection.send_command(command) + '\n\n')
        connection.disconnect()
    except netmiko_exceptions as e:
        print('Failed to ', device['ip'], e)