def test_scenario3(self): '''Scenario 3.''' h=Hosts(self.hostsfile) h['localhost'] = '10.0.0.3' h['10.0.0.3'] ='toto' del h['toto'] del h['broadcasthost'] # Add a name to an address, change the address of a hostname h['localhost'] = '127.0.0.1' h.append('localhost', 'localhost.yemen') del h['127.0.0.1'] h.write(self.emptyfile) correct = """## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 1.0.0.1 localhost localhost.yemen # very pertinent comment ::1 localhost fe80::1%lo0 localhost 1.0.0.2 mc02 """ self.assertEqual(file(self.emptyfile).read(), '')
def test_write(self): '''Write''' h=Hosts() h['localhost'], h['mc02'] = '1.0.0.1', '1.0.0.2' h.write(self.emptyfile) h.write(self.emptyfile) correct = '1.0.0.1\t\t\tlocalhost\n1.0.0.2\t\t\tmc02\n' self.assertEqual(file(self.emptyfile).read(), correct)
def test_purge_empty_records(self): with open(self.hosts_file, 'a') as f: f.write('1.2.3.4') self.assertHasHostLine('1.2.3.4') hosts = Hosts(self.hosts_file) hosts.set_one("test", "1.2.3.4") hosts.set_one("", "1.2.3.4") hosts.write(self.hosts_file) self.assertDoesNotHaveHostLine('1.2.3.4')
class HostManipulationTestCase(unittest.TestCase): def setUp(self): fh, self.hosts_file = tempfile.mkstemp() self.hosts = Hosts(self.hosts_file) def tearDown(self): os.unlink(self.hosts_file) def assertHasHostLine(self, host_line): if not has_host_line(self.hosts_file, host_line): raise AssertionError("Line not present: {0}".format(host_line)) def assertDoesNotHaveHostLine(self, host_line): if has_host_line(self.hosts_file, host_line): raise AssertionError("Line is present: {0}".format(host_line)) def test_set_one(self): self.hosts.set_one("test", "1.2.3.4") self.hosts.write(self.hosts_file) self.assertHasHostLine("1.2.3.4 test") def test_set_all(self): self.hosts.set_all(["test", "alias"], "1.2.3.4") self.hosts.write(self.hosts_file) self.assertHasHostLine("1.2.3.4 test") self.assertHasHostLine("1.2.3.4 alias") def test_remove_one(self): self.hosts.set_one("test", "1.2.3.4") self.hosts.write(self.hosts_file) self.hosts = Hosts(self.hosts_file) self.hosts.remove_one("test") self.hosts.write(self.hosts_file) self.assertDoesNotHaveHostLine("1.2.3.4 test") def test_remove_no_raise(self): self.assertRaises(KeyError, self.hosts.remove_one, "test") self.hosts.remove_one("test", False) def test_purge_empty_records(self): with open(self.hosts_file, 'a') as f: f.write('1.2.3.4') self.assertHasHostLine('1.2.3.4') hosts = Hosts(self.hosts_file) hosts.set_one("test", "1.2.3.4") hosts.set_one("", "1.2.3.4") hosts.write(self.hosts_file) self.assertDoesNotHaveHostLine('1.2.3.4')
def test_update(self): '''Update''' h=Hosts(self.hostsfile, mc02='1.0.0.2', localhost='10.0.0.2') h.write() correct = """## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 10.0.0.2 localhost localhost.yemen # very pertinent comment 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost 1.0.0.2 mc02 """ self.assertEqual(file(self.hostsfile).read(), correct)
fol=os.getcwd() hostsFolder = os.environ['systemroot']+"\\System32\\drivers\\etc"#从系统变量读取 防止出现用户的系统不在C盘的情况 copyfile(hostsFolder+"\\hosts", fol+"\\hosts_bak") if len(good_ips) > 0: fastest_ip= good_ips[0]['ip'] else: fastest_ip= ip_info[0]['ip'] #编码处理 try: open(hostsFolder+"\\hosts", 'r') pass except ValueError: color_print('请尝试将您的Hosts文件保存为UTF-8 with BOM编码',status=1) color_print('您可以尝试将 '+ fastest_ip +' '+host+' 拷贝到hosts文件最后一行,您的Hosts文件路径为 '+hostsFolder+"\\hosts") else: pass #Hosts文件操作 fastHosts = Hosts() fastHosts.remove_all_matching(name=host) new_entry = HostsEntry(entry_type='ipv4', address=fastest_ip, names=[host]) du=fastHosts.add([new_entry]) fastHosts.write() #判断是否修改成功 if cmp(hostsFolder+"\\hosts", fol+"\\hosts_bak") & du['duplicate_count']==0: color_print("好像出现错误了,请尝试手动添加!", status=1) else: color_print("成功添加", status=2) os.system('ipconfig /flushdns') print() input('按回车退出') sys.exit(0)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ __author__ = 'wenin819' """ from hosts import Hosts def_hosts = { "test": "1.2.3.6" } if __name__ == '__main__': import os import argparse if os.name == 'nt': hosts_path = os.path.join(os.environ['SYSTEMROOT'], 'system32/drivers/etc/hosts') elif os.name == 'posix': hosts_path = '/etc/hosts' else: raise Exception('Unsupported OS: %s' % os.name) hosts = Hosts(hosts_path) for (host, val) in def_hosts.items(): hosts.set_one(host, val) hosts.write(hosts_path)