# -*- coding: utf-8 -*-
#!/usr/bin/env python
import pexpect,os #导入需要用到模块
def ssh_cmd(ip, shell_cmd):
passwd= '1qaz#EDC'
print 'host: %s is connected... ' % ip
child = pexpect.spawn('ssh root@%s' % (ip))
fout = file('log.txt','a')
child.logfile = fout
try:
i = child.expect('password:')
if i == 0:
child.sendline(passwd)
elif i == 1:
child.sendline('yes\n')
child.expect('password: ')
child.sendline(passwd)
print 'host:%s Login ok!' % ip
child.expect('#')
child.sendline(shell_cmd) #执行传过来的shell命令
child.expect('#')
print 'host:%s Command Exe