0

so, i am maintaining large mysql servers and using below python code to check whether the server is using my default username and password. What i want to achieve is to make a list of my server ip and check if it is connecting using my set default username and password then print only the ip addresses that is using my default username and password.

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword"
)

print(mydb)

Is there any simple fix for what i want to achieve. Thanks in advance.

1
  • 1
    do you mean ssh into the server and then try to connect to localhost? Commented Nov 28, 2020 at 21:24

1 Answer 1

1

You can do so with try except:

import mysql.connector

my_hosts = ["mysql-host1", "mysql-host2"] # List all your hosts names/IPs here

hosts_with_default_password = []
for host in my_hosts:
    try:
        mydb = mysql.connector.connect(
        host=host,
        user="yourusername",
        password="yourpassword")
        hosts_with_default_password.append(host)
    except Exception as e:
        print('Failed to connect to host {}'.format(host))

print(hosts_with_default_password)
Sign up to request clarification or add additional context in comments.

2 Comments

For this code to work, you'd need to add "hosts_with_default_password.append(host)" in the try block.
Well darn. my apologies. late at night here. Edited that in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.