Skip to content

Instantly share code, notes, and snippets.

@darkcolonist
Last active August 28, 2024 06:01
Show Gist options
  • Save darkcolonist/020d11091af057f0c2a8489c6ec14666 to your computer and use it in GitHub Desktop.
Save darkcolonist/020d11091af057f0c2a8489c6ec14666 to your computer and use it in GitHub Desktop.
get your current IP as well as total time
@echo off
setlocal enabledelayedexpansion
:: Define the API to get the current public IP address
set "api_url=https://api.ipify.org"
:: Define the sleep duration in seconds
set "sleep_duration=1"
:: Get the current public IP address
for /f "delims=" %%i in ('curl -s %api_url%') do set "currentIP=%%i"
:: Set the IP address to monitor
set "ip1=%currentIP%"
:: Initialize variables
set "rto_count=0"
:ping_loop
:: Get the current timestamp
for /f "tokens=2 delims==" %%t in ('wmic os get localdatetime /value') do set "timestamp=%%t"
set "timestamp=%timestamp:~0,4%-%timestamp:~4,2%-%timestamp:~6,2% %timestamp:~8,2%:%timestamp:~10,2%:%timestamp:~12,2%"
:: Ping the current IP address
ping -n 1 %ip1% | findstr "TTL=" >nul
if %errorlevel%==0 (
:: Extract the ping time
for /f "tokens=6 delims== " %%a in ('ping -n 1 %ip1% ^| findstr "TTL="') do (
set "ping_time=%%a"
set "rto_count=0"
echo %timestamp% %ip1% time=!ping_time!
)
) else (
set /a rto_count+=1
echo %timestamp% rto
if !rto_count! geq 3 (
:: Update the IP address after 3 consecutive RTOs
for /f "delims=" %%i in ('curl -s %api_url%') do set "currentIP=%%i"
if "%currentIP%" neq "%ip1%" (
set "ip1=%currentIP%"
echo %timestamp% IP address changed to %ip1%
set "rto_count=0"
)
)
:: Ping the new IP address if it was updated
ping -n 1 %ip1% | findstr "TTL=" >nul
if %errorlevel%==0 (
for /f "tokens=6 delims== " %%a in ('ping -n 1 %ip1% ^| findstr "TTL="') do (
set "ping_time=%%a"
echo %timestamp% %ip1% time=!ping_time!
)
) else (
echo %timestamp% rto
)
)
:: Sleep for the defined duration
timeout /t %sleep_duration% /nobreak >nul
:: Loop the script indefinitely
goto ping_loop
#!/usr/bin/python3
"""
@author: Tyrel F
"""
import requests
import socket
def get_public_ip():
try:
response = requests.get('https://api.ipify.org?format=json')
response.raise_for_status()
ip = response.json()['ip']
return ip
except requests.RequestException as e:
print(f"Error obtaining public IP: {e}")
return None
def get_private_ip():
try:
hostname = socket.gethostname()
private_ip = socket.gethostbyname(hostname)
return private_ip
except socket.error as e:
print(f"Error obtaining private IP: {e}")
return None
if __name__ == "__main__":
public_ip = get_public_ip()
private_ip = get_private_ip()
if public_ip:
print(f"Public IP: {public_ip}")
else:
print("Could not determine public IP")
if private_ip:
print(f"Private IP: {private_ip}")
else:
print("Could not determine private IP")
@darkcolonist
Copy link
Author

sample output

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment