-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh-handler
More file actions
executable file
·90 lines (79 loc) · 2.05 KB
/
Copy pathssh-handler
File metadata and controls
executable file
·90 lines (79 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash -ue
# Christian Bryn <chr.bryn@gmail.com> 2013
# ssh-handler with somewhat limited tmux capabilities
# This script handles ssh hyperlinks (ssh://)
# Passing hosts without the protocol prefix also works
config_file=~/.ssh-handler.conf
## defaults
mode="default"
command="ssh"
# terminal emulators in prioritized order, highest priority first
terminals="urxvt xterm"
function print_usage() {
cat <<EOF
Usage: ${0} [-h|-c <command>|-m <mode>|-t '<terminal> [<terminal>]'] <ssh://my.remote.host>
-h This.
-c Command used to connect to remote host. Defaults to '${command}'
-m Mode. Defaults to '${mode}'
-t Space separates list of terminal emulators. Defaults to '${terminals}'
Available modes:
default, tmux
The following variables can be set in ${config_file}:
mode="${mode}"
command="${command}"
terminals="$terminals"
Examples:
${0} ssh://my.remote.host
${0} my.remote.host
${0} -m tmux ssh://my.remote.host
${0} -c 'remmina -n --protocol=SSH -s'
${0} -t 'gnome-terminal xterm'
EOF
}
function tmux_open() {
# open new tmux window and run supplied command
# checks for a default running session called $USER,
# if not it starts a new one called 'ssh'
tmux_session=$USER
if ( tmux has-session -t "${tmux_session}" )
then
tmux new-window -t "${tmux_session}" "$@"
elif ( tmux has-session -t "ssh" )
then
tmux new-window -t "ssh" "$@"
else
tmux -2 new-session -s ssh -d "$@" \; attach
fi
}
## read config file
if [ -f "${config_file}" ]; then
source <( egrep "^mode=|^command=|^terminals=" "${config_file}" )
fi
while getopts hcmt o
do
case $o in
h)
print_usage ; exit ;;
c)
command="$OPTARG" ;;
m)
mode="$OPTARG" ;;
t)
terminals="$OPTARG" ;;
esac
done
shift $(($OPTIND-1))
[ $# -gt 0 ] || { echo 'No host URI given'; print_usage ; exit; }
dest=${1#ssh://}
case $mode in
tmux)
tmux_open "${command} ${dest}" ;;
default)
for t in $terminals
do
which $t >/dev/null || continue
${t} -e "${command} ${dest} " &
break
done
;;
esac