Форум сайта python.su
Здравствуйте! Пытаюсь автоматизированно собрать информацию с удаленных машин, но вот даже с uname проблема.
Видимо, Я не могу до конца понять принцип 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)
host1
host2
.....
hostN
Отредактировано Master_Sergius (Авг. 14, 2014 19:48:26)
Офлайн
Всё-таки Я разобрался самостоятельно. Он такой редиска, этот pexpect, что ему нужно явно указать ожидание приглашения для ввода, вот вместо этого:
connection.expect('.*')
connection.expect('[#$] ')
Офлайн