Created
November 20, 2023 03:44
-
-
Save sfxworks/8b094748de83c59a7c0b286b8381e9c8 to your computer and use it in GitHub Desktop.
APC AP7750 Switcher
This file contains hidden or 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
import telnetlib | |
import time | |
import sys | |
# Configuration | |
host = "192.168.2.10" # APC 7750's IP address | |
port = 23 # Standard Telnet port | |
username = "apc" # Your username | |
password = "apc" # Your password | |
power_choice = sys.argv[1] if len(sys.argv) > 1 else "1" # Default to "1" if no argument is provided | |
#power_choice = data.get("power_source") | |
# Sequence of commands | |
commands = ["1", "1", "2", "5", power_choice, "7"] | |
# Telnet Connection | |
try: | |
print(f"Connecting to {host} on port {port}...") | |
tn = telnetlib.Telnet(host, port) | |
print("Connected successfully.") | |
# Login | |
print("Logging in...") | |
output = tn.read_until(b"User Name : ") | |
print("Received:", output.decode('ascii')) # Display output | |
tn.write(username.encode('ascii') + b"\r\n") | |
output = tn.read_until(b"Password : ") | |
print("Received:", output.decode('ascii')) # Display output | |
tn.write(password.encode('ascii') + b"\r\n") | |
# Additional output display after login | |
output = tn.read_some() | |
print("Post-login output:", output.decode('ascii')) | |
print("Login attempt complete.") | |
# Wait for login process to complete | |
time.sleep(2) # Adjust as necessary | |
# Sending commands | |
for cmd in commands: | |
print(f"Sending command: {cmd}") | |
tn.write(cmd.encode('ascii') + b"\r\n") | |
time.sleep(1) # Adjust as necessary | |
# Display output after each command | |
output = tn.read_some() | |
print("Received:", output.decode('ascii')) | |
print("All commands sent.") | |
# Close the connection | |
tn.close() | |
print("Connection closed.") | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
print("Script execution completed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment