在一行上打印大量信息

时间:2015-08-11 15:31:16

标签: python

我有这个python代码,它登录到一堆juniper交换机,并从安装在其中的不同卡中抓取它们的序列号。

现在它打印出不同行的信息,但我希望它只是一行,所以我可以将它导入到csv文件中更容易一些。

这是代码:

from jnpr.junos import Device
import getpass

#Grab credentials
username = raw_input("Enter your username:")
password = getpass.getpass("Enter your password:")

with open('switchlist') as infile:
for host in infile:
    try:
        #Connect to devices in switchlsit file using username and password provided above
        dev = Device(host=host.strip(),  user=username, password=password)
        dev.open()
        # XML RPC Command for "show chassis"
        chassisInfo = dev.rpc.get_chassis_inventory()
        print "Switch: ", dev.facts['hostname']
        for chassis in chassisInfo.iter('chassis-module'):
            if "FPC" in chassis.find('name').text:
                print "Serial: ", chassis.find('name').text,chassis.find('serial-number').text

        for chassisPIC in chassisInfo.iter('chassis-sub-module'):
            if "PIC 0" in chassisPIC.find('name').text:
                continue
            if "PIC" in chassisPIC.find('name').text:
                print "Serial: ", chassisPIC.find('name').text,chassisPIC.find('serial-number').text
    except:
        print "error"

有什么想法吗?我不确定我怎么能这样做。想到以某种方式将信息存储到变量中,然后打印出来,但我不知道该怎么做。

当脚本运行时,它目前看起来像这样:

Switch:  switch1
Serial:  FPC 0 TB3712345467
Serial:  PIC 1 RR3712345467
Serial:  PIC 2 RR3712345467
Switch:  switch2
Serial:  FPC 0 TB3712345467
Serial:  PIC 1 RR3712345467
Serial:  PIC 2 RR312345467

我希望它像是

 switch switch1, fpc0 TB3712345467, pic1 TB3712345467, pic2 TB3712345467

2 个答案:

答案 0 :(得分:0)

将所有数据放入列表并使用', '.join

for host in infile:
    data = []
    try:
        # Connect to devices in switchlsit file using username and password provided above
        dev = Device(host=host.strip(),  user=username, password=password)
        dev.open()
        # XML RPC Command for "show chassis"
        chassisInfo = dev.rpc.get_chassis_inventory()
        data.append("Switch: {}".format(dev.facts['hostname']))
        for chassis in chassisInfo.iter('chassis-module'):
            if "FPC" in chassis.find('name').text:
                data.append("Serial: {} {}".format(
                    chassis.find('name').text, 
                    chassis.find('serial-number').text)

        for chassisPIC in chassisInfo.iter('chassis-sub-module'):
            if "PIC 0" in chassisPIC.find('name').text:
                continue
            if "PIC" in chassisPIC.find('name').text:
                data.append("Serial: {} {}".format(
                    chassisPIC.find('name').text, 
                    chassisPIC.find('serial-number').text)
    except:
        print "error"
    finally:
        print(", ".join(data))

虽然如果你的最终目标是将其作为CSV,我不清楚为什么要将它打印到屏幕上。也许只是:

all_data = []
# all the code written above, with the finally block changed to:
finally:
    print(", ".join(data))
    all_data.append(data)

然后,当您离开for循环后,all_data就是每个主机的字段列表列表。这很有用,因为您可以这样做:

import csv

headers = ['some', 'column', 'headers', 'go', 'here']
with open('path/to/outfile.csv', 'w') as outf:
    writer = csv.writer(outf)
    writer.writerows(headers + all_data)

答案 1 :(得分:0)

快速,丑陋的回答:

将调用替换为sys.stdout.write打印。

答案很长:

理想情况下,您可以将功能分为两个功能:

  • 进行扫描的人。将相关数据编译成一个列表。
  • 执行格式化的人。以适当的格式从列表中吐出数据。

这是两个不同的任务,所以每个任务都应该有自己的功能。实际上,如果它已经拆分了,你就不必问了。

对于处理CSV,正如有人指出的那样,你的格式化功能可以利用python的CSV module