Last active
March 22, 2019 09:19
-
-
Save serkanberksoy/3e777c84e228e35987e3 to your computer and use it in GitHub Desktop.
Find MySQL Servers with default port on network and try to connect
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Linq; | |
using System.Net; | |
using System.Net.NetworkInformation; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MySql.Data.MySqlClient; | |
namespace FindMySqlServers | |
{ | |
class Program | |
{ | |
private const int MYSQL_DEFAULT_PORT = 3306; | |
static void Main(string[] args) | |
{ | |
List<IPAddress> result = new List<IPAddress>(); | |
Parallel.ForEach(GetAllAddresses(), (address) => | |
{ | |
if (PingPort(address, MYSQL_DEFAULT_PORT)) | |
{ | |
result.Add(address); | |
} | |
}); | |
LogAddresses(result); | |
TestAddresses(result); | |
Console.ReadKey(); | |
} | |
private static IEnumerable<IPAddress> GetAllAddresses() | |
{ | |
List<IPAddress> result = new List<IPAddress>(); | |
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); | |
foreach (NetworkInterface adapter in adapters) | |
{ | |
IPInterfaceProperties properties = adapter.GetIPProperties(); | |
foreach (IPAddressInformation uniCast in properties.UnicastAddresses) | |
{ | |
// Ignore loop-back addresses & IPv6 | |
if (!IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6) | |
result.Add(uniCast.Address); | |
} | |
} | |
return result; | |
} | |
private static bool PingPort(IPAddress address, int port) | |
{ | |
bool result = false; | |
TcpClient tcpClient = new TcpClient | |
{ | |
SendTimeout = 3000, | |
ReceiveTimeout = 3000 | |
}; | |
try | |
{ | |
tcpClient.Connect(address, MYSQL_DEFAULT_PORT); | |
result = tcpClient.Connected; | |
} | |
catch | |
{ | |
// Console.WriteLine(address + " Port " + MYSQL_DEFAULT_PORT + " is closed"); | |
} | |
return result; | |
} | |
private static void LogAddresses(List<IPAddress> result) | |
{ | |
foreach (IPAddress address in result) | |
{ | |
Console.WriteLine(address); | |
} | |
} | |
private static void TestAddresses(List<IPAddress> result) | |
{ | |
foreach (IPAddress address in result) | |
{ | |
if (checkDB_Conn(address, MYSQL_DEFAULT_PORT, "database", "username", "password")) | |
{ | |
Console.WriteLine(address + " database connected"); | |
} | |
else | |
{ | |
Console.WriteLine(address + " has no database / user"); | |
} | |
} | |
} | |
public static bool checkDB_Conn(IPAddress address, int port, string database, string username, string password) | |
{ | |
var conn_info = "Server=" + address + ";Port=" + port + ";Database=" + database+ ";Uid=" + username + ";Pwd=" + password; | |
bool isConn = false; | |
MySqlConnection conn = null; | |
try | |
{ | |
conn = new MySqlConnection(conn_info); | |
conn.Open(); | |
isConn = true; | |
} | |
catch (ArgumentException a_ex) | |
{ | |
Console.WriteLine("Check the Connection String."); | |
Console.WriteLine(a_ex.Message); | |
Console.WriteLine(a_ex.ToString()); | |
} | |
catch (MySqlException ex) | |
{ | |
string sqlErrorMessage = "Message: " + ex.Message + "\n" + | |
"Source: " + ex.Source + "\n" + | |
"Number: " + ex.Number; | |
Console.WriteLine(sqlErrorMessage); | |
isConn = false; | |
switch (ex.Number) | |
{ | |
//http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html | |
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port) | |
break; | |
case 0: // Access denied (Check DB name,username,password) | |
break; | |
default: | |
break; | |
} | |
} | |
finally | |
{ | |
if (conn.State == ConnectionState.Open) | |
{ | |
conn.Close(); | |
} | |
} | |
return isConn; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment