Best Python code snippet using autotest_python
cluster-prep-service.py
Source:cluster-prep-service.py
...6import sys7BASEDIR = "/usr/local/cluster-prep/"8def log(msg):9 print("[cluster-prep-service]: "+msg)10def determine_hostname(update_hostname=False):11 if 'INSTANCE_ROLE' in os.environ:12 role = os.environ['INSTANCE_ROLE']13 else:14 ip_addr = None15 for device in ["eth0", "ens3", "ens0", "ens1", "ens2", "ens3", "ens4", "ens5", "ens6", "ens7"]:16 try:17 ipcmd = subprocess.check_output(["ip", "addr", "show", "dev", device]).decode("utf-8")18 except:19 continue20 print(ipcmd)21 ipcmd = [line.strip().split(" ")[1].split("/")[0] for line in ipcmd.split("\n") if22 line.strip().startswith("inet ")]23 ip_addr = ipcmd[0]24 break25 if ip_addr is None:26 log("Failed to determine IP address")27 exit(-1)28 log("Determined IP Address: %s" % (ip_addr))29 etchlines = open("/etc/hosts", "r").readlines()30 for line in etchlines:31 parts = re.split(r"\s+", line)32 if parts[0]==ip_addr:33 hostname = parts[1]34 if update_hostname:35 log("Setting hostname to %s")36 os.system("hostname %s" % (hostname))37 else:38 log("Determined real hostname as %s" % (hostname))39 return hostname40def is_locked(lock):41 if os.path.isfile(BASEDIR + "locks/%s.lock" % (lock)):42 return True43 return False44def do_lock(lock, status="locked"):45 os.makedirs(BASEDIR + "locks", exist_ok=True)46 with open(BASEDIR + "locks/%s.lock" % (lock), "w") as fh:47 fh.write(status)48def execute(script):49 log("running " + script)50 if script.endswith(".sh"):51 try:52 subprocess.check_output(["/bin/bash", script])53 log("finished running " + script)54 return "OK"55 except Exception as e:56 log("Failed to run script %s" % (script))57 import traceback58 traceback.print_exc()59 return "Exception: " + str(e)60 elif script.endswith(".py"):61 try:62 subprocess.check_output(["/usr/bin/python3", script])63 log("finished running " + script)64 return "OK"65 except Exception as e:66 log("Failed to run script %s" % (script))67 import traceback68 traceback.print_exc()69 return "Exception: " + str(e)70 else:71 try:72 subprocess.check_output([script])73 log("finished running " + script)74 return "OK"75 except Exception as e:76 log("Failed to run script %s" % (script))77 import traceback78 traceback.print_exc()79 return "Exception: "+str(e)80def main():81 hostname = determine_hostname(True)82 role = hostname83 log("cluster-prep start")84 if role.startswith("node"):85 role = "node"86 log("Determined role: %s" % (role))87 systemd.daemon.notify('READY=1')88 setup_scripts = sorted(glob.glob(BASEDIR + "/setup-scripts/%s/*" % (role)))89 boot_scripts = sorted(glob.glob(BASEDIR + "/setup-scripts/%s/*" % (role)))90 for script in setup_scripts:91 sname = os.path.basename(script)92 if not is_locked(sname):93 log("Executing setup script %s" % (sname))94 do_lock(role + "." + sname, execute(script))95 for script in boot_scripts:...
test_utilities.py
Source:test_utilities.py
...10foo = bar11potato = pancake12""".strip()13def test_display_name():14 assert util.determine_hostname(display_name='foo') == 'foo'15def test_determine_hostname():16 import socket17 hostname = socket.gethostname()18 fqdn = socket.getfqdn()19 assert util.determine_hostname() == (hostname or fqdn)20 assert util.determine_hostname() != 'foo'21def test_get_time():22 time_regex = re.match('\d{4}-\d{2}-\d{2}\D\d{2}:\d{2}:\d{2}\.\d+',23 util.get_time())24 assert time_regex.group(0) is not None25def test_write_to_disk():26 content = 'boop'27 filename = '/tmp/testing'28 util.write_to_disk(filename, content=content)29 assert os.path.isfile(filename)30 with open(filename, 'r') as f:31 result = f.read()32 assert result == 'boop'33 util.write_to_disk(filename, delete=True) is None34def test_generate_machine_id():...
dnsrecon_nmap.py
Source:dnsrecon_nmap.py
2host_ip_dict = {}3all_ip = []4# Determine ip is shared by which hostname5# Return a list of shared ip hostname6# print determine_hostname('1.2.3.4',_host_ip_dict)7#['www.esea.net', 'tv.esea.net', 'db.esea.net', 'play.esea.net', 'stem.esea.net', 'news.esea.net']8def determine_hostname(_ip,_host_ip_dict):9 shared_hostname = []10 for key in _host_ip_dict:11 for i in _host_ip_dict[key]:12 if i == _ip:13 shared_hostname.append(key)14 return shared_hostname15def fill_host_ip_dict():16 f = open('subdomains','r')17 arr = []18 for line in f.readlines():19 line = line.strip()20 arr = line.split(' ')21 ip = arr[4]22 hostname = arr[3]23 if host_ip_dict.has_key(hostname) == False:24 host_ip_dict[hostname] = [ip]25 else:26 old_ip_list = host_ip_dict[hostname]27 old_ip_list.append(ip)28 host_ip_dict[hostname] = old_ip_list29 f.close()30 for key in host_ip_dict:31 for i in host_ip_dict[key]:32 all_ip.append(i)33def main():34 fill_host_ip_dict()35 ip_set = set(all_ip)36 #Start nmap scan37 for i in list(ip_set):38 print "Start nmap scan with " + i39 hostname_shared = determine_hostname(i,host_ip_dict)40 pretty_hostname_shared = ""41 for j in hostname_shared:42 pretty_hostname_shared = j + "X" + pretty_hostname_shared43 output = subprocess.check_output('nmap --top-ports 1000 -oS %s %s' % (pretty_hostname_shared+i,i), shell=True)44 print output...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!