-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ecebff
commit 0e12821
Showing
2 changed files
with
30 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,60 @@ | ||
""" | ||
This module provides functions for enabling/disabling IP forwarding | ||
and retrieving the IP address of the current machine. | ||
""" | ||
|
||
import subprocess | ||
import socket | ||
import core.platform as platform | ||
from core import platform | ||
|
||
|
||
def enable_ip_forwarding(): | ||
""" | ||
Enables IP forwarding on the current machine. | ||
IP forwarding is enabled based on the operating system type | ||
(macOS, Linux, or Windows). | ||
""" | ||
os_platform = platform.get_os() | ||
|
||
cmd = None | ||
if os_platform == 'mac': | ||
cmd = ['/usr/sbin/sysctl', '-w', 'net.inet.ip.forwarding=1'] | ||
elif os_platform == 'linux': | ||
cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=1'] | ||
elif os_platform == 'windows': | ||
cmd = ['powershell', 'Set-NetIPInterface', '-Forwarding', 'Enabled'] | ||
|
||
else: | ||
raise ValueError(f"Unsupported OS platform: {os_platform}") | ||
assert subprocess.call(cmd) == 0 | ||
|
||
|
||
def disable_ip_forwarding(): | ||
""" | ||
Disables IP forwarding on the current machine. | ||
IP forwarding is disabled based on the operating system type | ||
(macOS, Linux, or Windows). | ||
""" | ||
os_platform = platform.get_os() | ||
|
||
cmd = None | ||
if os_platform == 'mac': | ||
cmd = ['/usr/sbin/sysctl', '-w', 'net.inet.ip.forwarding=0'] | ||
elif os_platform == 'linux': | ||
cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=0'] | ||
elif os_platform == 'windows': | ||
cmd = ['powershell', 'Set-NetIPInterface', '-Forwarding', 'Disabled'] | ||
else: | ||
raise ValueError(f"Unsupported OS platform: {os_platform}") | ||
|
||
assert subprocess.call(cmd) == 0 | ||
|
||
def get_ip_address(): | ||
"""Get the IP address of the current machine.""" | ||
try: | ||
# Create a socket object | ||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: | ||
# Connect to a remote server (doesn't have to be reachable) | ||
s.connect(("8.8.8.8", 80)) | ||
# Get the IP address of the current machine | ||
ip_address = s.getsockname()[0] | ||
return ip_address | ||
except Exception as e: | ||
except OSError as e: | ||
print(f"Error while getting IP address: {e}") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters