Identifying which Ethernet cable was connected

2

I have 2 Ethernet cables coming from separate ISPs. I want to create a CMD (.bat) script which would find out which cable was connected and then it would change Ethernet settings accordingly.

I have the script part which changes the settings. How could I identify the cable?

:CHANGE-ISP-1
  netsh interface ipv4 set address name="Ethernet 2" static 108.129.156.199 255.255.255.0 108.129.156.1 store=persistent
  netsh interface ipv4 set dnsservers name="Ethernet 2" source=static address="108.129.159.31" validate=no
  netsh interface ipv4 add dnsservers name="Ethernet 2" address="108.129.159.33" validate=no index=2
:CHANGE-ISP-2
  netsh interface ipv4 set address name="Ethernet 2" source=dhcp
  netsh interface ipv4 set dnsservers name="Ethernet 2" source=dhcp

ZygD

Posted 2020-02-03T14:54:36.847

Reputation: 911

Maybe this Post will help you :) https://stackoverflow.com/questions/22439971/determine-if-ethernet-is-plugged-unplugged-batch-code

– Shayvin – 2020-02-03T15:06:24.133

Thanks, but in my case both connections (interfaces) has the same name "Ethernet 2", because it is essentially the same interface, I just want to change its settings. – ZygD – 2020-02-03T16:44:23.733

Answers

1


1) You have predictable settings to each ISP

2) Don't need to use 2 labels to each ISP settings

3) Replace find /i ... errorlevel to findstr && || (operators)

4) Replace your labels to if...() else ()...


@echo off && setlocal enabledelayedexpansion 

cls & echo/ & for %%i in (1,2
)do "%__APPDIR__%ping.exe" -n 1 dns.ser.ver.%%~i|%__APPDIR__%findstr.exe "TTL=" >nul && (
echo/ DNS ISP-%%~i ping/link status: UP^^!! && set "_isp_srv=%%~i" && goto :_change-isp_:
)|| echo/ DNS ISP-%%i ping/link status: DOWN^^!! & if "%%~i" == "2" endlocal && goto :EOF

:_change-isp_:
set "_cmd_netsh=%__APPDIR__%netsh.exe interface ipv4" && if not "!_isp_srv!" == "1" ( 
!_cmd_netsh! set address name="Ethernet 2" source=dhcp && !_cmd_netsh! set dnsservers name="Ethernet 2" source=dhcp 
) else (!_cmd_netsh! set address name="Ethernet 2" static 108.129.156.199 255.255.255.0 108.129.156.1 store=persistent
!_cmd_netsh! set dnsservers name="Ethernet 2" source=static address="108.129.159.31" validate=no
!_cmd_netsh! add dnsservers name="Ethernet 2" address="108.129.159.33" validate=no index=2 )

rem./ do more tasks here, after that add "endlocal" at the end:  ===^> && endlocal && goto :EOF

For this to work for you with real name/IP, some adjustments to some lines are needed to deal with real names/ip for DNS.SER.VER.1 and DNS.SER.VER.2.


cls & echo/ & for %%i in ( 1,2  //> replace 1,2 to real name or IP 
)do "%__APPDIR__%ping.exe" -n 1 dns.ser.ver.%%~i //> replace for using only the variable in loop  dns.ser.ver.  %%i 
set "_cmd_netsh=%__APPDIR__%netsh.exe interface ipv4" && if not "!_isp_srv!" == "1" ( //> replace "1" to the real name for DNS.SER.VER.1
 )|| echo/ DNS  ISP-%%i //> replace to the var in loop %%i
& if "%%~i" == "2" //> replace to the real name for DNS.SER.VER.2 

  • The same code in "conventional formatting"
@echo off && setlocal enabledelayedexpansion 

cls 
echo/ 

for %%i in (1,2) do (
   "%__APPDIR__%ping.exe" -n 1 dns.ser.ver.%%~i|%__APPDIR__%findstr.exe "TTL=" >nul && (
   echo/ DNS ISP-%%~i ping/link status: UP^^!!
   set "_isp_srv=%%~i"
   goto :_change-isp_:

  ) || (

   echo/ DNS ISP-%%i ping/link status: DOWN^^!!
   if "%%~i" == "2" endlocal && goto :EOF

  ) 
)

:_change-isp_:

set "_cmd_netsh=%__APPDIR__%netsh.exe interface ipv4"

if not "!_isp_srv!" == "1" ( 

     !_cmd_netsh! set address name="Ethernet 2" source=dhcp
     !_cmd_netsh! set dnsservers name="Ethernet 2" source=dhcp 

   ) else (

     !_cmd_netsh! set address name="Ethernet 2" static 108.129.156.199 
     255.255.255.0 108.129.156.1 store=persistent
     !_cmd_netsh! set dnsservers name="Ethernet 2" source=static 
     address="108.129.159.31" validate=no
     !_cmd_netsh! add dnsservers name="Ethernet 2" address="108.129.159.33" validate=no index=2

   )

rem./ do more tasks here, after that add "endlocal" at the end:  ===^> && endlocal && goto :EOF

  • This question suggests that you want to change the actual cable settings from current to another ISP ...

  • So, you can also do this with simpler code, like: yes, maybe it's not that simple.


@echo off && setlocal enabledelayedexpansion && cls & echo/ 

pushd %__APPDIR__% & set "_cmd_netsh=netsh.exe interface ipv4" && ping.exe -n 1 108.129.156.1|findstr "TTL=">nul && (
!_cmd_netsh! set address name="Ethernet 2" source=dhcp && !_cmd_netsh! set dnsservers name="Ethernet 2" source=dhcp
)||( !_cmd_netsh! set address name="Ethernet 2" static 108.129.156.199 255.255.255.0 108.129.156.1 store=persistent
!_cmd_netsh! set dnsservers name="Ethernet 2" source=static address="108.129.159.31" validate=no
!_cmd_netsh! add dnsservers name="Ethernet 2" address="108.129.159.33" validate=no index=2 )

podp & endlocal && goto :EOF

Sorry my limited English

It Wasn't Me

Posted 2020-02-03T14:54:36.847

Reputation: 851

0

Here's some pseudocode to illustrate how to check, brought to you by Crude Hacks 'R Us:

ping DNS.SER.VER.1 | find /I "TTL"    
if not errorlevel 1 echo DNS ISP-1 UP    
if not errorlevel 1 GOTO CHANGE-ISP-1    
ping DNS.SER.VER.2 | find /I "TTL"     
if not errorlevel 1 echo DNS ISP-2 UP   
if not errorlevel 1 GOTO CHANGE-ISP-2  
echo BOTH ISP CONNECTIONS FAIL   

Please substitute the real DNS Server IP addresses for DNS.SER.VER.1 and DNS.SER.VER.2

If the first ping doesn't find the ISP-1 DNS Server, FIND kicks an Errorlevel 1.
If it does find it, Errorlevel is 0 so you get an echo DNS SERVER ISP-1 UP
then the script throws you to CHANGE-ISP-1

If the ping to ISP-1 DNS Server fails, then the script performs ping DNS.SER.VER.2 | find /I "TTL"
If it finds ISP-2, then Errorlevel is 0 and you see DNS SERVER ISP-2 UP
then the script throws you to CHANGE-ISP-2

If at this point you haven't been redirected to either CHANGE-ISP-1 or CHANGE-ISP-2 you see BOTH ISP CONNECTIONS FAIL and can perform whatever warning to the sysop you wish.

K7AAY

Posted 2020-02-03T14:54:36.847

Reputation: 6 962

Thank you for the response. I have tried it. For some reason, in case where my settings are set to come from DHCP (ISP-2), when I plug in ISP-1, both pings fail. – ZygD – 2020-02-04T09:04:10.897