Skip to content

Instantly share code, notes, and snippets.

@sfxworks
Created November 20, 2023 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfxworks/8b094748de83c59a7c0b286b8381e9c8 to your computer and use it in GitHub Desktop.
Save sfxworks/8b094748de83c59a7c0b286b8381e9c8 to your computer and use it in GitHub Desktop.
APC AP7750 Switcher
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