Skip to content

Instantly share code, notes, and snippets.

@phwelo
Created May 13, 2020 16:57
Show Gist options
  • Select an option

  • Save phwelo/818ccbcb001ca084e299e7aa674426e7 to your computer and use it in GitHub Desktop.

Select an option

Save phwelo/818ccbcb001ca084e299e7aa674426e7 to your computer and use it in GitHub Desktop.
Fart
#!/usr/bin/env python3
import socket
import pymysql
username = "A USERNAME"
password = "A REALLY GOOD PASSWORD"
hostname = "SOME HOSTNAME OR IP"
def is_port_open(host, port):
"""
determine whether `host` has the `port` open
"""
s = socket.socket()
try:
print("Testing connection to " + host + "...")
s.settimeout(1)
s.connect((host, port))
except:
print("Unable to connect to " + host)
return False
else:
return True
def vpn_test(ip_address):
return is_port_open(ip_address, 443)
def dbcon(host, user, password, database):
print("Connecting to database...")
con = pymysql.connect(host, user, password, database)
return con
def get_cluster_nodes(dbconnection):
node_list = []
with dbconnection:
cur = dbconnection.cursor()
print("Querying database...")
cur.execute("SELECT * FROM config WHERE profile_id > 1 AND name like 'sacli_ip'")
for row in cur:
node_list.append(row[2])
return node_list
def get_id_from_ip(ip, dbconnection):
with dbconnection:
cur = dbconnection.cursor()
cur.execute("SELECT * FROM config WHERE value LIKE \"" + ip + "\"")
return cur.fetchone()[0]
def remove_profile(pid, dbconnection):
with dbconnection:
cur = dbconnection.cursor()
cur.execute("DELETE FROM config WHERE profile_id LIKE " + str(pid))
def remove_node(host, dbconnection):
print("Removing " + host + ".")
# get profile_id from ip address via sql query
pid = get_id_from_ip(host, dbconnection)
# delete all entries matching that profile_id
remove_profile(pid, dbconnection)
mysql_connection = dbcon(hostname, username, password, 'as_cluster')
node_list = get_cluster_nodes(mysql_connection)
failed_nodes = []
for node in node_list:
test_result = vpn_test(node)
if test_result != True:
failed_nodes.append(node)
if len(node) > 0:
if len(failed_nodes) > 0:
print("Failed Nodes: " + " ".join(failed_nodes))
for host in failed_nodes:
choice = input("Would you like to remove " + host + "? (Y/n) : " )
if str.upper(choice) in "Y":
remove_node(host, mysql_connection)
print("done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment