![](/img/trans.png)
[英]Python 2.7 fabric/paramiko EOF when trying to untar a remote file
[英]Python Fabric - erasing my remote file when it should be copying to it
解決了
看來我需要使用以下命令關閉sys.stdout文件(該文件已打開以供寫入)
sys.stdout.close()
然后重新打開它,但是在讀取模式下,
sys.stdout = open ('/home/myuser/verify_yslog_conf/remote_hostname/hi', 'r')
然后正確上傳。 我用了
time.sleep(60)
在運行中查找文件以進行故障排除。 在def完成之前它是空的,默認情況下會關閉“ hi”文件。 所以我意識到我需要關閉它才能完全寫入文件。 謝謝。
下面的代碼用於檢查syslog是否正在運行,如果是,則從拉出的遠程syslog.conf文件中用“ @”(而不是@@ 192.168.x.xx或192.168.x.xxx)注釋掉任何行。 在本地對這些行進行注釋后,應該將新文件上傳到舊位置的遠程服務器。 但是,所有發生的事情都是刪除了/etc/syslog.conf文件。 該文件未刪除,只是空的。
我知道問題出在看跌聲明中
fabric.operations.put('/home/myuser/verify_yslog_conf/remote_hostname/hi', '/etc/syslog.conf', use_sudo=True)
但看起來沒有錯。
def verify():
#lines below are good; keep them
ip1 = '192.168.x.xx'
ip2 = '92.168.x.xxx'
#check if syslog is running
output = sudo("/sbin/service syslog status")
if 'is running...' in output:
#pull the syslog file from the remote server
fabric.operations.get('/etc/syslog.conf')
#read thru the file
fh = open('/home/myuser/verify_yslog_conf/remote_hostname/syslog.conf', 'r')
f = fh.read()
fh.close()
#if the file needs commenting ...
if re.search(r'(@(?!({0}|{1})))'.format(ip1, ip2), f):
#get the file again -- maybe the problem? was advised to do this
fabric.operations.get('/etc/syslog.conf')
#save the file locally for reading and then for writing (r+ failed)
conf = open ('/home/myuser/verify_yslog_conf/remote_hostname/syslog.conf', 'r')
sys.stdout = open ('/home/myuser/verify_yslog_conf/remote_hostname/hi', 'w')
#for every line in the old /etc/syslog.conf
for line in conf:
#if it has an @ but not followed by the permitted IPs
if re.search(r'(@(?!({0}|{1})))'.format(ip1, ip2), line):
#comment out the line then print it (does it twice, dont care)
line = "#" + line
sys.stdout.write(line)
sys.stdout.write(line)
conf.close()
#upload the newly commented file to the remote host
fabric.operations.put('/home/myuser/verify_yslog_conf/remote_hostname/hi', '/etc/syslog.conf', use_sudo=True)
else:
print GOOD
else:
print RSYSLOG
嘗試在fabric.operations.put
命令之前立即執行sys.stdout.close()
。
可能在put操作之前,數據沒有刷新到文件,因此它發送的是空文件,但是當腳本完成后,它會自動刷新(寫入)數據,這就是為什么文件在本地系統上顯示正常的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.