我需要提一下。我使用Windows。
现在我知道如何使用boto。但是我遇到了一个问题,我无法运行" sudo"基于boto。
status, stdout, stderr = ssh_client.run('sudo python killerparser.py')
错误是 sudo:抱歉,你必须有一个tty来运行sudo
然后我尝试运行它。
status, stdout, stderr = ssh_client.run('ssh -t localhost sudo python killerparser.py')
但现在错误变为'由于stdin不是终端,因此不会分配伪终端。\ r \ n主机密钥验证失败。'
我不想更改不安全的用户数据。所以它需要使用面料的想法。但是如何定义主机和密钥路径?我认为面料不是基于对象的,这实在令人沮丧。 我的所有代码:
import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance
from fabric.api import env, run, cd, settings, sudo,hosts;
env.host = 'ec2-user@#.#.#.#'
env.user = "ec2-user"
env.key_filename = "D:\key.pem"
conn = boto.ec2.connect_to_region('us-east-1',aws_access_key_id="***",aws_secret_access_key="*")
instance = conn.get_all_instances(['***'])[0].instances[0]
ssh_client = sshclient_from_instance(instance,
ssh_key_file='**',
user_name='ec2-user')
sudo("cd ~");
sudo("python killerparser.py");
现在没有错误。但它无法执行shell
killerparser.py
import subprocess, signal,os;
for line in os.popen("ps ax | grep -i newLive.py"):
if "grep" in line: continue;
fields = line.split()
pid = fields[0]
os.kill(int(pid), signal.SIGKILL)
proc = subprocess.Popen('sudo python newLive.py 2>newLive.err', shell=True,
stdin=None, stdout=None, stderr=None, close_fds=True)
答案 0 :(得分:1)
我不同意你正在做的两件事。一:sudo python...
否。将其作为www数据或等效运行。另外,请使用supervisord
而不是您目前正在做的事情。
如果你不在窗户上,那不是很重要..你告诉我这不适合你吗?
fabfile.py:
import boto.ec2
from fabric.api import env, run, sudo, task
env.key_filename = "/PATH/TO/SSH/FILE.pem"
env.user = "ubuntu"
@task
def amazon(**kwargs):
conn = boto.ec2.connect_to_region(
'us-east-1',
aws_access_key_id="*********",
aws_secret_access_key="**************"
)
hosts = []
for reservation in conn.get_all_instances():
for instance in reservation.instances:
# if filters were applied
if kwargs:
skip_instance = False
for key, value in kwargs.items():
instance_value = getattr(instance, key)
# makes sure that `group` is handeled
if isinstance(instance_value, list):
new_value = []
for item in instance_value:
if isinstance(item, boto.ec2.group.Group):
new_value.append(item.name)
else:
new_value.append(item)
instance_value = new_value
if value not in instance_value:
skip_instance = True
break
else:
# every other single value gets handeled here
if instance_value != value:
skip_instance = True
break
if skip_instance:
continue
if instance.dns_name:
hosts.append(instance.dns_name)
elif instance.ip_address:
hosts.append(instance.ip_address)
env.hosts = hosts
@task
def whoami():
run('whoami')
sudo('whoami')
我为您添加了过滤器,以防万一,您可以将其运行为:
fab amazon whoami
- 它将遍历amazon中的每个服务器并连接并运行whoami
命令。
fab amazon:ip_address=<IP OF AN INSTANCE YOU KNOW OF> whoami
- 只会使用过滤器上匹配的ip框。 (它应该适用于boto中的instance
中的每个字段)
那只是一个噱头,groups
是一个&#34;我&#34;会使用:
fab amazon:groups=<GROUP NAME FROM AMAZON> whoami
- 将在与所述组名匹配的所有服务器上运行whoami
。
证明:
$ fab amazon:dns_name=******* whoami
[*******] Executing task 'whoami'
[*******] run: whoami
[*******] out: ubuntu
[*******] out:
[*******] sudo: whoami
[*******] out: root
[*******] out:
Done.
Disconnecting from *******... done.
和
$ fab amazon:groups=webservers whoami
[***1***] Executing task 'whoami'
[***1***] run: whoami
[***1***] out: ubuntu
[***1***] out:
[***1***] sudo: whoami
[***1***] out: root
[***1***] out:
... truncated...
[***4***] Executing task 'whoami'
[***4***] run: whoami
[***4***] out: ubuntu
[***4***] out:
[***4***] sudo: whoami
[***4***] out: root
[***4***] out:
Done.
Disconnecting from ***1***... done.
Disconnecting from ***2***... done.
Disconnecting from ***3***... done.
Disconnecting from ***4***... done.
Disconnecting from ***5***... done.
Disconnecting from ***6***... done.
Disconnecting from ***7***... done.