Advertisement
Guest User

Untitled

a guest
Mar 20th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # author: oscarp
  3. # pexpect practice
  4.  
  5. import pexpect
  6.  
  7. PROMPT = ['# ', '>>> ', '> ', '\$ ']
  8.  
  9. def send_command(child, cmd):
  10. child.sendline(cmd)
  11. child.expect(PROMPT)
  12. print child.before
  13.  
  14. def connect(user, host, password):
  15. ssh_newkey = 'Are you sure you wanna continue connect? '
  16. connStr = 'ssh ' + user + '@' + host
  17. child = pexpect.spawn(connStr)
  18. ret = child.expect([pexpect.TIMEOUT, ssh_newkey, '[P|p]assword:'])
  19. if ret == 0:
  20. print('[-] Error connecting')
  21. return
  22. if ret == 1:
  23. child.sendline('yes')
  24. ret = child.expect([pexpect.TIMEOUT, '[P|p]assword:'])
  25. if ret == 0:
  26. print('[-] Error connecting.')
  27. return
  28. child.sendline(password)
  29. child.expect(PROMPT)
  30. return child
  31.  
  32. def main():
  33. host = 'localhost'
  34. user = 'root'
  35. password = 'toor'
  36. child = connect(user, host, password)
  37. send_command(child, 'cat /etc/shadow | grep root')
  38.  
  39. if __name__ == '__main__':
  40.  
  41. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement