2

I am trying to connect to a specific central server that has configured passwordless connection to all other servers, the server I am currently on has no access to the ones I want to run commands. So I am trying to connect to central server and from there do ssh into other servers I need to run commands. When I run this after I execute ssh command program gets frozen and does not allow to execute commands into final remote server. In this case let's say I want to run ifconfig on final server 'host.name'.

def get_host_info_linux(self,host,db_engine):
    #Create ssh client from paramiko library
    client = paramiko.SSHClient()

    try:
        # Connect to remote host
        #logger.info(username_pass)
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname='tunel host name', username=f'{db_engine}')
        #this command is to do ssh into the server I want to execute commands
        ssh_cmd = f'ssh {host.name}'

        ssh_std = client.exec_command(ssh_cmd)
        if (ssh_std[2].readlines() == []):+

            logger.debug(ssh_std[1].readlines()[0])

        else:
            logger.error(ssh_std[2].readlines())

      client.exe_command('ifconfig')

    except Exception as e:
        logging.error(e)
    finally:
        client.close()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

0
  • In general to execute commands on server to which you can connect only via another SSH server, you should use port forwarding:
    Nested SSH using Python Paramiko

  • But if I understood correctly, you do not want to authenticate from the local machine, as you have the authentication set up from the jump host (do you have a private key stored on the jump host?) In that case, you want this:
    Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko. Or you can bring the key from the jump host to the local machine. See Nested SSH with RSA key file.

  • Though instead of feeding the commands to ssh via a redirected input, you might also use ssh command line as a more standardized interface:

    ssh destination command
    
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992