gpt4 book ai didi

python - 无法使用 boto 将 SCP 文件发送到 AWS

转载 作者:太空宇宙 更新时间:2023-11-04 10:17:46 25 4
gpt4 key购买 nike

我正在尝试使用 python 将某些内容自动上传到我的 AWS EC2 服务器。我无法让 ssh_client.put_file() 工作。它要么一直给我 IOERROR: Failire 要么 IOError: [Errno 2] No such file

关于我缺少什么的任何想法?这个ssh_client不能用来scp上传吗?

import boto
import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance
import argparse

#Parse input
parser = argparse.ArgumentParser(description='Upload and train images for detection')
parser.add_argument('path_to_key', help='Path to Pem key')
parser.add_argument('path_to_tar', help='Path to positives.tar')
args = parser.parse_args()
args_keypath = args.path_to_key
args_tarpath = args.path_to_tar

# Connect to your region of choice
print "Connecting to server..."
access_key = ""
secret_access_key = ""
conn = boto.ec2.connect_to_region('us-east-1', aws_access_key_id=access_key, aws_secret_access_key=secret_access_key)

print "Connecting to instance..."
# Connect to an existing instance
reservations = conn.get_all_instances(['i-c8aab576'])
instance = reservations[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.)
print "Creating CommandShell..."
key_path = args_keypath
ssh_client = boto.manage.cmdshell.sshclient_from_instance(instance,
key_path,
host_key_file='~/.ssh/known_hosts',
user_name='ubuntu')

status, stdout, stderr = ssh_client.run('ls -al')
print(status)
print(stdout)
print(stderr)

#Upload positives - WELL THIS ISN'T WORKING
print "Uploading file..."
local_filepath = args_tarpath
remote_filepath = "~/Sharing/"
ssh_client.put_file("/home/willem/.ssh/test.txt", "/home/ubuntu/Sharing/")
#ssh_client.put_file(local_filepath, remote_filepath)

最佳答案

如果您有 ssh 登录权限,您可以使用 .pem 并在本地运行命令,我的解决方案是:

通过创建预留来创建服务器:

reservation = conn.run_instances(my_AMI,
key_name=my_key,
instance_type='c4.xlarge',
security_group_ids=security_group,
placement='us-east-1d')

instance = reservation.instances[0]
print colored("Instance IP: %s" % instance.ip_address, 'yellow')

然后我可以对文件进行 scp:

instance_IP = instance.ip_address
os.system('scp -i %s %s ubuntu@%s:~/Sharing' % (key_path, args_tarpath, instance_IP) )

关于python - 无法使用 boto 将 SCP 文件发送到 AWS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com