zhaocai / alfred2-ruby-template

Alfred 2 Workflow Ruby Template
http://zhaocai.github.com/alfred2-ruby-template/
144 stars 37 forks source link

Running in the background #7

Open hadifarnoud opened 10 years ago

hadifarnoud commented 10 years ago

I want to write a workflow in Ruby. It was written in shell script. see here: http://www.alfredforum.com/topic/3289-need-help-with-set-socks-proxy-workflow-i-wrote/

I wonder if I can run anything in background? for this specific example, I want to be able to have an ssh connection until user turns SOCKS proxy off.

zhaocai commented 10 years ago

please be more specific.

hadifarnoud commented 10 years ago

I have a bash script like this:

#!/bin/bash
disable_proxy()
{
        networksetup -setsocksfirewallproxystate Wi-Fi off
        networksetup -setsocksfirewallproxystate Ethernet off
        echo "SOCKS proxy disabled."
}
trap disable_proxy INT

networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 8080
networksetup -setsocksfirewallproxy Ethernet 127.0.0.1 8080
networksetup -setsocksfirewallproxystate Wi-Fi on
networksetup -setsocksfirewallproxystate Ethernet on
echo "SOCKS proxy enabled."
echo "Tunneling..."
ssh -ND 8080 user@yourserver.com&

because of ssh and trap template, this script does not work properly in Alfred. Since I know Ruby a bit too, I wonder if I can do this with Ruby?

I want to start a ruby script by an alfred keyword (e.g. "socks on") and terminate the script with another keyword e.g. "socks off". this way I can revert the settings and disconnect SSH tunnel. terminating the ssh tunnel in alfred is very difficult with bash (using 'expect' and all that).

zhaocai commented 10 years ago

terminating the ssh tunnel in alfred is very difficult with bash (using 'expect' and all that).

why expect? because you need to enter password?

hadifarnoud commented 10 years ago

yes. I know I can edit sudoer with NOPASS but I want to keep it simple so others can use this workflow too

zhaocai commented 10 years ago

option 1

check out https://github.com/zhaocai/alfred2-top-workflow . It has a Authenticate.app you can copy to your project and use.

use the sudo.sh to run command. the first time it will ask for password, then it will be memorized in keychain.

#!/usr/bin/env zsh
PASS=$(Authenticate.app/Contents/MacOS/Authenticate -get password)
if [ "$PASS" = "(null)" ] ; then
    Authenticate.app/Contents/MacOS/Authenticate
    PASS=$(Authenticate.app/Contents/MacOS/Authenticate -get password)
fi
echo $PASS | sudo -S "$@"

option 2

use mac built in security command. This is a better solution but I do not have a code snippet right now.