Link to home
Start Free TrialLog in
Avatar of CEHJ
CEHJFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Need RAS .scp file

that redials my connection until a particular subnet of my ISP has been avoided (chameleon.dialup.pol.co.uk)
Avatar of kidnme
kidnme

Sure this could be be done with with a batch script. I wrote one that creates a RAS connection, dials it, then views and modifies the route table removing the RAS connection as the default gateway. This was done to effectively block remote users from using our RAS servers to access the internet.
On exit the file would then disconnect and then delete the RAS connection to hide the setting from the user. Anyway...

This could be adapted easily to fit your needs as follows.

Start Loop
Dial connection
Parse Route table for subnet
Check if subnet is desired subnet
If not Loop

I will include the original scripts here so if you are able to you can modify to suit your needs. If not make another post and I will script it for you. :)

The Kid.

This is the first file. Run this once to create the connection and store the settings in the file RAS.

:: DO THIS ONCE to create the RAS Connection

:: BACKUP CURRENT PHONE BOOK
ren "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk" rasphone.bak

:: CREATE RAS CONNECTION AND DIAL THEN HANGUP
RASPHONE -a "RAS Connection"
RASPHONE -D "RAS Connection"
RASPHONE -H "RAS Connection"

:: RESTORE ORIGINAL PHONE BOOK FILE
rem COPY "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk" RAS
DEL "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk"
ren "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.bak" rasphone.pbk

:: END DO THIS ONCE
:: ---------------------------------------------------------------------------

This is the second file. Use this to dial the connection, modify the route table, then delete the connection.

@echo off

:: ADD RAS ENTRY TO PHONE BOOK

type RAS >> "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk"

::DIAL RAS SERVER

rasphone -D "RAS Connection"

:: restore_default_gateway.cmd

:: --------------------------------------------------------
:: set variable defaultgw to gateway address for original connection
:: --------------------------------------------------------

for /f "usebackq skip=10 tokens=3" %%i in (`route print`) do (
 if not defined defaultgw set defaultgw=%%i
 )

::---------------------------------------------------------
:: set variable rasgw to gateway address for RAS connection
::---------------------------------------------------------

:get_rasgw
for /f "usebackq skip=9 tokens=3" %%i in (`route print`) do (
 if not defined rasgw set rasgw=%%i
 )

::---------------------------------------------------------
:: set variable fstoct subnet to first octet of RAS connection
::---------------------------------------------------------

:get_fstoct
for /f "usebackq skip=9 tokens=9 delims=. " %%i in (`route print`) do (
 if not defined fstoct set fstoct=%%i
 )

::---------------------------------------------------------
:: now to alter my route table
::---------------------------------------------------------

:: delete default gateways
route delete 0.0.0.0

:: restore my original gate as default
route add 0.0.0.0 mask 0.0.0.0 %defaultgw%

:: add ras subnets
route add %fstoct%.0.0.0 mask 255.0.0.0 %rasgw%

:: END restore_default_gateway.cmd

:: START my app
pause

:: DISCONNECT RAS CONNECTION
rasphone -h "RAS Connection"

:: DELETE RAS CONNECTION
rasphone -r "RAS Connection"


Avatar of CEHJ

ASKER

Oh dear, i hope i haven't come out with a red herring with this RAS business - maybe i should have said Dial Up Networking?
You said "Oh dear, i hope i haven't come out with a red herring with this RAS business - maybe i should have said Dial Up Networking?"

RAS is short for Remote Access Server. Generally, you dial into this server with a modem. It uses Dial up networking and actually is a Dial Up Networking connection. Hope this clears things up.

As I said before, I can modify this script into one file that you can run to do exactly what you are asking. Just send me the info on the subnet you want to avoid.
Avatar of CEHJ

ASKER

Yes, i know RAS and DUN are related, but i thought they may be separated as far as the application level is concerned

>>Just send me the info on the subnet you want to avoid.

Actually i'm not certain it *is* a subnet - it could simply be an ip address/range. The address i need to avoid is

modem-xxxx.chameleon.dialup.pol.co.uk

where 'xxxx' is a number. The reason is that my connection doesn't work properly to that address
Avatar of CEHJ

ASKER

Where did you go!?
No they arent seperate. You can use rasphone from a command line to start DUN.
I need more info.
What OS are you using? You are using a modem?

You say "modem-xxxx.chameleon.dialup.pol.co.uk

where 'xxxx' is a number. The reason is that my connection doesn't work properly to that address"

What address??? Why do you think this? HOw did you come to this conlusion? Where did you get "modem-xxxx.chameleon.dialup.pol.co.uk" from?  Where do you see this?

When you dial up click start then run. Type in cmd then hit enter to go to a command line.

now type in the following:

route print > c:\route.txt

Now open my computer and browse to your C: drive open the file route.txt and copy and paste here.
Avatar of CEHJ

ASKER

>>What OS are you using?

Win2k

You are using a modem?

Yes. 56K

>>Why do you think this?

See below. You can see the address of the gateway includes 'chimpanzee'. That's fine. When it includes 'chameleon', my connection doesn't work properly - i'm unable to access POP/NNTP and i get DNS errors


===========================================================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x2000003 ...00 53 45 00 00 00 ...... WAN (PPP/SLIP) Interface
===========================================================================
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0   217.134.120.87  217.134.120.87       1
        127.0.0.0        255.0.0.0        127.0.0.1       127.0.0.1       1
    195.92.66.188  255.255.255.255   217.134.120.87  217.134.120.87       1
   217.134.120.87  255.255.255.255        127.0.0.1       127.0.0.1       1
  217.134.120.255  255.255.255.255   217.134.120.87  217.134.120.87       1
        224.0.0.0        224.0.0.0   217.134.120.87  217.134.120.87       1
Default Gateway:    217.134.120.87
===========================================================================
Persistent Routes:
  None

C:\>nslookup 217.134.120.87
Server:  resolver1.svr.pol.co.uk
Address:  195.92.195.94

Name:    modem-2135.chimpanzee.dialup.pol.co.uk
Address:  217.134.120.87


C:\>
Avatar of CEHJ

ASKER

And here's a bad guy - and now i can't access my email:

C:\>route print
===========================================================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x6000003 ...00 53 45 00 00 00 ...... WAN (PPP/SLIP) Interface
===========================================================================
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0   217.134.85.241  217.134.85.241       1
        127.0.0.0        255.0.0.0        127.0.0.1       127.0.0.1       1
    195.92.66.186  255.255.255.255   217.134.85.241  217.134.85.241       1
   217.134.85.241  255.255.255.255        127.0.0.1       127.0.0.1       1
   217.134.85.255  255.255.255.255   217.134.85.241  217.134.85.241       1
        224.0.0.0        224.0.0.0   217.134.85.241  217.134.85.241       1
Default Gateway:    217.134.85.241
===========================================================================
Persistent Routes:
  None

C:\>nslookup 217.134.85.241
Server:  resolver2.svr.pol.co.uk
Address:  195.92.195.95

Name:    modem-1521.chameleon.dialup.pol.co.uk
Address:  217.134.85.241


C:\>
Ok try this on for size!  This is a batch file. Right click and choose New > Text File. Name this myfile.cmd. The .CMD part is important. Now, right click on the file you just created and choose Edit. Copy and paste the following into this file and save. Double click to run.

@echo off

:: Look up dial up connection name in phone book and dial

 cd "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk"
 FOR /F "eol=; tokens=1,2,3* delims=,[]" %%i in (rasphone.pbk) do (
   if not defined DUN set DUN=%%i
 )
 rasphone -d "%DUN%"


:: get IP

:: Loop until not "chameleon"
 :LOOP
   :get_IP
   for /f "usebackq skip=9 tokens=3" %%i in (`route print`) do (
     if not defined gw set gw=%%i
   )

   :: lookup IP - check for "chameleon"
   nslookup %gw% |find /i "chameleon" > temp
   FOR /F "eol=; tokens=1,2,3* delims=. " %%i in (temp) do (
     set this=%%k
   )
   if not "%this%" == "chameleon" goto :GOOD_IP
   ipconfig /renew
   goto :LOOP


:: Got a good IP!!!
 :GOOD_IP
 echo .
 echo Found good IP! = %gw%
 echo .
 nslookup %gw%
 echo .
 pause
I think you should be able to renew the IP, if not let me know and I can set it to disconnect and redial.
Avatar of CEHJ

ASKER

Before i let that weapon loose on my machine, i'd like to ask you some questions:

1. how is that first bit going to find the *one only* connection i need to dial?
it will find the first connection defined. Assuming you only have one, then no problem.
Search your computer for rasphone.pbk. Open this file with notepad. You will see the dial up connection defined like this. [my connection].

What this does is open this file, and 'parse' each piece of the file.

cd "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk"
 FOR /F "eol=; tokens=1,2,3* delims=,[]" %%i in (rasphone.pbk) do (
   if not defined DUN set DUN=%%i
 )

This setting 'delims=,[]" ' sets the delimiter to [ and ] meaning it skips these as if this is the begining of a new 'piece' of info.

This line 'if not defined DUN set DUN=%%i' sets the variable DUN to the value inputed if DUN isn't already defined. What this means is that only the first dial up connection will be used.

Then the line 'rasphone -d "%DUN%"' dials the connection named DUN.


Avatar of CEHJ

ASKER

>>it will find the first connection defined. Assuming you only have one, then no problem.

I have more than one. In my case, that loop could be replaced by

set DUN="_New Freeserve"

Almost ready to give this a try ;-)
ASKER CERTIFIED SOLUTION
Avatar of kidnme
kidnme

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ

ASKER

The only thing is that i notice that rasphone is a gui (not wanted) app and i can't find a way of turning off interaction
Yes Raspone is a GUI. I dont think there is a way to dial this quietly. You always initiate your dial up session manually. Do you want me to seperate teh dialing part and the findind a good IP part?
Avatar of CEHJ

ASKER

Using your ideas, i've managed to come up with the following:

@echo off
REM Dial "_New Freeserve"

SET DUN_ENTRY="_New Freeserve"
SET UNAME=xxxxxxxxxxx
SET PASS=yyyyyyyyyyyy

rasdial %DUN_ENTRY% %UNAME% %PASS%


-------------------------------------------------------------------------------------------------------

@echo off

:: Do a lookup on the gateway address

for /F "usebackq tokens=3" %%x in (`route print ^| find  "Default Gateway"`) do (
      nslookup %%x >lookup.txt
)      
:: Get out if the lookup file has not been created
if not exist lookup.txt goto :EOF
:: Else print the name of the machine you're connected to
for /F "skip=3 delims=. tokens=2" %%x in (lookup.txt) do (
      echo You are connected to '%%x'
      goto :delete_and_exit
)      
goto :EOF
:delete_and_exit
if exist lookup.text delete lookup.txt


Now all that needs to be done is to glue them together (oh and cure the fact that the temp file deletion doesn't work [locked?])
Avatar of CEHJ

ASKER

btw, 'delete' was a typo but del doesn't work either
Avatar of CEHJ

ASKER

Thanks to your intro to the tortuous syntax of Windoze scripting, i've managed to produce the following:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off

set BAD_CONNECTION=chameleon

set DUN_ENTRY="_New Freeserve"
set UNAME=xxxxxxxxxxx
set PASS=yyyyyyyyyyyy

:CONNECT
rasphone -h %DUN_ENTRY%
rasdial %DUN_ENTRY% %UNAME% %PASS%


:: Do a lookup on the gateway address

for /F "usebackq tokens=3" %%x in (`route print ^| find  "Default Gateway"`) do (
      nslookup %%x >lookup.txt
)      
:: Get out if the lookup file has not been created
if not exist lookup.txt goto :EOF
:: Else print the name of the machine you're connected to
for /F "skip=3 delims=. tokens=2" %%x in (lookup.txt) do (
      set connection=%%x
      echo Connected to %connection%
      if %connection%==%BAD_CONNECTION% goto CONNECT
      goto :delete_and_exit
)      
goto :EOF
:delete_and_exit
if exist lookup.text del lookup.txt

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Which just produced the following highly satisfying output:

Connecting to _NEW FREESERVE...
Verifying username and password...
Registering your computer on the network...
Successfully connected to _NEW FREESERVE.
Command completed successfully.
Connected to chameleon
Connecting to _NEW FREESERVE...
Verifying username and password...
Registering your computer on the network...
Successfully connected to _NEW FREESERVE.
Command completed successfully.
Connected to chameleon
Connecting to _NEW FREESERVE...
Verifying username and password...
Registering your computer on the network...
Successfully connected to _NEW FREESERVE.
Command completed successfully.
Connected to chameleon
Connecting to _NEW FREESERVE...
Verifying username and password...
Registering your computer on the network...
Successfully connected to _NEW FREESERVE.
Command completed successfully.
Connected to cheetah

Before giving you your points, there's just the little matter of the temp file to address ;-)
Avatar of CEHJ

ASKER

Actually i fixed the temp file thing - there was yet another typo! But, there's another nasty bug occurring over the assignment (i think) in the last loop. I'm now getting:


==chameleon was unexpected at this time.
past your complete file here and I will look at it. BTW, Good job with the script.
Avatar of CEHJ

ASKER

Here we are. I thought the error above might be something to do with the variable in the last for loop not being defined properly until the loop has finished, so i tried a subroutine, but still the same problem. Maybe the subroutine in itself is not right (something to do with returning from it?)


@echo off

set BAD_CONNECTION=chameleon

set DUN_ENTRY="_New Freeserve"
set UNAME=xxxxxxxxx
set PASS=yyyyyyyyyy

:CONNECT
rasphone -h %DUN_ENTRY%
rasdial %DUN_ENTRY% %UNAME% %PASS%


:: Do a lookup on the gateway address

:: The following caused nslookup to fall over, showing its 'usage' output!
::nslookup %%x ^| find "Name:" >lookup.txt

for /F "usebackq tokens=3" %%x in (`route print ^| find  "Default Gateway"`) do (
      nslookup %%x  >lookup.txt
)      
:: Get out if the lookup file has not been created
if not exist lookup.txt goto :EOF
set machine=
:: Else print the name of the machine you're connected to
for /F "skip=3 delims=. tokens=2" %%x in (lookup.txt) do (
      call :CHECK_CONNECTION %%x
      echo Connected to %machine%
      if %machine%==%BAD_CONNECTION% goto CONNECT else goto DELETE_AND_EXIT
)

:CHECK_CONNECTION
      set machine=%1

:DELETE_AND_EXIT
if exist lookup.txt del lookup.txt
Avatar of CEHJ

ASKER

Oh btw, that commented-out attempt to pipe nslookup through find was not in that place when i tried it ;-)
Ok, the problem was how you were initializing your variables, specifically machine.

-----------------------------
rem @echo off

set BAD_CONNECTION=chameleon

set DUN_ENTRY="_New Freeserve"
set UNAME=xxxxxxxxx
set PASS=yyyyyyyyyy

:CONNECT
rasphone -h %DUN_ENTRY%
rasdial %DUN_ENTRY% %UNAME% %PASS%
:: Do a lookup on the gateway address

:: The following caused nslookup to fall over, showing its 'usage' output!
::nslookup %%x ^| find "Name:" >lookup.txt

for /F "usebackq tokens=3" %%x in (`route print ^| find  "Default Gateway"`) do (
     nslookup %%x  >lookup.txt
)    
:: Get out if the lookup file has not been created
if not exist lookup.txt goto :EOF
set machine=""
:: Else print the name of the machine you're connected to
for /F "skip=3 delims=. tokens=2" %%x in (lookup.txt) do (
     set machine=%%x
     echo Connected to %machine%
     if %machine%==%BAD_CONNECTION% goto CONNECT else goto DELETE_AND_EXIT
)


:DELETE_AND_EXIT
if exist lookup.txt del lookup.txt

Avatar of CEHJ

ASKER

Thanks. Shall try this when i'm at my machine later. Any ideas about:

>>:: The following caused nslookup to fall over, showing its 'usage' output!

?
Avatar of CEHJ

ASKER

No, that didn't fix it. I'm now getting

Connecting to _NEW FREESERVE...
Verifying username and password...
Registering your computer on the network...
Successfully connected to _NEW FREESERVE.
Command completed successfully.
Connected to ""
Connected to ""

which also suggests it's not breaking out of the loop
if %machine%==%BAD_CONNECTION% goto CONNECT else goto DELETE_AND_EXIT

This wont 'go' The subroutine must be proceeded with : It should be :DELETE_AND_EXIT

As far as this is concerned...
Connected to ""
Connected to ""

this has to do with how the text is being parsed.

Look at mine. It works.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ

ASKER

That's good dlwyatt, i'd kind of figured something like this. In fact i used a subroutine to get around the problem. Any ideas about the previous problem with

>>
:: The following caused nslookup to fall over, showing its 'usage' output!
::nslookup %%x ^| find "Name:" >lookup.txt
>>

?
nslookup %%x ^| find "Name:" >lookup.txt

The ^ is the problem.

Try this.....

nslookup %%x | find "Name:" >lookup.txt

Avatar of CEHJ

ASKER

Yes, changing it back to what i had originally makes it work now. It wasn't before - which is why i thought i had to escape the pipe. Thanks guys
Avatar of CEHJ

ASKER

Oh - one more thing - do i take it that i can set this as a script in the gui dun configuration?
You said "The only thing is that i notice that rasphone is a gui (not wanted) app and i can't find a way of turning off interaction"

So now you want to change It again? I stated "You always initiate your dial up session manually. Do you want me to seperate teh dialing part and the findind a good IP part?"

I originally wrote this as a script that could be added. Now you have modified it to be a stand alone script. The way it is written, It will work stand-alone.
Avatar of CEHJ

ASKER

You're missing the point. It should run *without* a gui on its own but should be able to be called by Dial Up Networking if necessary. I'm just wondering if that will work under 'Run script'
Avatar of CEHJ

ASKER

Never mind - i'll use it in DUN later if necessary. Thanks