Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 45 lines (38 sloc) 792 Bytes
#!/usr/bin/env ruby
# kill certain app if vpn fails for ubuntu
# e.g. vpn-fuse BankNetwork 'pkill -f banking-client'
NAME = ARGV[0] || raise('give me a name!')
COMMAND = ARGV[1] || raise('give me a command')
$retry = ARGV[2].to_i
def run(cmd)
puts cmd
IO.popen(cmd) do |pipe|
while str = pipe.gets
puts str
end
end
$?.success?
end
def vpn_up?
run("nmcli con status id #{NAME} | grep #{NAME}")
end
def start_vpn
run "nmcli con up id #{NAME}" || raise("could not start vpn") unless vpn_up?
end
start_vpn unless vpn_up?
# Ctrl+c or watcher fails --> disconnect and kill app
at_exit do
run COMMAND
run "nmcli con down id #{NAME}"
end
loop do
if not vpn_up?
if $retry == 0
exit
else
start_vpn
$retry -= 1
end
end
sleep 1
end