from os import fork, waitpid, execv, read, write
import pty, sys
class ssh():
def __init__(self, host, execute='echo "done" > /root/testing.txt', askpass=False, user='root', password='UberPassword'):
self.exec = execute
self.host = host
self.user = user
self.password = password
self.askpass = askpass
self.run()
def run(self):
command = [
'/usr/bin/ssh',
self.user+'@'+self.host,
'-o', 'NumberOfPasswordPrompts=1',
self.exec,
]
pid, child_fd = pty.fork()
if not pid: # Child process
# Replace child process with our SSH process
execv(command[0], command)
## if we havn't setup pub-key authentication
## we can loop for a password promt and "insert" the password.
while self.askpass:
try:
output = read(child_fd, 1024).strip()
except:
break
lower = output.lower()
# Write the password
if b'password:' in lower:
write(child_fd, self.password + b'\n')
break
elif b'are you sure you want to continue connecting' in lower:
# Adding key to known_hosts
write(child_fd, b'yes\n')
elif b'MOTD and Leagal warning' in lower:
pass # This is an understood message
else:
print('Error:',output)
waitpid(pid, 0)
Will only work on linux tho thanks to pty.
Another short solution is (but you need public keys):
from subprocess import Popen, PIPE, STDOUT
x = Popen("ssh -t -t root@hostname.com 'echo \"done\" > /root/testing.txt'", shell=True, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
while x.poll() == None:
pass
x.stdout.close()
x.stdin.close()