alfred2-ruby-template icon indicating copy to clipboard operation
alfred2-ruby-template copied to clipboard

Running in the background

Open hadifarnoud opened this issue 10 years ago • 5 comments

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.

hadifarnoud avatar Oct 25 '13 12:10 hadifarnoud

please be more specific.

zhaocai avatar Oct 25 '13 17:10 zhaocai

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 [email protected]&

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).

hadifarnoud avatar Oct 30 '13 21:10 hadifarnoud

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

why expect? because you need to enter password?

zhaocai avatar Oct 31 '13 00:10 zhaocai

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

hadifarnoud avatar Oct 31 '13 09:10 hadifarnoud

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.

zhaocai avatar Oct 31 '13 12:10 zhaocai