def set_connection_mode_automatic_with_command():
     run_command(
         "mclient --quiet set root/Network/Wired/Method 'Automatic' &&"
         "mclient --quiet set root/Network/Wired/IPAddress '' &&"
         "mclient --quiet set root/Network/Wired/DefaultGateway '' &&"
         "mclient --quiet set root/Network/Wired/SubnetMask '' &&"
         "mclient commit")
 def __set_vdi(self):
     front_command = "mclient --quiet set root/ConnectionType/"
     command = ""
     if not self.vdi_id:
         raise CaseRunningError("No VDI Select")
     if self.settings:
         for key, values in self.settings.items():
             joined_command = front_command + "{}/connections/{}/{} '{}' && ".format(
                 self.vdi, self.vdi_id, key, values)
             command += joined_command
     command += "mclient commit"
     run_command(commands=command, timeout=30)
 def delete_vdi(cls, vdi):
     log.info("start delete {} uuid".format(vdi))
     citrix_id = cls.vdi_connection_id(vdi)
     if len(citrix_id) > 0:
         for c_id in citrix_id:
             run_command(
                 "mclient --quiet delete root/ConnectionType/{}/connections/{} && "
                 "mclient commit".format(vdi, c_id))
             time.sleep(0.2)
         if len(cls.vdi_connection_id('vdi')) == 0:
             log.info("delete {} uuid success".format(vdi))
     else:
         log.info("not exist {}, not need to delete".format(vdi))
         pass
 def check_dns_info_after_reboot(self):
     """
     Index: 1
     """
     domains = run_command(
         "mclient --quiet get root/Network/SearchDomains").strip()
     servers = run_command(
         "mclient --quiet get root/Network/DNSServers").strip()
     if domains != self.format_search_domains:
         raise CaseRunningError(
             f"Chcek SearchDomains Fail! Expect: {self.format_search_domains} Actual: {domains}"
         )
     if servers != self.format_dns_server:
         raise CaseRunningError(
             f"Chcek DNSServers Fail! Expect: {self.format_dns_server} Actual: {servers}"
         )
 def check_frequency(freq=2.4):
     response = run_command("sudo iwlist wlan0 channel")
     result = re.search(r"(?i)Current Frequency.*?([\d\.]+).*?GHZ.*?",
                        response)
     if result:
         frequency = float(result.group(1))
         if abs(frequency - freq) < 1:
             return
         raise CaseRunningError(
             f"Check Frequency Fail! Current: {frequency} Expect: {freq}")
     raise CaseRunningError("Wireless Connect Fail")
 def close_Cert_Window(self):
     run_command("wmctrl -c 'Certificate Authority Certificate' ")
 def force_logoff(self):
     command = f'sudo connection-mgr kill {self.vdi_id}'
     run_command(command)
 def logoff(self):
     command = f'sudo connection-mgr stop {self.vdi_id}'
     run_command(command)
     time.sleep(5)
     if self.is_active():
         self.force_logoff()
 def is_active(self):
     command_list_active = f'connection-mgr listActive'
     response = run_command(command_list_active)
     if self.vdi_id not in response:
         return False
     return True
def ping(ip, count=10):
    result = run_command(f"ping {ip} -c {count}", timeout=30)
    fail_rate = re.search(r",.*?(\d+)% packet loss", result)
    if fail_rate and int(fail_rate.group(1)) < 99:
        return True
    return False
 def wired_wireless_switch_off():
     run_command(
         "mclient set root/Network/WiredWirelessSwitch 0 && mclient commit")
 def disabled_wired():
     run_command(
         "sudo /usr/lib/hptc-network-mgr/common/netmgr_wired --down eth0 && ifconfig eth0 down"
     )
 def enable_wired():
     run_command(
         "sudo /usr/lib/hptc-network-mgr/common/netmgr_wired --up eth0 && ifconfig eth0 up"
     )
 def restore_dns_callback(self):
     run_command("mclient --quiet set root/Network/SearchDomains '' && "
                 "mclient --quiet set root/Network/DNSServers '' && "
                 "mclient commit")