Stdout 如何使用Python/Paramiko脚本忽略MOTD和横幅消息?

Stdout 如何使用Python/Paramiko脚本忽略MOTD和横幅消息?,stdout,paramiko,Stdout,Paramiko,我有一个脚本,可以读取带有IP地址的host.csv文件,然后依次连接到每个服务器并运行一些命令。我希望输出转到脚本中原始输入中指定的文件名。现在我只打印输出,但我想将所有输出保存到一个文件中,除了登录信息(BANNER/MOTD)和出现的时间戳。我一直在努力使它只打印我在每个框上运行的实际命令的输出,但我一直无法使它工作 我已经用Python编程3个月了,我已经取得了一些成功,但有时我会遇到一些我还无法克服的障碍 from __future__ import print_function im

我有一个脚本,可以读取带有IP地址的host.csv文件,然后依次连接到每个服务器并运行一些命令。我希望输出转到脚本中原始输入中指定的文件名。现在我只打印输出,但我想将所有输出保存到一个文件中,除了登录信息(BANNER/MOTD)和出现的时间戳。我一直在努力使它只打印我在每个框上运行的实际命令的输出,但我一直无法使它工作

我已经用Python编程3个月了,我已经取得了一些成功,但有时我会遇到一些我还无法克服的障碍

from __future__ import print_function
import threading
import paramiko
import getpass
import time
import os
import sys

os.system('clear')
class ssh(object):

    shell = None
    client = None
    transport = None

    def __init__(self, address, username, password):

        print('Connecting to server on ip', str(address) + '.')
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()

    def close_connection(self):

        if(self.client != None):
            self.client.close()
            self.transport.close()

    def open_shell(self):

        self.shell = self.client.invoke_shell()
        #self.shell.recv(1000)

    def send_shell(self, command):

        if(self.shell):
            self.shell.send(command + '\n')
        else:
            print("Shell not opened.")

    def process(self):

        while True:
            # Print data when available
            if self.shell is not None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                data = alldata.split(os.linesep)

                for line in data:
                    print(line)

header = "################## WHAT DO YOU WANT TO DO?? ##################"
headlen = len(header) - 2
secondline = "|" + (" " * headlen) + "|"
thirdline = "|" + "  1. Run the daily ATM Script." + (" " * 30) + "|"
fourthline = "|" + "  2. Run a single command on the entire ATM network" + (" " * 9) + "|"

print(header)
print(secondline)
print(secondline)
print(thirdline)
print(fourthline)
print(secondline)
print(secondline)
print("#" * len(header))

choice = raw_input("Choose your destiny: ")


while choice:
    if choice == "1":
            print('  \n##### STARTING PROGRAM #####\n')

            sshUsername = getpass.getpass(prompt='ENTER USERNAME:  ')
            sshPassword = getpass.getpass('ENTER PASSWORD:  ')
            filename = raw_input("What filename dost thou chooseth?")
            writefile = open(filename, 'a')

            def main():

                with open("host.csv", "r") as r:
                    for address in r:
                        print(address)
                        try:
                            connection = ssh(address, sshUsername, sshPassword)
                            try:
                                connection.open_shell()
                                time.sleep(1)
                                connection.send_shell('hardware cecplus timing references show')
                                time.sleep(1)
                                connection.send_shell('power')
                                time.sleep(1)
                                connection.send_shell('hard port show')
                                time.sleep(1)
                                connection.send_shell('sig show')
                                time.sleep(1)
                                connection.send_shell('conn spvcc pp show')
                                time.sleep(1)
                                connection.send_shell('interface sonet path near total')
                                time.sleep(1)
                            except:
                                print('Failed to run commands on:', r)
                        except:
                            print('Failed connection to:', r)
                        time.sleep(5)
                        connection.close_connection()

            main()



    elif choice == "2":

        COMMAND = raw_input("INPUT A SINGLE COMMAND TO RUN ON THE ENTIRE ATM NETWORK:" )

        if __name__ == '__main__':

                print('  \n##### STARTING PROGRAM #####\n')

                sshUsername = getpass.getpass(prompt='ENTER USERNAME:  ')
                sshPassword = getpass.getpass('ENTER PASSWORD:  ')

                def main():

                        with open("host.csv", "r") as r:
                            for address in r:
                                print(address)
                                try:
                                    connection = ssh(address, sshUsername, sshPassword)
                                    try:
                                        connection.open_shell()
                                        time.sleep(1)
                                        connection.send_shell(COMMAND)
                                        time.sleep(1)

                                    except:
                                        print('Failed to run commands on:', r)
                                except:
                                    print('Failed connection to:', r)
                                time.sleep(1)
                                connection.close_connection()

                main()



    else:
        print("If thou dost not Enter 1 or 2 thou willest not proceed")
        choice = input("Choose your destiny: ")
当前结果只是打印所有内容,包括横幅和motd。如果我能让它只打印命令的输出,我知道我可以将这些输出写入文件。我基本上想忽略MOTD和横幅消息