For each and every ssh command asks for password, Python
Question: I am trying to execute the following code, which asks me password for each and
every ssh command though I provide my password in the code. Can any one please
tell me where I am doing mistake. Thanks in advance

    
    
    import signal
    from subprocess import call, PIPE, Popen
    from time import sleep
    import os, pty
    class SshCmd:
    
        socket = ''
        pid = 0
        password = None
    
        def __init__(self, password = None):
            if password:
                SshCmd.password = password
    
                # start agent
                devnull = open(os.devnull, 'w')
                call(['killall', 'ssh-agent'], stderr=devnull)
                process = Popen('/usr/bin/ssh-agent', stdout=PIPE, stderr=devnull)
                stdout, stderr = process.communicate()
                lines = stdout.splitlines()
                SshCmd.socket = lines[0].decode().split(';')[0].split('=')[1]
                SshCmd.pid = lines[1].decode().split(';')[0].split('=')[1]
                devnull.close()
    
                # unlock key
                pid, fd = pty.fork()
                if 0 == pid:
                    # child adds key
                    os.execve('/usr/bin/ssh-add', ['/usr/bin/ssh-add'], \
                        {'SSH_AUTH_SOCK': SshCmd.socket, 'SSH_AGENT_PID': SshCmd.pid})
                else:
                    # parent send credentials
                    cmsg = os.read(fd, 1024)
                    os.write(fd, SshCmd.password.encode())
                    os.write(fd, os.linesep.encode())
                    cmsg = os.read(fd, 1024)
                    if len(cmsg) <= 2:
                        os.waitpid(pid, 0)
                    else:
                        os.kill(pid, signal.SIGTERM)
    
        def execve(self, path, args, env = {}):
            if not SshCmd.password:
                return
            pid = os.fork()
            if 0 == pid:
                env['SSH_AUTH_SOCK'] = SshCmd.socket
                env['SSH_AGENT_PID'] = SshCmd.pid
                os.execve(path, args, env)
            else:
                os.waitpid(pid, 0)
    
        def ssh(self, user, host, cmd, args = []):
            cmdLine = cmd
            for arg in args:
                cmdLine += ' '
                cmdLine += arg
            self.execve('/usr/bin/ssh',
                    ['/usr/bin/ssh',
                    '-o',
                    'UserKnownHostsFile=/dev/null',
                    '-o',
                    'StrictHostKeyChecking=false',
                    '%(user)s@%(host)s' % {'user': user, 'host': host},
                    cmdLine])
    
    if '__main__' == __name__:
        other = SshCmd('passowrd')
        other.ssh('root', 'host', '/sbin/ifconfig')
        other.ssh('root', 'host', 'ping', ['-c', '5', 'localhost'])
    


Answer: You are not making a mistake. In order to skip the password step, you need to
pre-validate your request, you can do this by using the command `ssh-copy-id`
first. which will store the credentials and allow connection through a key.
You need to have a key first, which you can create with `ssh-keygen`

Note: these commands may change depending on the linux distribution.

