Results 1 to 14 of 14

Thread: Simple script that starts a service at router level

  1. #1


    Member Since
    Mar 20, 2010
    Posts
    19
    Simple script that starts a service at router level
    I need to create a simple script with the following lines (as I would input them in Terminal):

    ssh root@192.168.1.1
    (enter password)
    /etc/init.d/openvpn start

    How do I go about that?

  2. #2

    rman's Avatar
    Member Since
    Dec 24, 2002
    Location
    Los Angeles, California
    Posts
    12,588
    Interesting I was not aware that you could access a router via ssh. Which router are you using?
    Life isn't about waiting for the storm to pass, It's about learning to dance in the rain!

  3. #3


    Member Since
    Mar 20, 2010
    Posts
    19
    Itís a generic ISP-provided router (BT Home Hub 5), which has been reflashed with LEDE/OpenWRT.

  4. #4

    pm-r's Avatar
    Member Since
    Oct 16, 2010
    Location
    Brentwood Bay, BC, Canada
    Posts
    9,054
    Quote Originally Posted by Miller_ View Post
    It’s a generic ISP-provided router (BT Home Hub 5), which has been reflashed with LEDE/OpenWRT.

    Anything useful in this:
    https://www.cisco.com/c/en/us/td/doc.../3routconf.pdf




    - Patrick
    ======

  5. #5

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    10,634
    Your best bet is to use Expect to accomplish this. It is a good scripting language to send commands, wait for things and send additional commands and wait, and so on.

    So you first send the "ssh" command and then expect "password:" or some other recognizable string and then send the password and expect "$" or whatever the prompt looks like, and then you'd send the "openvpn" command and then maybe send a "ps -e | grep openvpn" command and expect to see "openvpn" in the list confirming that it started and then you'd send "exit" to kill the SSH session and close the script.

    If you are familiar with coding, translate my "pseudo" code to the actual code, if not, I'll take a whack at it when I have a moment and send it across.
    --
    Regards
    ...Ashwin


  6. #6

    Slydude's Avatar
    Member Since
    Nov 16, 2009
    Location
    North Louisiana, USA
    Posts
    13,174
    Humor a non coder for asking the abvious question here: Is the oobjective of the code in post #1 to open a vpn at the router level? The goal being to route all traffic through a vpn without having to have separate programs on each device.

  7. #7

    rman's Avatar
    Member Since
    Dec 24, 2002
    Location
    Los Angeles, California
    Posts
    12,588
    Slydude, I was wondering the same thing, since I am able to setup a vpn server via my router.
    Life isn't about waiting for the storm to pass, It's about learning to dance in the rain!

  8. #8


    Member Since
    Mar 20, 2010
    Posts
    19
    Yeah, you start the VPN at router level and all traffic will be directed through the VPN. As opposed to starting a VPN session on the computer, which only affects the machine in question. It comes really useful when you have devices not capable of initiating a VPN connection on their own, like an apple TV for instance.


    Thanks for the input guys, I came up with a very basic code in the meantime:

    tell application "Terminal"
    activate
    do script "ssh root@192.168.1.1"
    delay 3
    tell application "System Events"
    keystroke "myPassword"
    keystroke return
    keystroke "/etc/init.d/openvpn start"
    keystroke return
    delay 1
    keystroke "exit"
    keystroke return
    delay 1
    end tell
    quit application "Terminal"
    end tell


    It actually works very well, but not what you suggested Raz0rEdge - the biggest problem being with the password out in the open. So I use Expect to fix that, but I'm really struggling to understand how it works. How would the password not be 'in the open' with Expect?

  9. #9

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    10,634
    Miller_, that AppleScript would work and Expect would work essentially the same. However, as you've found out, both of them suffer the problem of your password being in clear text. The other thing you can do is setup SSH keys (assuming your router supports that). You can generate DSA or RSA keys and then save the public key on the router and keep the private key locally and then when you SSH, you will not be required for a password at all.

    The assumption here is that the account you are logging into so that you can run the script is where you would authenticate and keep that secure.

    The second option you have is to compile your AppleScript into an application. Choose File->Export and then for File Format, choose Application. This should now give you an compiled application that will "hide" your plain text password (confirm that by looking into the .app folder that is your application and using 'grep' for your password). You can now zip your script up behind a password or something to keep it safe.
    --
    Regards
    ...Ashwin


  10. #10


    Member Since
    Mar 20, 2010
    Posts
    19
    Perfect, went for the easier option and got it saved as a run-only script, and blanked out the password in the original applescript file.

    Strangely, it didn't run well when saved as an application. It ignored my delay commands and the whole process would bog down.

  11. #11

    pm-r's Avatar
    Member Since
    Oct 16, 2010
    Location
    Brentwood Bay, BC, Canada
    Posts
    9,054
    Quote Originally Posted by Miller_ View Post
    Perfect, went for the easier option and got it saved as a run-only script, and blanked out the password in the original applescript file.

    Strangely, it didn't run well when saved as an application. It ignored my delay commands and the whole process would bog down.


    I say congratulations to both programmers here for their combined success. It also really shows the power of what can be done with what's provided. As they say, it's just a matter of knowing how. clap.gif thumbup2.gif

    Yeah right… but how about knowing how to know how????

    Well done I say!!!

    As a good Mac programmer told me one day — if you can think it, it can be done. Yeah right, for some maybe!!!



    - Patrick
    ======

  12. #12

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    10,634
    Quote Originally Posted by Miller_ View Post
    Perfect, went for the easier option and got it saved as a run-only script, and blanked out the password in the original applescript file.

    Strangely, it didn't run well when saved as an application. It ignored my delay commands and the whole process would bog down.
    I find it odd that the delays didn't take effect. I wonder if the compiled app somehow makes the delay seem shorter. You might want to try extending the delays to say 5 seconds and see if that has the desired effect when compiled.

    If not, I will play with some delays as well and let's see what we can come up with.
    --
    Regards
    ...Ashwin


  13. #13


    Member Since
    Mar 20, 2010
    Posts
    19
    Played around with that code a bit and found that if write delay 3 line twice, it works as expected by delaying 3 when compiled as an app. It does delay 6 when run directly from the script. It doesn't make much sense, probably a bug.

    Thank you very much for the props pm-r, although I can assure you I am as much of a programmer as you are. Raz0rEdge is the one with the knowhow I'm just muddling through with bits of code I found on various threads.

  14. #14

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    10,634
    Excellent, glad you could figure it out. That does sound like a bug with delays. Must be some goofiness with the way the script is being compiled with how the delay gets implemented in the application. One of the thing with code compilation is that sometimes compilers get a little too smart and try to optimize delays away to get you efficiency without realizing that you put the delay in there on purpose.
    --
    Regards
    ...Ashwin


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. simple script works in 10.8.5 but doesn't work in 10.10.1
    By luckystone in forum macOS - Development and Darwin
    Replies: 0
    Last Post: 01-19-2015, 12:40 PM
  2. Help with a simple script
    By dgcrane in forum macOS - Development and Darwin
    Replies: 7
    Last Post: 07-11-2013, 10:16 PM
  3. Shortcut or simple script help needed
    By martinielsen in forum macOS - Operating System
    Replies: 6
    Last Post: 07-24-2012, 07:18 PM
  4. Help with simple Automator file copy script
    By PBear in forum macOS - Apps and Games
    Replies: 0
    Last Post: 04-20-2012, 10:09 PM
  5. Simple rsync script advice, please
    By Seventh in forum macOS - Operating System
    Replies: 0
    Last Post: 07-29-2011, 04:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •