我是EC2和boto的新手。我有一個EC2運行實例,我想執行一個shell命令,例如通過博託apt-get update
。Boto在ec2實例上執行shell命令
我尋覓了一番,發現在run_instances命令使用user_data
一個解決方案,但如果實例已經啓動呢?
我甚至不知道它是否可能。此參考文獻中的任何線索都會對您有所幫助。
我是EC2和boto的新手。我有一個EC2運行實例,我想執行一個shell命令,例如通過博託apt-get update
。Boto在ec2實例上執行shell命令
我尋覓了一番,發現在run_instances命令使用user_data
一個解決方案,但如果實例已經啓動呢?
我甚至不知道它是否可能。此參考文獻中的任何線索都會對您有所幫助。
boto.manage.cmdshell模塊可用於執行此操作。要使用它,你必須安裝paramiko軟件包。它的一個簡單的例子是使用:
import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance
# Connect to your region of choice
conn = boto.ec2.connect_to_region('us-west-2')
# Find the instance object related to my instanceId
instance = conn.get_all_instances(['i-12345678'])[0].instances[0]
# Create an SSH client for our instance
# key_path is the path to the SSH private key associated with instance
# user_name is the user to login as on the instance (e.g. ubuntu, ec2-user, etc.)
ssh_client = sshclient_from_instance(instance,
'<path to SSH keyfile>',
user_name='ec2-user')
# Run the command. Returns a tuple consisting of:
# The integer status of the command
# A string containing the output of the command
# A string containing the stderr output of the command
status, stdout, stderr = ssh_client.run('ls -al')
,這是從內存類型,但我認爲這是正確的。
您還可以查看Fabric(http://docs.fabfile.org/),它具有類似的功能,但也具有更復雜的功能和功能。
非常感謝garnaat。它看起來不錯,需要檢查。 – vibhor 2013-03-19 17:01:42
感謝您的鏈接和簡潔的比較 – Forethinker 2013-07-08 18:34:44
如果你想使用cmdshell你需要安裝paramiko。它並未在boto中列爲依賴項,因爲它可能難以在某些平臺上安裝,並且'cmdshell'不是boto功能的核心。 – garnaat 2015-07-14 12:28:17
我認爲你可以使用面料來滿足你的要求。只需檢查一次織物包裝。您可以通過結構庫在遠程服務器shell上執行該命令。
這是非常容易使用,你可以集成博託和織物。他們一起工作輝煌。
加上相同的命令可以執行n個節點。我相信這可能是你的要求
只需檢查出來。
是的喬治你是對的。這真的是一個很好的幫助,但我的問題已經解決了。我只使用布料。反正從我身邊+1,我也接受你的答案 – vibhor 2013-03-21 18:34:47
感謝Steffen的編輯。請記住更正。 – vibhor 2013-03-21 18:41:59
相關:[如何使用boto3 SSH和運行EC2中的命令?](https://stackoverflow.com/q/42645196/55075) – kenorb 2018-02-07 16:41:54