這涵蓋了基地。注意sudo用於需要更多權限的事情。我們配置了sudo以允許那些用戶使用這些命令,而不需要輸入密碼。
另外,請記住,你應該運行ssh-agent來使這個「有道理」。但總而言之,它運作得非常好。運行deploy-control httpd configtest
將檢查所有遠程服務器上的apache配置。
#!/usr/local/bin/python
import subprocess
import sys
# The [email protected]: for the SourceURLs (NO TRAILING SLASH)
RemoteUsers = [
"[email protected]",
"[email protected]",
]
###################################################################################################
# Global Variables
Arg = None
# Implicitly verified below in if/else
Command = tuple(sys.argv[1:])
ResultList = []
###################################################################################################
for UH in RemoteUsers:
print "-"*80
print "Running %s command on: %s" % (Command, UH)
#----------------------------------------------------------------------------------------------
if Command == ('httpd', 'configtest'):
CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd configtest'))
#----------------------------------------------------------------------------------------------
elif Command == ('httpd', 'graceful'):
CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd graceful'))
#----------------------------------------------------------------------------------------------
elif Command == ('httpd', 'status'):
CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd status'))
#----------------------------------------------------------------------------------------------
elif Command == ('disk', 'usage'):
CommandResult = subprocess.call(('ssh', UH, 'df -h'))
#----------------------------------------------------------------------------------------------
elif Command == ('uptime',):
CommandResult = subprocess.call(('ssh', UH, 'uptime'))
#----------------------------------------------------------------------------------------------
else:
print
print "#"*80
print
print "Error: invalid command"
print
HelpAndExit()
#----------------------------------------------------------------------------------------------
ResultList.append(CommandResult)
print
###################################################################################################
if any(ResultList):
print "#"*80
print "#"*80
print "#"*80
print
print "ERRORS FOUND. SEE ABOVE"
print
sys.exit(0)
else:
print "-"*80
print
print "Looks OK!"
print
sys.exit(1)
尼斯。這是我開始工作的第一個代碼。問題:你知道什麼時候ssh連接終止了嗎? – physicsmichael 2009-08-20 00:41:37
運行命令後。 – Peter 2009-08-20 00:42:41
@Peter:如果我需要與遠程主機(上提示的答案,等等)的相互作用,我應該使用Pexpect的或有一個內置的庫這樣的功能 – legesh 2009-09-06 08:08:13