Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- def TerminalCommand(command='~/take_snapshot.sh ', shell=True):
- proc = subprocess.Popen(
- command,
- shell=shell,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- )
- msg = 'running command: "{}"'.format(command).encode('utf-8')
- stdout_value, stderr_value = proc.communicate(msg)
- print('pass through:', repr(stdout_value.decode('utf-8')))
- print('stderr :', repr(stderr_value.decode('utf-8')))
- from paramiko import SSHClient, AutoAddPolicy
- def Connect(ip, username='pi', pw='password'):
- '''ssh into the pi'''
- print('connecting to {}@{}...'.format(username, ip))
- ssh = SSHClient()
- ssh.set_missing_host_key_policy(AutoAddPolicy())
- ssh.connect(ip, username=username, password=pw)
- print('connection status =', ssh.get_transport().is_active())
- return ssh
- def SendCommand(ssh, command, pw='password'):
- '''send a terminal/bash command to the ssh'ed-into machine '''
- print('sending a command... ', command)
- stdin, stdout, stderr = ssh.exec_command( command )
- if "sudo" in command:
- stdin.write(pw+'n')
- stdin.flush()
- print('nstout:',stdout.read())
- print('nsterr:',stderr.read())
- myssh = Connect(ip='192.168.3.14')
- SendCommand(myssh, command='~/script.py')
- ssh user@pi.ip "/path/to/python/code/code.py"
Add Comment
Please, Sign In to add comment