How to use get_remote_public_key method in autotest

Best Python code snippet using autotest_python

ssh_key.py

Source:ssh_key.py Github

copy

Full Screen

...34 public_key = open(public_key_path, 'r')35 public_key_str = public_key.read()36 public_key.close()37 return public_key_str38def get_remote_public_key(session):39 """40 Return a valid string ssh public key for the user executing autoserv or41 autotest. If there's no DSA or RSA public key, create a DSA keypair with42 ssh-keygen and return it.43 :param session: A ShellSession for remote host44 :returns: a ssh public key45 :rtype: str46 """47 session.cmd_output("mkdir -p ~/.ssh")48 session.cmd_output("chmod 700 ~/.ssh")49 ssh_conf_path = "~/.ssh"50 dsa_public_key_path = os.path.join(ssh_conf_path, 'id_dsa.pub')51 dsa_private_key_path = os.path.join(ssh_conf_path, 'id_dsa')52 rsa_public_key_path = os.path.join(ssh_conf_path, 'id_rsa.pub')53 rsa_private_key_path = os.path.join(ssh_conf_path, 'id_rsa')54 dsa_public_s = session.cmd_status("ls %s" % dsa_public_key_path)55 dsa_private_s = session.cmd_status("ls %s" % dsa_private_key_path)56 rsa_public_s = session.cmd_status("ls %s" % rsa_public_key_path)57 rsa_private_s = session.cmd_status("ls %s" % rsa_private_key_path)58 has_dsa_keypair = dsa_public_s == 0 and dsa_private_s == 059 has_rsa_keypair = rsa_public_s == 0 and rsa_private_s == 060 if has_dsa_keypair:61 logging.info('DSA keypair found on %s, using it', session)62 public_key_path = dsa_public_key_path63 elif has_rsa_keypair:64 logging.info('RSA keypair found on %s, using it', session)65 public_key_path = rsa_public_key_path66 else:67 logging.info('Neither RSA nor DSA keypair found, '68 'creating DSA ssh key pair')69 session.cmd('ssh-keygen -t dsa -q -N "" -f %s' % dsa_private_key_path)70 public_key_path = dsa_public_key_path71 return session.cmd_output("cat %s" % public_key_path)72def setup_ssh_key(hostname, user, password, port=22):73 '''74 Setup up remote login in another server by using public key75 :param hostname: the server to login76 :type hostname: str77 :param user: user to login78 :type user: str79 :param password: password80 :type password: str81 :param port: port number82 :type port: int83 '''84 logging.debug('Performing SSH key setup on %s:%d as %s.' %85 (hostname, port, user))86 try:87 public_key = get_public_key()88 session = remote.remote_login(client='ssh', host=hostname, port=port,89 username=user, password=password,90 prompt=r'[$#%]')91 session.cmd_output('mkdir -p ~/.ssh')92 session.cmd_output('chmod 700 ~/.ssh')93 session.cmd_output("echo '%s' >> ~/.ssh/authorized_keys; " %94 public_key)95 session.cmd_output('chmod 600 ~/.ssh/authorized_keys')96 logging.debug('SSH key setup complete.')97 except Exception as err:98 logging.debug('SSH key setup has failed: %s', err)99 try:100 session.close()101 except Exception:102 pass103def setup_remote_ssh_key(hostname1, user1, password1,104 hostname2=None, user2=None, password2=None,105 port=22):106 '''107 Setup up remote to remote login in another server by using public key108 If hostname2 is not supplied, setup to local.109 :param hostname1: the server wants to login other host110 :param hostname2: the server to be logged in111 :type hostname: str112 :param user: user to login113 :type user: str114 :param password: password115 :type password: str116 :param port: port number117 :type port: int118 '''119 logging.debug('Performing SSH key setup on %s:%d as %s.' %120 (hostname1, port, user1))121 try:122 session1 = remote.remote_login(client='ssh', host=hostname1, port=port,123 username=user1, password=password1,124 prompt=r'[$#%]')125 public_key = get_remote_public_key(session1)126 if hostname2 is None:127 # Simply create a session to local128 session2 = aexpect.ShellSession("sh", linesep='\n', prompt='#')129 else:130 session2 = remote.remote_login(client='ssh', host=hostname2,131 port=port, username=user2,132 password=password2,133 prompt=r'[$#%]')134 session2.cmd_output('mkdir -p ~/.ssh')135 session2.cmd_output('chmod 700 ~/.ssh')136 session2.cmd_output("echo '%s' >> ~/.ssh/authorized_keys; " %137 public_key)138 session2.cmd_output('chmod 600 ~/.ssh/authorized_keys')139 logging.debug('SSH key setup on %s complete.', session2)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful