How to use file_contains_pattern method in autotest

Best Python code snippet using autotest_python

base_utils.py

Source:base_utils.py Github

copy

Full Screen

...179 return dest180def force_link(src, dest):181 """Link src to dest, overwriting it if it exists"""182 return utils.system("ln -sf %s %s" % (src, dest))183def file_contains_pattern(file, pattern):184 """Return true if file contains the specified egrep pattern"""185 if not os.path.isfile(file):186 raise NameError('file %s does not exist' % file)187 cmd_result = utils.run('egrep -q "' + pattern + '" ' + file,188 ignore_status=True, verbose=False)189 return not cmd_result.exit_status190def list_grep(list, pattern):191 """True if any item in list matches the specified pattern."""192 compiled = re.compile(pattern)193 for line in list:194 match = compiled.search(line)195 if (match):196 return 1197 return 0198def get_os_vendor():199 """200 Try to guess what's the os vendor.201 """202 logging.warn('utils.get_os_vendor() is deprecated, please use '203 'autotest.client.shared.distro.detect() instead')204 vendor = 'Unknown'205 if os.path.isfile('/etc/SuSE-release'):206 return 'SUSE'207 issue = '/etc/issue'208 if not os.path.isfile(issue):209 return vendor210 if file_contains_pattern(issue, 'Red Hat'):211 vendor = 'Red Hat'212 if file_contains_pattern(issue, 'CentOS'):213 vendor = 'Red Hat'214 elif file_contains_pattern(issue, 'Fedora'):215 vendor = 'Fedora'216 elif file_contains_pattern(issue, 'SUSE'):217 vendor = 'SUSE'218 elif file_contains_pattern(issue, 'Ubuntu'):219 vendor = 'Ubuntu'220 elif file_contains_pattern(issue, 'Debian'):221 vendor = 'Debian'222 logging.debug("Detected OS vendor: %s", vendor)223 return vendor224def get_cc():225 try:226 return os.environ['CC']227 except KeyError:228 return 'gcc'229def get_vmlinux():230 """Return the full path to vmlinux231 Ahem. This is crap. Pray harder. Bad Martin.232 """233 vmlinux = '/boot/vmlinux-%s' % os.uname()[2]234 if os.path.isfile(vmlinux):...

Full Screen

Full Screen

6. securityChecker.py

Source:6. securityChecker.py Github

copy

Full Screen

...36 os.system("rm f result")37 38def adjust_release_user_existence():39 if get_redhat_release() == "RedHat 6":40 if file_contains_pattern("/etc/sudoers", target_user):41 print(target_user + " is added as sudo.")42 else:43 print(target_user + " does not exist as sudo.")44 elif get_redhat_release() == "RedHat 7":45 check_user_being_part_of_group(target_user, target_group)46 else:47 print("Sorry, the script does not support your current RedHat release")48 49def file_contains_pattern(file_name, *pattern):50 result = ([x for x in pattern if x in open(file_name).read()])51 return len(result) == len(pattern)52 53def check_for_snow_client_installation(destination="/etc/snowclient.conf"):54 if os.path.isfile(destination):55 with open(destination, 'r') as f:56 first_line = f.readline().split("//")57 web_server_ip = first_line[len(first_line)-1]58 print("Snow agent is installed.")59 print("The web server to which Snow Agent reports: " + web_server_ip)60 else:61 print("Sorry, " + destination + " does not exist as a file.")62def check_ssh_session_timeout(file_name="/etc/ssh/sshd_config", first_property="ClientAliveInterval 7200", \63second_property="ClientAliveCountMax 0"):64 if file_contains_pattern(file_name, first_property, second_property):65 print("The SSH session time out is configured properly.")66 else:67 print("SECURITY BRIDGE => ssh session time out is not configured according to security policy.")68 69def check_pam_policy_settings(file_name="/etc/pam.d/password-auth-hpes", first_property="deny=5", \70second_property="password requisite pam_cracklib.so try_first_pass reject_username retry=3 type= minlen=8 lcredit=-1 ucredit=-1 dcredit=-1 difok=3 ocredit=-1 maxrepeat=2 minclass=3", \71third_property="password sufficient pam_unix.so sha512 shadow try_first_pass use_authtok remember=5"):72 if file_contains_pattern(file_name, first_property, second_property, third_property):73 print("The PAM settings are configured properly.")74 else:75 print("SECURITY BRIDGE => pam settings are not configured properly")76def check_se_linux_being_disabled(file_name=["/etc/sysconfig/selinux", "/etc/selinux/config"], first_property='SELINUX=disabled'):77 if file_contains_pattern(file_name[0], first_property) and file_contains_pattern(file_name[1], first_property):78 print("SELINUX is configured properly.")79 else:80 print("SELINUX is not disabled.")81 82def main():83 adjust_release_user_existence()84 check_ssh_session_timeout()85 check_pam_policy_settings()86 check_for_snow_client_installation()87 check_se_linux_being_disabled()88if __name__ == "__main__":...

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