Aggiornato Wiki

This commit is contained in:
Miguel 2025-04-29 12:23:08 +02:00
parent f8b13c7bab
commit 099af4eb4d
7 changed files with 183 additions and 282 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ __pycache__/
# C extensions
*.so
*.json
# Distribution / packaging
.Python

View File

@ -1,22 +0,0 @@
import json
IP_HISTORY_FILE = "tu_ruta_a_ip_history.json" # Asegúrate de definir la ruta correcta
def remove_duplicates_preserve_order(history):
"""Elimina duplicados de la lista manteniendo el orden."""
seen = set()
new_history = []
for item in history:
if item not in seen:
seen.add(item)
new_history.append(item)
return new_history
if os.path.exists(IP_HISTORY_FILE):
with open(IP_HISTORY_FILE, "r") as file:
ip_history = json.load(file)
ip_history = remove_duplicates_preserve_order(ip_history)
# Guardar la lista actualizada en el archivo JSON
with open(IP_HISTORY_FILE, "w") as file:
json.dump(ip_history, file)

View File

@ -1,203 +0,0 @@
import ctypes
import subprocess
import sys
import re
import os
import json
import time
from win10toast import ToastNotifier
import threading
# Definir una constante para la ruta del directorio
DIR = "D:\\Proyectos\\Scripts\\IPChanger"
IP_HISTORY_FILE = os.path.join(DIR, "ip_history.json")
def is_admin():
"""Verifica si el script se está ejecutando con privilegios de administrador."""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run_as_admin(argv=None, debug=False):
"""Ejecuta el script como administrador."""
shell32 = ctypes.windll.shell32
if argv is None and shell32.IsUserAnAdmin():
# Ya estamos ejecutando como administrador, no es necesario hacer nada
return True
if argv is None:
argv = sys.argv
if hasattr(sys, "_MEIPASS"):
# Soporte para PyInstaller
arguments = map(str, argv[1:])
else:
arguments = map(str, argv)
argument_line = " ".join(arguments)
executable = str(sys.executable)
if debug:
print(f"Command line: {executable} {argument_line}")
ret = shell32.ShellExecuteW(None, "runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
return None
def set_static_ip(ip_address, gateway):
"""Establece una dirección IP estática para el adaptador Ethernet."""
try:
subprocess.run(
f'netsh interface ip set address "Ethernet" static {ip_address} 255.255.255.0 {gateway}',
shell=True,
)
return f"Dirección IP establecida en {ip_address}"
except Exception as e:
return f"Error al establecer la dirección IP: {e}"
def set_dhcp():
"""Configura el adaptador Ethernet para obtener la dirección IP automáticamente a través de DHCP."""
try:
subprocess.run('netsh interface ip set address "Ethernet" dhcp', shell=True)
return "Adaptador Ethernet configurado para DHCP"
except Exception as e:
return f"Error al configurar DHCP: {e}"
def save_current_ip(ip_history):
"""Guarda la dirección IP actual del adaptador Ethernet en un archivo y detecta si es de DHCP."""
try:
ip_config_output = subprocess.check_output("ipconfig /all", shell=True).decode(
"cp850"
)
# Buscar la sección del adaptador de Ethernet y DHCP
ethernet_config = re.search(
"Adaptador de Ethernet Ethernet:[\s\S]*?(DHCP habilitado[\s\S]*?)\r\n\r\n",
ip_config_output,
)
current_ip = re.search(
"Dirección IPv4. . . . . . . . . . . . . . : (\d+\.\d+\.\d+\.\d+)",
ethernet_config.group(1) if ethernet_config else "",
)
dhcp_enabled = re.search(
"DHCP habilitado . . . . . . . . . . . . . : sí",
ethernet_config.group(1) if ethernet_config else "",
)
if current_ip and not dhcp_enabled:
ip_history.insert(0, current_ip.group(1))
ip_history = ip_history[:10]
return f"Dirección IP actual guardada: {current_ip.group(1)}"
elif dhcp_enabled:
return "La dirección IP actual es obtenida por DHCP."
else:
return "No se encontró la dirección IP actual."
except Exception as e:
return f"Error al obtener la configuración IP: {e}"
def restore_last_ip(ip_history):
"""Restaura la última dirección IP guardada para el adaptador Ethernet."""
if ip_history:
last_ip = ip_history[0] # Obtener la última IP del historial
gateway = last_ip.rsplit(".", 1)[0] + ".1"
return set_static_ip(last_ip, gateway)
else:
return "No hay direcciones IP guardadas en el historial."
def get_input_parameter():
"""Solicita al usuario un parámetro por línea de comandos."""
return input(
"Ingrese la dirección IP, 0-9 para los ultimas IP, 'a' o 'auto' para DHCP, o 'l' o 'last' para la última IP: "
)
def display_ip_history(ip_history):
"""Muestra el historial de las últimas 10 IPs en orden inverso (de la más antigua a la más reciente) y permite al usuario seleccionar una."""
if ip_history:
for i, ip in enumerate(reversed(ip_history)):
print(f"{len(ip_history) - 1 - i}: {ip}")
return None
def remove_duplicates_preserve_order(ip_history):
"""Elimina duplicados de la lista manteniendo el orden."""
seen = set()
new_history = []
for item in ip_history:
if item not in seen:
seen.add(item)
new_history.append(item)
return new_history
def show_notification(message, duration):
toaster = ToastNotifier()
toaster.show_toast("Cambio de IP", message, duration=duration)
def main():
if not is_admin():
# Si no se está ejecutando como administrador, solicitar elevación
result = run_as_admin()
if result is True:
print("Ejecutando como administrador.")
elif result is False:
print("No se pudo obtener privilegios de administrador.")
return
else:
# El script se reinició con privilegios de administrador
return
if os.path.exists(IP_HISTORY_FILE):
with open(IP_HISTORY_FILE, "r") as file:
ip_history = json.load(file)
ip_history = remove_duplicates_preserve_order(ip_history)
else:
ip_history = []
argument = sys.argv[1] if len(sys.argv) > 1 else None
if not argument:
chosen_ip = display_ip_history(ip_history)
argument = get_input_parameter()
if len(argument) == 0:
return
if ip_history:
if len(argument) == 1:
if argument.isdigit() and 0 <= int(argument) < len(ip_history):
argument = ip_history[int(argument)]
# Guardar la dirección IP actual solo si no es 'auto'
message = save_current_ip(ip_history)
print(message)
if argument == "auto" or argument == "a":
message = set_dhcp()
elif argument == "last" or argument == "l":
if ip_history:
message = restore_last_ip(ip_history)
print(message)
else:
ip_parts = argument.split(".")
if len(ip_parts) == 3:
argument += ".249"
gateway = ".".join(ip_parts[:3]) + ".1"
message = set_static_ip(argument, gateway)
print(message)
with open(IP_HISTORY_FILE, "w") as file:
json.dump(ip_history, file)
notification_thread = threading.Thread(target=show_notification, args=(message, 3))
notification_thread.start()
if __name__ == "__main__":
main()

View File

@ -1 +0,0 @@
python ipchange.py

View File

@ -1,55 +0,0 @@
import ctypes
import sys
import os
import subprocess
import re
def is_admin():
"""Verifica si el script se está ejecutando con privilegios de administrador."""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run_as_admin(argv=None, debug=False):
"""Ejecuta el script como administrador."""
shell32 = ctypes.windll.shell32
if argv is None and shell32.IsUserAnAdmin():
# Ya estamos ejecutando como administrador, no es necesario hacer nada
return True
if argv is None:
argv = sys.argv
if hasattr(sys, '_MEIPASS'):
# Soporte para PyInstaller
arguments = map(str, argv[1:])
else:
arguments = map(str, argv)
argument_line = u' '.join(arguments)
executable = str(sys.executable)
if debug:
print(f'Command line: {executable} {argument_line}')
ret = shell32.ShellExecuteW(None, "runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
return None
def main():
if not is_admin():
# Si no se está ejecutando como administrador, solicitar elevación
result = run_as_admin()
if result is True:
print("Ejecutando como administrador.")
elif result is False:
print("No se pudo obtener privilegios de administrador.")
return
else:
# El script se reinició con privilegios de administrador
return
# Aquí va el resto del código de tu script...
# Por ejemplo:
# save_current_ip()
# ...
if __name__ == "__main__":
main()

View File

@ -47,7 +47,7 @@ Note: Administrative privileges are required to change network settings.
## Usage
See the [Wiki](WIKI.md) for detailed usage instructions.
See the [Wiki](wiki.md) for detailed usage instructions.
## License

181
wiki.md Normal file
View File

@ -0,0 +1,181 @@
# Network Interface IP Configuration Utility Wiki
## Table of Contents
- [Overview](#overview)
- [Installation](#installation)
- [Main Components](#main-components)
- [IP Setup Tab](#ip-setup-tab)
- [Tools Tab](#tools-tab)
- [Network Scanning](#network-scanning)
- [Administrator Privileges](#administrator-privileges)
- [Configuration Files](#configuration-files)
- [Troubleshooting](#troubleshooting)
- [FAQ](#faq)
## Overview
The Network Interface IP Configuration Utility is a Python-based desktop application designed to simplify network interface management on Windows systems. It provides an intuitive graphical interface for changing IP addresses, enabling/disabling DHCP, and performing network diagnostics.
## Installation
### Prerequisites
- Windows operating system
- Python 3.6 or higher
- Administrative privileges (required for changing network settings)
### Dependencies
Install the required Python packages:
```
pip install -r requirements.txt
```
Optional dependencies (enhanced functionality):
- `pythonping`: For improved ping functionality (`pip install pythonping`)
- `pysnmp`: For SNMP functionality (`pip install pysnmp`)
- `wmi`: For improved network interface detection (`pip install wmi`)
- `mac-vendor-lookup`: For MAC address vendor lookup (`pip install mac-vendor-lookup`)
### Running the Application
Run the main script:
```
python menu-ip-change.py
```
## Main Components
The application is divided into two main tabs:
1. **IP Setup**: For configuring network interfaces
2. **Tools**: For network diagnostics and scanning
Both tabs share a common log area that displays operation results and informational messages.
## IP Setup Tab
### Interface Selection
- Use the dropdown menu to select the network interface you want to configure
- The current configuration (IP, subnet mask, gateway, and DHCP status) will be displayed
### Setting a Static IP
1. Enter the IP prefix (first three octets) in the "IP Prefix" field
2. Enter the last octet in the "Last Octet" field
3. Set the subnet mask (directly or using CIDR notation)
4. Click "Set Static IP"
### Enabling DHCP
1. Select the network interface
2. Click "Enable DHCP"
### Using IP History
- Previously used IP addresses are saved in the IP History dropdown
- Select an entry to quickly configure that IP
- The application remembers subnet masks for each IP prefix
### Quick Actions
- **Restore Previous**: Reverts to the previous configuration
- **Refresh Interfaces**: Updates the list of available network interfaces
- **Copy buttons**: Quick copy of current IP or gateway to clipboard
## Tools Tab
### Ping Tool
- Enter an IP address or hostname in the "Target IP/Host" field
- Click "Ping" to send echo requests
- Enable "Continuous Ping" to keep pinging until stopped
- "Last RTT" shows the most recent ping response time
- Use "Stop" to end continuous pinging
### Network Scanner
- Define an IP range (Start IP, End IP) or use the CIDR field
- Click "Start Scan" to discover active hosts in the specified range
- The scan progress bar shows completion status
- Results appear in the table with IP addresses and ping times
- Click "Get Host Info" to retrieve hostnames and MAC addresses
- MAC vendors are automatically identified (when available)
## Network Scanning
### Scan Process
1. Define the IP range to scan
2. Start the scan to discover active hosts
3. Use "Get Host Info" to gather detailed information about discovered hosts
### Scan Results
The scan results display:
- IP Address
- Ping time (ms)
- Hostname (when resolved)
- MAC Address (when available)
- MAC Vendor (manufacturer)
## Administrator Privileges
Administrator privileges are required to change network settings on Windows. When you attempt to change network settings, the application will:
1. Launch a separate script with administrator privileges
2. Execute the appropriate network commands
3. Log the results
### How It Works
The main application (`menu-ip-change.py`) uses a helper script (`ip-changer-admin.py`) to execute network configuration commands with elevated privileges. This helper script is launched using the Windows `ShellExecuteW` API with the "runas" verb.
## Configuration Files
The application stores configuration in several JSON files:
### ip_config.json
Stores:
- Last used interface
- Last used IP prefix
- Previous configuration (for restore function)
- Last used subnet mask
### ip_history.json
Stores a history of previously used IP addresses and their subnet masks.
### ping_targets.json
Stores preferred ping targets for each IP prefix.
## Troubleshooting
### Common Issues
#### Unable to Change Network Settings
- Ensure you have administrator privileges
- Check the operation log for specific error messages
- Verify the interface name is correct
#### Network Scan Not Working
- Check firewall settings (may block ping/ICMP)
- Ensure the IP range is correctly specified
- Try scanning a smaller range
#### MAC Address Not Found
- Some devices may not respond to ARP
- Try using "Get Host Info" to retry MAC detection
- Some virtualized interfaces won't return MAC addresses
### Log Messages
The application maintains a detailed operation log that can help diagnose issues. Common message types:
- INFO: Informational messages
- ERROR: Error conditions
- SUCCESS: Successful operations
- DISCOVERY: Host discovery during scanning
- PING: Ping results
- MAC/HOSTNAME/VENDOR: Device information
## FAQ
### Q: Does this work on non-Windows systems?
A: No, this application is designed specifically for Windows and uses Windows-specific commands.
### Q: Why does it need administrator privileges?
A: Changing network settings on Windows requires elevated privileges for security reasons.
### Q: Can I scan large IP ranges?
A: Yes, but scanning large ranges (thousands of IPs) may take a significant amount of time.
### Q: How does the continuous ping work?
A: It sends ping requests at regular intervals (approximately once per second) until stopped.
### Q: How does the MAC vendor lookup work?
A: The application uses the mac-vendor-lookup library, which contains a database of MAC address prefixes and their corresponding manufacturers.