1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

.bat conversion

Discussion in 'Mac Programming' started by drewbuchman, Mar 16, 2012.

  1. macrumors newbie

    #1
    Ok so I have broght my mac to work as it is much easier for me to work on. My company has fail over internet and a bat file to switch between the two service providers. The default in the router is the T1 line (which is painfully slow) so we created this for switching to dsl problem being it is for a pc not a mac. Can someone PLEASE help me convert this to applescript and preferably point me in the right direction of making it clickable instead of having to type it into terminal thanks in advance :apple:



    @echo off
    goto menu
    :menu
    echo.
    echo 1 Route through DSL
    echo 2 Route through T1
    echo 3 Route print
    echo 4 Quit
    echo.

    :choice
    set /P C=[1,2,3,4]?
    if "%C%"=="4" goto quit
    if "%C%"=="3" goto RoutePrint
    if "%C%"=="2" goto RouteT1
    if "%C%"=="1" goto RouteDSL
    goto choice

    :RoutePrint
    echo route print
    route print
    goto menu

    :RouteDSL
    echo route delete 0.0.0.0
    route delete 0.0.0.0
    echo route add 0.0.0.0 mask 0.0.0.0 172.20.1.4
    route add 0.0.0.0 mask 0.0.0.0 172.20.1.4
    goto menu

    :RouteCbeyond
    echo route delete 0.0.0.0
    route delete 0.0.0.0
    echo route add 0.0.0.0 mask 0.0.0.0 172.20.1.254
    route add 0.0.0.0 mask 0.0.0.0 172.20.1.254
    goto menu

    :quit
    exit
     
  2. macrumors 68000

    #2
    All this does is change the default route in your network settings. You could do that by hand. It is the one called 'router'.

    [​IMG]

    This shows you how to access the network preferences with Applescript, it should get you started

    http://www.macosxautomation.com/applescript/features/system-prefs.html
     
  3. macrumors regular

    #3
    Scriptbased interacting with a GUI is hard.

    show wich default route is used:

    Code:
    route get default
    
    change to the one route:

    Code:
    route change default 172.20.1.4
    
    change to the other route:

    Code:
    route change default 172.20.1.254
    You could use '-net 0.0.0.0 0.0.0.0' of '-net 0.0.0.0/0' or even '-net 0/0', but 'default' is just as clear.

    There are some other changes needed, but none major.

    The other point is: is there a willingness to resolve this for the whole company? A cheap router with two WAN-ports can do this automagically for everybody.
     
  4. macrumors newbie

    #4
    Thanks

    Thank you both for your help. but what i was poking for was clickable method... I'm guessing by the replays that this is not and ez task. Ill keep that in mind.... Thanks again
     
  5. macrumors 6502

    blaster_boy

    #5
    Are you root on your machine ?

    I agree with a previous poster above - a simple WAN router with failover could solve this for the whole company without even anyone noticing.

    Anyways, been looking into writing a tiny python script for this and using Platypus (google for "platypus mac" to create a program out of it - but from testing the route command it seems you must be 'root' to do the change.

    And Platypus says it can run with admin privileges, but that this not the same as root. Maybe it'll work, maybe it won't.

    Here's at least an app that will show you where the default gateway is currently pointing to... it'll run in your top menu bar and when clicked on will show you the extremely useful message :) of your ip address and what interface it's on.

    It works on my mac mini (latest Mac Os, Python 2.7 which is installed by default on all macs so should not be a problem). But YMMV.

    Good luck !
     

    Attached Files:

  6. kryten2, Mar 17, 2012
    Last edited by a moderator: Mar 18, 2012

    macrumors 6502a

    #6
    Try this little script

    @burne : Thank you for the commands.

    @drewbuchman : Looking at your .bat file I guess goto RouteT1 must be goto RouteCbeyond or maybe it's a typo. The script should get you going writing your own. As it is your mac i'm also guessing you have an administrator account because the script will ask for your credentials. Please forgive me if I've made any mistakes as I'm not a native English speaker.

    Run this in Scripteditor :

    Code:
    display dialog "Show or Change Default Route" buttons {"RouteT1", "RouteDSL", "RoutePrint"} default button 3
    set the button_pressed to the button returned of the result
    if the button_pressed is "RouteT1" then
    	-- action for 1st button goes here
    	set RouteT1 to do shell script "route change default 172.20.1.254" with administrator privileges
    	display dialog RouteT1
    else if the button_pressed is "RouteDSL" then
    	-- action for 2nd button goes here
    	set RouteDSL to do shell script "route change default 172.20.1.4" with administrator privileges
    	display dialog RouteDSL
    else
    	-- action for 3rd button goes here
    	set RoutePrint to do shell script "route get default | grep gateway"
    	display dialog RoutePrint
    end if
     
  7. macrumors newbie

    #7
    awesome

    kryten2-- thank you so much for that works like a charm

    we have a cisco 2800 router that handles failover the t1 line is so slow but it is the default route. this will let me switch when the t1 has low bandwidth.



    Thanks all for your help
     
  8. macrumors 6502a

    #8
    Nice to hear it worked. Thank you for the feedback. If you don't want to see the dialog box asking for your password you can add it to the script like so :

    set RouteT1 to do shell script "route change default 172.20.1.254" user name "your username" password "mypassword" with administrator privileges

    Only do so if you're the only one with access to your mac as it exposes your credentials to anyone who edits the script.

    Happy scripting.
     

Share This Page