Guest User

Untitled

a guest
Jun 25th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. from pexpect import pxssh
  2. import pexpect
  3.  
  4. # conn_up is a function for connecting to a remote server and doing an uptime.
  5.  
  6. def conn_up():
  7. try:
  8. s = pxssh.pxssh()
  9. hostname = '192.168.1.32'
  10. username = 'pi'
  11. s.login(hostname, username,ssh_key='/home/miaou/priv_keys')
  12. s.sendline('uptime') # run a command
  13. s.prompt() # match the prompt
  14. print(s.before.decode()) # print everything before the prompt.
  15. except pxssh.ExceptionPxssh as e:
  16. print("pxssh failed on login.")
  17. print(e)
  18.  
  19.  
  20. # conn_wr is a function for connecting to a remote server and create a file 123toto.txt
  21. # on the remote server
  22. # this method is for programs like sftp or ssh, which needs some human interactions
  23.  
  24. def conn_wr():
  25. child = pexpect.spawn('ssh pi@192.168.1.32')
  26. child.expect('password:')
  27. child.sendline('XXX')
  28. child.expect('pi')
  29. child.sendline('touch 123toto.txt')
  30. child.expect('pi')
  31. child.sendline('exit')
  32.  
  33. if __name__ == '__main__':
  34. conn_up()
  35. conn_wr()
  36.  
  37. if __name__ == '__main__':
  38. conn_up()
  39. conn_wr()
Add Comment
Please, Sign In to add comment