我有一个脚本,该脚本读取具有IP地址的host.csv文件,然后依次连接到每个服务器并运行一些命令。我希望输出转到在脚本的raw_input中指定的文件名。现在,我只是打印输出,但是我想将所有输出保存到文件中,除了登录信息(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和横幅消息。