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

    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
    echo 1 Route through DSL
    echo 2 Route through T1
    echo 3 Route print
    echo 4 Quit

    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

    echo route print
    route print
    goto menu

    echo route delete
    route delete
    echo route add mask
    route add mask
    goto menu

    echo route delete
    route delete
    echo route add mask
    route add mask
    goto menu

  2. macrumors 68000

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


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

  3. macrumors regular

    Scriptbased interacting with a GUI is hard.

    show wich default route is used:

    route get default
    change to the one route:

    route change default
    change to the other route:

    route change default
    You could use '-net' of '-net' 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


    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


    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

    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 :

    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" 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" with administrator privileges
    	display dialog RouteDSL
    	-- 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


    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

    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" 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