Мой код:SSH в AWS сервера и редактировать файл хостов
По какой-то причине это, кажется, бесконечный цикл и неоднократно печати «» и здесь 2 выход 'Ls -lah. Является ли что-то кровоточащим, я поступаю неправильно?
def update_hosts_file(public_dns,hosts_file_info):
for dns in public_dns:
print 'here2'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # wont require saying 'yes' to new fingerprint
key_path = os.path.join(os.path.expanduser(KEY_DIR), KEY_NAME)+'.pem'
ssh.connect(dns,username='ubuntu',key_filename=key_path)
ssh.exec_command('touch testing')
a,b,c=ssh.exec_command("ls -lah")
print b.readlines()
a,b,c=ssh.exec_command("file = open('/home/ubuntu/hosts', 'w')")
#print b.readlines()
ssh.exec_command("file.write('127.0.0.1 localhost\n')")
for tag,ip in hosts_file_info.iteritems():
ssh.exec_command("file.write('%s %s\n' % (ip,tag))")
ssh.exec_command("file.close()")
ssh.close()
public_dns = 'ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com'
print public_dns
hosts_file_info = {}
#hosts_file_info['1']='test'
#hosts_file_info['2']='test2'
#hosts_file_info['3']='test3'
#print hosts_file_info
update_hosts_file(public_dns,hosts_file_info)
Похоже, вы пытаетесь выполнять операторы Python, как если бы они были Баш команды, как файл ' = open ('/ home/ubuntu/hosts', 'w') '. Я не уверен, что это даст вам синтаксическую ошибку bash, или ошибка из 'file' like' can not open '=' (Нет такого файла) ', но я уверен, что она ничего не принесет. – abarnert
BTW, проверьте Ткань http://docs.fabfile.org/en/1.8/. Это делает этот стиль автоматизации простым и удобным. –