Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IP address for users
#1
Hi all -

Using Python 3 .

I have been tasked with logging the IP address, date and time when users attempt to access a database. There are many ways to find MY address, but so far I can't figure out how to capture someone else's IP.

Using this code now -

from datetime import datetime

# Importing socket library
import socket

attempts = 0
 
# Get the current date and time
current_datetime = datetime.now()

# Update the number of attempts
attempts += 1

print(current_datetime)
print(attempts)

# Function to display hostname and IP address
def get_Host_name_IP():


    try:
        host_name = socket.gethostname()
        host_ip = socket.gethostbyname(host_name)
        print("Hostname :  ", host_name)
        print("IP : ", host_ip)
        
    except:
        print("Unable to get Hostname and IP")
 
 
# Driver code
get_Host_name_IP()  # Function call
But I am not sure if this is just capturing my info or if it will capture attempts to login to the database.
...and yes, I realize the datetime and attempts are simply printing at this point.
Larz60+ write Oct-07-2023, 09:11 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I modified your code this time. Please use BBCode tags on future posts.
Reply
#2
crosh> blocks off all information from the destination or any information on the local host address. It uses a ping [ -4]. But this command pings the destination address, only it doesn't show it. But it knows where this ping is at.
Programs are like instructions or rules. Learning it gets us closer to a solution. Desired outcome. Computer talk.
Reply
#3
The easiest, most direct method is to use PHP at the top of the login page:

Quote:<?php
$ipAddress = $_SERVER['REMOTE_ADDR'];
echo "Your IP Address is: {$ipAddress}";
?>

For users coming via VPN there are various strategies to try to get their real ip.

Search for:
Quote:PHP catch user's ip on login

Catching and storing a user's ip and other data may have legal ramifications, beware!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020