Видимо, Я не могу до конца понять принцип pexpect
import pexpect .............................. def execute_cmd(connection, cmd): #connection.expect('.*') connection.send(cmd) connection.expect('.*') return connection.before + '\n' + connection.after def execute_remote_command(cmd, host, username='testuser', password='testpass'): connection = pexpect.spawn("ssh %s@%s -o 'StrictHostKeyChecking=no' -o 'UserKnownHostsFile /dev/null'" % (username, host), maxread=1) connection.expect('.*(P|p)assword.*') connection.sendline(password) connection.expect('.*') output = execute_cmd(connection, cmd) return output def get_host_info(host): #myclient = hostclient.HostClient([host],sudo=False,username='testuser',password='ironport') #output, code = myclient.execute('cat /etc/motd') output = execute_remote_command('uname -a', host) return output .................. hosts_to_process = (host1, host2, ... hostN) for item in hosts_to_process: print 'Processing %s\n' % item with open(fn, 'a') as f: host_info = get_host_info(item) f.write(item + '\n') f.write(host_info)
В вобщем в файле имеется только имя хоста (не вывод команды uname -a, а имена хостов со списка) и пустые строки:
host1
host2
.....
hostN