Best Python code snippet using lisa_python
set_hostinfo_by_nic.py
Source:set_hostinfo_by_nic.py
1#coding:utf-82#æ¬èæ¬ç¨äºéè¿macå°åæ¥è®¾ç½®ipå°å3import subprocess4import re5hostname_domain='.alv.pub'6hostname={}7hostname['2']='ipa' #ipa æå¡ãå
æ¬ldapï¼dnsæå¡ã8hostname['3']='internal' #ç©çæº ææèææºé½æ¯å®è£
å¨è¿é9hostname['4']='zabbix' #zabbixçæ§10hostname['5']='jenkins' #èªå¨åæµç¨11hostname['6']='git' #代ç æå¡å¨12hostname['9']='meta' #å
¶ä»èææºè¦å建çå¾æ¶åï¼æ·è´metaççãç¶åä¿®æ¹ç³»ç»é
置导å
¥ä¸ºæ°çèææº13hostname['10']='mysql_' #mysqlå端14hostname['11']='mysql1'15hostname['12']='mysql2'16hostname['13']='mysql3'17hostname['20']='redis' #rediså端18hostname['21']='redis1'19hostname['22']='redis2'20hostname['23']='redis3'21hostname['30']='mongodb' #mongodb å端22hostname['31']='mongodb1'23hostname['32']='mongodb2'24hostname['33']='mongodb3'25hostname['41']='test1'26hostname['42']='test2'27hostname['43']='test3'28hostname['44']='test4'29hostname['45']='test5'30hostname['46']='test6'31hostname['47']='test7'32hostname['48']='test8'33hostname['49']='test9'34hostname['50']='w7'35hostname['51']='w10'36hostname['52']='kali'37hostname['60']='mysql'38hostname['61']='redis'39hostname['68']='centos6u8'40hostname['74']='centos7u4'41hostname['73']='centos7u3'42hostname['81']='k8s1'43hostname['82']='k8s2'44hostname['83']='k8s3'45hostname['84']='k8s4'46hostname['90']='vpnserver'47hostname['91']='ubuntu14u4'48hostname['92']='ubuntu16u4'49hostname['93']='ubuntu18u4'50#è·åmacå°å51get_nic=subprocess.check_output("ip a s|grep ether|awk '{print $2}'",shell=True).split('\n')[0]52#è·åmacå°åæåä¸ä½53tail_1=get_nic.split(':')[-1]54#å¦ææåä¸æ®µæ°çå¼å¤´æ¯0ï¼å»æ055if tail_1[0] == '0':tail_1=tail_1[1]56#è·åmacå°ååæ°ç¬¬äºä½57tail_2=get_nic.split(':')[-2]58#Get mac tail 3 number59tail_3=get_nic.split(':')[-2]60if tail_2 == '01':tail_1='1'+tail_161#设置ipçç½ç»ä¿¡æ¯62sysinfo={}63sysinfo['ip']='192.168.3.%s'%tail_164sysinfo['gw']='192.168.3.3'65sysinfo['dns']='192.168.3.2'66sysinfo['dns_search']='alv.pub'67#sysinfo['nic']=subprocess.check_output("ip a s|grep state|grep -v lo|awk -F: '{print $2}'|sed 's/ //'",shell=True).split('\n')[0]68sysinfo['nic']=re.sub(r'(GENERAL.CONNECTION:\s+)','',subprocess.check_output("nmcli device show |grep -i CONNECTION|head -1",shell=True).split('\n')[0])69sysinfo['hostname']=hostname[tail_1]+hostname_domain70#设置ipå°å71def set_ip_info():72 if subprocess.call('nmcli connection modify "{nic}" ipv4.method manual ipv4.addresses {ip}/24 ipv4.gateway {gw} ipv4.dns {dns} ipv4.dns-search {dns_search} autoconnect yes && nmcli con up "{nic}"'.format(**sysinfo),shell=True) == 0:73 print('IP address has heen setup ok')74 else:75 print('IP address setup failed.')76def set_hostnaem():77 if subprocess.call('hostnamectl set-hostname %s'%sysinfo['hostname'],shell=True) == 0:78 print('Hostname setup ok')79 else:80 print('Hostname setup failed.')81def main():82 set_ip_info()83 set_hostnaem()84if __name__ == '__main__':...
set_hostinfo_by_nic_10.py
Source:set_hostinfo_by_nic_10.py
1#coding:utf-82#æ¬èæ¬ç¨äºéè¿macå°åæ¥è®¾ç½®ipå°å3import subprocess4import re5hostname_domain='.alv.pub'6hostname={}7hostname['2']='ipa' #ipa æå¡ãå
æ¬ldapï¼dnsæå¡ã8hostname['3']='internal' #ç©çæº ææèææºé½æ¯å®è£
å¨è¿é9hostname['4']='zabbix' #zabbixçæ§10hostname['5']='jenkins' #èªå¨åæµç¨11hostname['6']='git' #代ç æå¡å¨12hostname['9']='meta' #å
¶ä»èææºè¦å建çå¾æ¶åï¼æ·è´metaççãç¶åä¿®æ¹ç³»ç»é
置导å
¥ä¸ºæ°çèææº13hostname['10']='mysql_' #mysqlå端14hostname['11']='mysql1'15hostname['12']='mysql2'16hostname['13']='mysql3'17hostname['20']='redis' #rediså端18hostname['21']='redis1'19hostname['22']='redis2'20hostname['23']='redis3'21hostname['30']='mongodb' #mongodb å端22hostname['31']='mongodb1'23hostname['32']='mongodb2'24hostname['33']='mongodb3'25hostname['50']='w7'26hostname['51']='w10'27hostname['60']='mysql'28hostname['61']='redis'29hostname['68']='centos6u8'30hostname['74']='centos7u4'31hostname['73']='centos7u3'32hostname['81']='k8s1'33hostname['82']='k8s2'34hostname['83']='k8s3'35hostname['84']='k8s4'36hostname['90']='vpnserver'37#è·åmacå°å38get_nic=subprocess.check_output("ip a s|grep ether|awk '{print $2}'",shell=True).split('\n')[0]39#è·åmacå°åæåä¸ä½40tail_1=get_nic.split(':')[-1]41#å¦ææåä¸æ®µæ°çå¼å¤´æ¯0ï¼å»æ042if tail_1[0] == '0':tail_1=tail_1[1]43#è·åmacå°ååæ°ç¬¬äºä½44tail_2=get_nic.split(':')[-2]45#Get mac tail 3 number46tail_3=get_nic.split(':')[-2]47if tail_2 == '01':tail_1='1'+tail_148#设置ipçç½ç»ä¿¡æ¯49sysinfo={}50sysinfo['ip']='192.168.3.%s'%tail_151sysinfo['gw']='192.168.3.3'52sysinfo['dns']='192.168.3.2'53sysinfo['dns_search']='alv.pub'54#sysinfo['nic']=subprocess.check_output("ip a s|grep state|grep -v lo|awk -F: '{print $2}'|sed 's/ //'",shell=True).split('\n')[0]55sysinfo['nic']=re.sub(r'(GENERAL.CONNECTION:\s+)','',subprocess.check_output("nmcli device show |grep -i CONNECTION|head -1",shell=True).split('\n')[0])56sysinfo['hostname']=hostname[tail_1]+hostname_domain57#设置ipå°å58def set_ip_info():59 if subprocess.call('nmcli connection modify "{nic}" ipv4.method manual ipv4.addresses {ip}/24 ipv4.gateway {gw} ipv4.dns {dns} ipv4.dns-search {dns_search} autoconnect yes && nmcli con up "{nic}"'.format(**sysinfo),shell=True) == 0:60 print('IP address has heen setup ok')61 else:62 print('IP address setup failed.')63def set_hostnaem():64 if subprocess.call('hostnamectl set-hostname %s'%sysinfo['hostname'],shell=True) == 0:65 print('Hostname setup ok')66 else:67 print('Hostname setup failed.')68def main():69 set_ip_info()70 set_hostnaem()71if __name__ == '__main__':...
tests_helpers.py
Source:tests_helpers.py
...3class HelperTestCase(TestCase):4 siret = "123456789000"5 def test_get_siren(self):6 self.assertEqual(get_siren(self.siret), "123456789")7 def test_get_nic(self):...
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!!