0
我有一個腳本,將連接到主機名列表,運行一個命令,並打印輸出到屏幕。我知道如何將輸出發送到一個txt文件,但我想以某種方式進行設置,即每個主機上的輸出將創建自己的txt文件。理想情況下,我希望將文件保存爲host1.txt,host2.txt等。如果有更簡單/更智能的方法來實現此目的,請打開其他想法。保存輸出在多個文本文件
import sys, os, string, threading
import getpass
import paramiko
cmd = "sh vl bri"
lanid = raw_input("Enter your uname: ")
#get password info
def enterPassword():
while True: # repeat forever
pwd = getpass.getpass('Enter password:')
password_again = getpass.getpass('Confirm password:')
if pwd != password_again:
print 'Password and confirmation do not match.Please try again!!'
else:
return pwd
pwd = enterPassword()
outlock = threading.Lock()
def workon(host):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=lanid, password=pwd)
stdin, stdout, stderr = ssh.exec_command(cmd)
print stdout.read()
stdin.flush()
with outlock:
print stdout.readlines()
def main():
hosts = ['host1', 'host2', 'host3', ] # etc
threads = []
for h in hosts:
t = threading.Thread(target=workon, args=(h,))
t.start()
threads.append(t)
for t in threads:
t.join()
main()
我試過了,但即時得到縮進錯誤。還是新的腳本所以我確定它的東西很容易 高清workon(主持人): SSH = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(主機,用戶名= lanid,密碼= PWD) STDIN,STDOUT,標準錯誤= ssh.exec_command(CMD) #PRINT stdout.read() \t \t張開( 「輸出 - 」 +主機, 'W')爲f: \t \t f.write (stdout) stdin.flush() with outlock: print stdout.readlines() – JoeScripter
道歉用於格式化之前的評論。仍在試圖弄清楚如何將代碼粘貼到評論中。 – JoeScripter