updated get_ipv4

This commit is contained in:
Mathieu Broillet 2023-06-16 16:04:09 +02:00
parent 7d124cf751
commit cc408237d9
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32
2 changed files with 14 additions and 2 deletions

View File

@ -172,14 +172,25 @@ class LXC(LinuxMachine):
""" """
return self.bridge return self.bridge
def get_ipv4(self): def get_ipv4(self, netmask: bool = False):
""" """
Get IPv4 Get IPv4
:return: ipv4 :return: ipv4
""" """
if self.ipv4 == "dhcp": if self.ipv4 == "dhcp":
if self.is_running(): if self.is_running():
return super().get_ipv4() if self.has_program("ip"):
if netmask:
ip = self.run_command(
"""ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 """)
return ip
ip = self.run_command(
"""ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'""")
return ip
elif self.has_program("ifconfig"):
return self.run_command(command="ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'")
return self.ipv4 return self.ipv4

View File

@ -29,6 +29,7 @@ class LinuxMachine():
return self.run_command("free -m | grep Mem | awk '{print $2}'") return self.run_command("free -m | grep Mem | awk '{print $2}'")
def get_ipv4(self): def get_ipv4(self):
"""Get IPv4 address"""
if self.has_program("ip"): if self.has_program("ip"):
return self.run_command("""ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'""") return self.run_command("""ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'""")
elif self.has_program("ifconfig"): elif self.has_program("ifconfig"):