So, I know that you can use Pexpect to solve the problem, but I do not want to rely on additional libraries to solve the problem other than those that come with Python3.
I also know that generating public keys and resolving them on a remote host is ideal, but here is what I intend to use for this script to (configure the keys in the right place, etc.).
This is until I got "my own" with the help of the SO community. I was kind of stuck in checking if the divorced child pseudo-terminal finished doing what it should or not. Sense, I can determine if SSH is running or not, because it will work as long as the run() function works.
(my pid_exists will return True as long as it runs in run () mode. And if I exit run() so that the SSH session does not have time to do what it should do)
#!/usr/bin/python import pty, sys from subprocess import Popen, PIPE, STDOUT from time import sleep from os.path import expanduser, abspath from os import walk, setsid, fork, waitpid, execv, read, write, kill def pid_exists(pid): """Check whether pid exists in the current process table.""" if pid < 0: return False try: kill(pid, 0) except (OSError, e): return e.errno == errno.EPERMRM else: return True class ssh(): def __init__(self, host, execute='echo "done" > /root/testing.txt', user='root', password=b'SuperPassword'): self.exec = execute self.host = host self.user = user self.password = password self.run() def run(self): command = [ '/usr/bin/ssh', self.user+'@'+self.host, '-o', 'NumberOfPasswordPrompts=1', self.exec, ]
I cannot find much (or any) information on how to get the pseudo-pseudo-terminal execution process that Python opens and passes to me. Can I or should I include a subprocess, as described here: https://stackoverflow.com/a/167268/ or is there another way?