Skip to content

Instantly share code, notes, and snippets.

@RollingHog
Created March 29, 2022 21:23
Show Gist options
  • Save RollingHog/46d9b2c44f37216997e80061d61b3baf to your computer and use it in GitHub Desktop.
Save RollingHog/46d9b2c44f37216997e80061d61b3baf to your computer and use it in GitHub Desktop.
@echo off
REM run this on ROUTER computer which can't reach TARGET computer placed behind the NAT
set HAMACHI_PATH="C:\Program Files (x86)\LogMeIn Hamachi\x64\"
REM Hamachi target username
set target_username=Example
pushd %HAMACHI_PATH%
for /f "tokens=3" %%I in ('hamachi-2.exe --cli list ^| find /i ^"%target_username:"=%^" ') do set target_VPN_IP=%%I
popd
echo %target_username%: %target_VPN_IP%
if [%target_VPN_IP%] == [] echo Cannot get target_VPN_IP, check target_username and HAMACHI_PATH && pause && exit
set /p target_public_IP="target_public_IP? "
echo Tracing, please be patient
for /f "tokens=2 delims=[]" %%I in ('tracert %target_public_IP% ^| find /i ^".nat^"') do set target_NAT_IP=%%I
REM set target_NAT_IP=188.65.69.72
if [%target_NAT_IP%] == [] echo Cannot find target_NAT_IP, run TRACERT by hand && pause && exit
REM to acquire, run "ipconfig" on TARGET
set target_LAN_IP=192.168.1.100
set target_mask=255.255.252.0
set target_router=192.168.1.1
echo route add %target_LAN_IP% mask 255.255.255.0 %target_VPN_IP%
echo route add %target_router% mask 255.255.255.0 %target_LAN_IP%
echo route add %target_NAT_IP% mask %target_mask% %target_router%
REM TODO unroute
REM example NAT name v203.6702.cv19.at-home.ru
pause
:end
REM set /p target_public_IP="target_public_IP: " && for /f "tokens=2 delims=[]" %i in ('tracert %target_public_IP% ^| find /i ^".nat^"') do echo %i
REM set /p IP="IP: "&& for /f "tokens=2 delims=[]" %i in ("7 v181.nat7.cv19.at-home.ru [188.65.69.72] сообщает: Заданная сеть недоступна.") do echo %i
REM execute this on TARGET computer
REM or just run: netsh interface ip show config "Ethernet"
REM https://stackoverflow.com/questions/4440014/how-to-get-the-ip-address-of-a-particular-adapter-in-windows-xp-through-command/4440183
REM https://stackoverflow.com/questions/22307698/reading-a-specific-line-in-a-text-file-to-a-variable-in-a-batch-file
for /F "skip=3 delims=" %%i in ('netsh interface ip show config ^"Ethernet^"') do set netsh_raw=%%i &goto break_1
:break_1
echo target_LAN_IP=%netsh_raw:~-14,-1%
for /F "skip=5 delims=" %%i in ('netsh interface ip show config ^"Ethernet^"') do set netsh_raw=%%i &goto break_2
:break_2
echo target_router=%netsh_raw:~-12,-1%
for /F "skip=4 delims=" %%i in ('netsh interface ip show config ^"Ethernet^"') do set netsh_raw=%%i &goto break_3
:break_3
echo target_mask=%netsh_raw:~-15,-2%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment