【发布时间】:2014-01-11 07:53:27
【问题描述】:
我的代码:
由于某种原因,这似乎是无限循环并重复打印“here2”和“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 语句,就好像它们是 bash 命令一样,例如
file = open('/home/ubuntu/hosts', 'w')。我不确定这是否会给你一个 bash 语法错误,或者来自file的错误,比如cannot open '=' (No such file),但我相信它不会有任何用处。 -
顺便说一句,请查看 Fabric docs.fabfile.org/en/1.8。它使很多这种类型的自动化变得简单方便。
标签: python python-2.7 amazon-web-services ssh boto