How to use setup_ssh_key method in autotest

Best Python code snippet using autotest_python

ssh_copy_id.py

Source:ssh_copy_id.py Github

copy

Full Screen

...77 try:78 yield cnn79 finally:80 cnn.close()81def setup_ssh_key(cnn):82 '''83 ssh-keygen -t ed2551984 '''85 key0 = ("<>")86 rc = ssh_run(cnn, "mkdir -p ~/.ssh")87 print(view_resp(rc))88 rc = ssh_run(cnn, '''printf "\n{}" >> ~/.ssh/authorized_keys'''.format(key0))89 print(view_resp(rc))90 # remove duplicate91 with cnn.prefix("cd ~/.ssh"):92 ssh_run(cnn, "rm -f bak")93 ssh_run(cnn, "cat authorized_keys | sort | uniq > bak")94 ssh_run(cnn, "mv -f bak authorized_keys")95def main():96 onlines = []97 offlines = []98 try:99 hosts = [100 ]101 for i, host in enumerate(hosts):102 print(f"-[{i}]--------------- {host}")103 sys.stdout.flush()104 for _ in range(2):105 try:106 with connect(host) as cnn:107 setup_ssh_key(cnn)108 sys.stdout.flush()109 onlines.append(host)110 break111 except SSHException as ssherr:112 print(f"{type(ssherr)} {ssherr}")113 continue114 except sotimeout as tmoerr:115 print(f"{type(tmoerr)} {tmoerr}")116 continue117 except soerror as soerr:118 print(f"{type(soerr)} {soerr}")119 continue120 else:121 offlines.append(host)...

Full Screen

Full Screen

auth.py

Source:auth.py Github

copy

Full Screen

...17 for host in c["host_list"]:18 priv_key_path = get_key_path(host)19 if not os.path.exists(priv_key_path):20 print(f"No smurf ssh key found for host '{host}' : setting up new key...")21 setup_ssh_key(host)22 else:23 print(f"Smurf ssh key found for host '{host}' : '{priv_key_path}'")24 25def get_key_dir():26 """ Return the key directory and make sure it exists.27 Returns28 -------29 str30 Path to the key directory.31 """32 c = config.Config()33 key_dir = c.data["key_dir"]34 if not os.path.isdir(key_dir):35 os.makedirs(key_dir)36 return key_dir37def get_key_path(host):38 """ Return the path the private key.39 Parameters40 ----------41 host : str42 Hostname of the remote host.43 Returns44 -------45 str46 Path to private key file.47 """48 key_dir = get_key_dir()49 priv_key_path = os.path.join(key_dir, "id_rsa_smurf_" + host)50 return priv_key_path51def generate_ssh_key(host):52 """ Generate a ssh key to allow smurf to connect to host.53 Keys are stored in the ~/.smurf directory under the keys directory.54 Parameters55 ----------56 host : str57 Hostname of the remote host to setup keys for.58 """59 priv_key_path = get_key_path(host)60 comment = "'limited smurf access from {}@{}'".format(61 os.environ["USER"], os.uname()[1])62 subprocess.run(["ssh-keygen", "-C", comment, "-N",63 "", "-f", priv_key_path], check=True)64def get_pub_key(host):65 """ Get the public key data for a host. 66 Parameters67 ----------68 host : str69 Hostname of the remote host.70 Returns71 -------72 str73 Public key and comment.74 """75 pub_key_path = get_key_path(host) + ".pub"76 with open(pub_key_path, "r") as infile:77 content = infile.read().strip()78 return content79def copy_ssh_key(host):80 """ Copy the smurf key to the remote host and setup command restriction.81 This is done by preparing a temporary file and copying82 its content using ssh-copy-id.83 Parameters84 ----------85 host : str86 Remote host to register key on.87 """88 preprend = 'command="~/.local/bin/smurfnet-shell",permitopen="localhost:*",no-agent-forwarding,no-pty,no-user-rc,no-X11-forwarding'89 content = preprend + " " + get_pub_key(host)90 pub_key_path = get_key_path(host) + ".pub"91 os.rename(pub_key_path, pub_key_path + ".bak")92 with open(pub_key_path, "w") as outfile:93 outfile.write(content)94 subprocess.run(["ssh-copy-id", "-f", "-i", pub_key_path, host], check=True)95 os.rename(pub_key_path + ".bak", pub_key_path)96def setup_ssh_key(host):97 """ Generates a key for smurf and copy it to the remote host.98 Parameters99 ----------100 host : str101 Hostname of the remote host.102 """103 logging.info(f"Setting up new ssh key for host '{host}'")104 generate_ssh_key(host)105 copy_ssh_key(host)106def ensure_key(host):107 """ Create a key if it does not exists and then return the path to it.108 109 Attempt to create the key once.110 Parameters111 ----------112 str: host113 Name of the host.114 Returns115 -------116 str117 Path to the private key.118 """119 key_path = get_key_path(host)120 if not os.path.exists(key_path):121 setup_ssh_key(host)122 return key_path123if __name__ == "__main__":...

Full Screen

Full Screen

fabfile.py

Source:fabfile.py Github

copy

Full Screen

...33 '&& chmod 600 /root/.ssh/apitrary-staging-deploy')34def add_ssh_config():35 put('./assets/ssh_config', '/tmp/ssh_config')36 sudo('cat /tmp/ssh_config >> /root/.ssh/config')37def setup_ssh_key():38 sudo('mkdir -p /root/.ssh')39 execute(copy_public_ssh_key)40 execute(copy_secret_ssh_key)41 execute(add_ssh_config)42# ROLES-BASED CALLS43#44#45@roles('TEST')46def test_setup():47 execute(setup_ssh_key)48@roles('STAGING')49def staging_setup():50 execute(setup_ssh_key)51@roles('PRODUCTION')...

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