def run(self, cmd):
ssh_newkey = 'Are you sure you want to continue connecting'
child = pexpect.spawn('ssh -l %s -p %s %s %s'%(self._user, self._port, self._host, cmd))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
if i == 0: #timeout
raise Exception('ssh timeout')
if i == 1: #SSH does not have the public key. Just accept it.
child.sendline('yes')
child.expect('password: ')
i = child.expect([pexpect.TIMEOUT, 'password: '])
if i == 0: #timeout
raise Exception('ssh timeout')
child.sendline(self._pwd)
child.expect(pexpect.EOF)
return child.before
评论列表
文章目录