Best Python code snippet using lisa_python
hyperv.py
Source:hyperv.py  
...115        self.node.tools[PowerShell].run_cmdlet(116            f"New-VMSwitch -Name {name} -SwitchType Internal",117            force_run=True,118        )119    def get_switch_interface_index(self, switch_name: str) -> int:120        output = self.node.tools[PowerShell].run_cmdlet(121            f"(Get-NetAdapter -Name '*{switch_name}*').ifindex",122            force_run=True,123        )124        return int(output.strip())125    def exists_nat(self, name: str) -> bool:126        output = self.node.tools[PowerShell].run_cmdlet(127            f"Get-NetNat -Name {name}",128            fail_on_error=False,129            force_run=True,130        )131        return output.strip() != ""132    def delete_nat(self, name: str) -> None:133        if self.exists_nat(name):134            self.node.tools[PowerShell].run_cmdlet(135                f"Remove-NetNat -Name {name} -Confirm:$false",136                force_run=True,137            )138    def create_nat(self, name: str, ip_range: str) -> None:139        # delete NAT if it exists140        self.delete_nat(name)141        # create a new NAT142        self.node.tools[PowerShell].run_cmdlet(143            f"New-NetNat -Name {name} -InternalIPInterfaceAddressPrefix '{ip_range}' ",144            force_run=True,145        )146    def delete_nat_networking(self, switch_name: str, nat_name: str) -> None:147        # Delete switch148        self.delete_switch(switch_name)149        # delete NAT150        self.delete_nat(nat_name)151    def setup_nat_networking(self, switch_name: str, nat_name: str) -> None:152        """153        Setup NAT networking154        Reference: https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/setup-nat-network # noqa155        """156        # create a new switch157        self.create_switch(switch_name)158        # find interface index of the switch159        interface_index = self.get_switch_interface_index(switch_name)160        # set switch interface as gateway for NAT161        self.node.tools[PowerShell].run_cmdlet(162            "New-NetIPAddress -IPAddress 192.168.5.1 "163            f"-InterfaceIndex {interface_index} -PrefixLength 24",164            force_run=True,165        )166        # create a new NAT167        self.create_nat(nat_name, "192.168.5.0/24")168    def get_ip_address(self, name: str) -> str:169        # verify vm is running170        assert_that(self.exists_vm(name)).is_true()171        # get vm ip address172        output = self.node.tools[PowerShell].run_cmdlet(173            f"Get-VM -Name {name} | Select -ExpandProperty networkadapters | select"...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!!
