Guest User

Untitled

a guest
Mar 28th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import subprocess
  2. def TerminalCommand(command='~/take_snapshot.sh ', shell=True):
  3. proc = subprocess.Popen(
  4. command,
  5. shell=shell,
  6. stdin=subprocess.PIPE,
  7. stdout=subprocess.PIPE,
  8. stderr=subprocess.PIPE,
  9. )
  10.  
  11. msg = 'running command: "{}"'.format(command).encode('utf-8')
  12. stdout_value, stderr_value = proc.communicate(msg)
  13.  
  14. print('pass through:', repr(stdout_value.decode('utf-8')))
  15. print('stderr :', repr(stderr_value.decode('utf-8')))
  16.  
  17. from paramiko import SSHClient, AutoAddPolicy
  18.  
  19. def Connect(ip, username='pi', pw='password'):
  20. '''ssh into the pi'''
  21. print('connecting to {}@{}...'.format(username, ip))
  22. ssh = SSHClient()
  23. ssh.set_missing_host_key_policy(AutoAddPolicy())
  24. ssh.connect(ip, username=username, password=pw)
  25. print('connection status =', ssh.get_transport().is_active())
  26. return ssh
  27.  
  28. def SendCommand(ssh, command, pw='password'):
  29. '''send a terminal/bash command to the ssh'ed-into machine '''
  30. print('sending a command... ', command)
  31. stdin, stdout, stderr = ssh.exec_command( command )
  32. if "sudo" in command:
  33. stdin.write(pw+'n')
  34. stdin.flush()
  35. print('nstout:',stdout.read())
  36. print('nsterr:',stderr.read())
  37.  
  38. myssh = Connect(ip='192.168.3.14')
  39. SendCommand(myssh, command='~/script.py')
  40.  
  41. ssh user@pi.ip "/path/to/python/code/code.py"
Add Comment
Please, Sign In to add comment