これはベースをカバーしています。より多くの特権が必要なものに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
。 – Peter
@Peter:リモートホスト(プロンプトなどでの回答)とのやりとりが必要な場合は、Pexpectを使用するか、そのような機能を備えた組み込みのライブラリが必要です。 – legesh