Solved

Need RAS .scp file

Posted on 2004-09-10
35
358 Views
Last Modified: 2008-01-09
that redials my connection until a particular subnet of my ISP has been avoided (chameleon.dialup.pol.co.uk)
0
Comment
Question by:CEHJ
  • 21
  • 13
35 Comments
 
LVL 1

Expert Comment

by:kidnme
ID: 12037002
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"


0
 
LVL 86

Author Comment

by:CEHJ
ID: 12037580
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?
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12039231
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.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12045595
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
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12059565
Where did you go!?
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12061911
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.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12063458
>>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:\>
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12065457
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:\>
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12072089
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
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12072098
I think you should be able to renew the IP, if not let me know and I can set it to disconnect and redial.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12072981
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?
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12081216
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.


0
 
LVL 86

Author Comment

by:CEHJ
ID: 12082701
>>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 ;-)
0
 
LVL 1

Accepted Solution

by:
kidnme earned 400 total points
ID: 12093665
Ok this will dial connection _New Freeserve. It must be spelled exactly this way.

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


@echo off

set DUN="_New Freeserve"
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
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12094785
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
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12095778
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?
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12096304
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?])
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Author Comment

by:CEHJ
ID: 12096327
btw, 'delete' was a typo but del doesn't work either
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12096925
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 ;-)
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12101103
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.
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12109448
past your complete file here and I will look at it. BTW, Good job with the script.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12110037
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
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12110046
Oh btw, that commented-out attempt to pipe nslookup through find was not in that place when i tried it ;-)
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12123144
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

0
 
LVL 86

Author Comment

by:CEHJ
ID: 12124030
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!

?
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12127262
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
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12130933
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.
0
 
LVL 14

Assisted Solution

by:dlwyatt82
dlwyatt82 earned 100 total points
ID: 12143030
Code Snip:

****************

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
)


***************

This fails because you are expanding %machine% within a compound statement (the for loop). You need to enable delayed environment expansion by running cmd with the "/V:ON" switch, and modify the code:

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
)

***********************

Using the /V:ON switch and changing the %s to !s within the for loop tells the batch processor to evaluate "machine" at execution time, instead of just expanding it to its value of "" once before the for loop even starts.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12143624
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
>>

?
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12149162
nslookup %%x ^| find "Name:" >lookup.txt

The ^ is the problem.

Try this.....

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

0
 
LVL 86

Author Comment

by:CEHJ
ID: 12149496
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
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12149498
Oh - one more thing - do i take it that i can set this as a script in the gui dun configuration?
0
 
LVL 1

Expert Comment

by:kidnme
ID: 12155591
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.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12156362
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'
0
 
LVL 86

Author Comment

by:CEHJ
ID: 12317568
Never mind - i'll use it in DUN later if necessary. Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Use of TCL script on Cisco devices:  - create file and merge it with running configuration to apply configuration changes
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now