This topic describes how to identify the actual public IP address of a client when its IP address changes dynamically.
Scenario 1
Problem
Your client has a dynamic public IP address. Local IP lookup tools may report an inaccurate IP address. Even if you add this IP address to the whitelist of your RDS instance, connection errors still occur. To connect to the RDS instance, you must find the correct client IP address.
Notes
If the public IP address of your local device changes in a production environment, we recommend using an internal network connection or configuring an appropriate CIDR block in the whitelist. This practice prevents disconnections caused by IP address changes.
Determine client IP
-
Add
0.0.0.0/0to the whitelist of your RDS instance. For more information, see Configure a whitelist.ImportantThe entry
0.0.0.0/0allows access from any IP address. You must remove this entry immediately after you complete the test. -
Use a client to connect to the ApsaraDB RDS for SQL Server instance. For more information, see Connect to an ApsaraDB RDS for SQL Server instance.
-
Run the following command to find the client IP address.
SELECT CONNECTIONPROPERTY('PROTOCOL_TYPE') AS PROTOCOL_TYPE, CONNECTIONPROPERTY('CLIENT_NET_ADDRESS') AS CLIENT_NET_ADDRESSIn the result, the
CLIENT_NET_ADDRESSvalue is the client's actual IP address. -
Remove the
0.0.0.0/0entry from the whitelist, and add the actual egress IP address.
Scenario 2
Problem
Use the following method to find all IP addresses connected to your ApsaraDB RDS for SQL Server instance or to troubleshoot security issues such as connection leaks.
Find all connected IPs
-
Add
0.0.0.0/0to the whitelist of your RDS instance. For more information, see Configure a whitelist.ImportantThe entry
0.0.0.0/0allows access from any IP address. You must remove this entry immediately after you complete the test. -
Use a client to connect to the ApsaraDB RDS for SQL Server instance. For more information, see Connect to an ApsaraDB RDS for SQL Server instance.
-
Run the following command to query all IP addresses connected to the database.
SELECT SP.SPID, SP.LOGINAME, SP.LOGIN_TIME, SP.HOSTNAME, SP.PROGRAM_NAME, DC.CLIENT_TCP_PORT, DC.CLIENT_NET_ADDRESS FROM SYS.SYSPROCESSES AS SP INNER JOIN SYS.DM_EXEC_CONNECTIONS AS DC ON SP.SPID = DC.SESSION_ID WHERE SP.SPID > 50 AND DC.AUTH_SCHEME='SQL' -
Remove the
0.0.0.0/0entry from the whitelist.
View detailed connection parameters
After querying all connected IP addresses, run the following command to view detailed parameters for a specific connection:
SELECT * FROM SYS.DM_EXEC_SESSIONS WHERE SESSION_ID=<the previously obtained SPID>
A successful query returns a result set with the detailed parameters for the session. Key columns include session_id, login_time, host_name, program_name, host_process_id, client_version, client_interface_name, security_id, login_name, nt_domain, nt_user_name, and status.