-
Notifications
You must be signed in to change notification settings - Fork 1
/
executable_ssh.sh
165 lines (139 loc) · 5.69 KB
/
executable_ssh.sh
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
# Wrapper to launch SSH in a new tab with correct variables for iTerm.app
# Written by Adam Shand <adam@shand.net> on 27 April 2004
# 20110305 added -4 -C -c blowfish-cbc by recommendation to speed up things
# 20110306 added exec (doh, obvious!) and autossh logic
# 20190404 open ssh in new iTerm tab
# 20200423 support for new tabs in gnome-terminal and guake
# 20200512 ssh to multiple hosts at once
# 20200513 add support for an ssh jumphost
# 20230409 add support for opening in new WezTerm tab
## USAGE
# add alias to bash/zsh start up scripts, eg: alias s='~/bin/ssh.sh'
# or call manually as ssh.sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/homebrew/bin
#SSH_OPTIONS="-A -4C -e ^"
SSH_OPTIONS="-A"
unset DEBUG
#DEBUG="true"; #set -x
usage() {
echo "ssh to one (or more) hosts in a new terminal tab"
echo "usage: ssh.sh [-djv] [-J jumphost] <hostname> [hostname ...]"
echo " -d; disables opening in new tab"
echo " -h; usage"
echo " -j; uses $SSH_JUMPHOST"
echo " -J <jumphost>"
echo " -v; puts ssh in verbose mode"
exit
}
if [ -z "$1" ]; then
usage
fi
if [ "${OSTYPE:0:6}" == "darwin" ]; then
SSH_OPTIONS="$SSH_OPTIONS -o UseKeychain=yes"
# supports both iTerm.app and Apple_Terminal
APP="${TERM_PROGRAM}"
elif [ "${OSTYPE:0:5}" == "linux" ]; then
SSH_OPTIONS="$SSH_OPTIONS -Y"
# export DISPLAY=":0"
if [ -n "${GIO_LAUNCHED_DESKTOP_FILE}" ]; then
APP="Guake"
elif [ -n "$GNOME_TERMINAL_SCREEN" ]; then
APP="Gnome"
elif [ "$TERM_PROGRAM" == "WezTerm" ]; then
APP="WezTerm"
else
APP="Unknown"
fi
fi
test $DEBUG && echo -e "# APP: $APP"
while getopts dhjJv: option; do
case "${option}" in
d) APP="Disabled"
shift
;;
j) JUMPHOST="-J ${SSH_JUMPHOST:=kauri.local}" # unless already defined set to kauri.local
shift
;;
J) JUMPHOST="-J ${OPTARG}"
shift 2
;;
v) SSH_OPTIONS="${SSH_OPTIONS} -v"
APP="Disabled" # do not open in new tab
shift
;;
h) usage
;;
*) usage
;;
esac
done
SSH_OPTIONS="$SSH_OPTIONS $JUMPHOST"
SSH_HOSTS="$@" # can't do this higher because need to shift off args
echo "# ssh [${SSH_OPTIONS}] [${SSH_HOSTS}] [${APP}]"
for ssh_host in $SSH_HOSTS; do
test $DEBUG && echo -e "# APP: $APP"
if echo $ssh_host | grep ":" > /dev/null; then
SSH_HOST=${ssh_host%:*}
SSH_DIR=${ssh_host#*:}
else
SSH_HOST=${ssh_host}
SSH_DIR="~"
fi
test $DEBUG && echo "# SSH_HOST: $SSH_HOST SSH_DIR: $SSH_DIR"
if [ "$APP" == "WezTerm" ]; then
#SSH_OPTIONS="$SSH_OPTIONS -o RequestTTY=yes -o RemoteCommand=\"cd $SSH_DIR; $SHELL -li\""
#test $DEBUG && echo "# CMD: wezterm cli spawn ssh $SSH_OPTIONS -o RequestTTY=yes -o RemoteCommand=\"cd $SSH_DIR; $SHELL -li\" $SSH_HOST"
# TODO: quoting breaks when using above SSH_OPTIONS, works as below
# this works for any host but new panes don't open on ssh host.
wezterm cli spawn ssh $SSH_OPTIONS -o RequestTTY=yes -o RemoteCommand="cd $SSH_DIR; $SHELL -li" $SSH_HOST
# this only works for hosts in ~/.ssh/config but new panes open on ssh host.
# also no way to pass directories (?)
# wezterm cli spawn --domain-name SSH:mahoe
elif [ "$APP" == "iTerm.app" ]; then
SSH_OPTIONS="$SSH_OPTIONS -o RequestTTY=yes -o RemoteCommand=\\\"cd $SSH_DIR; $SHELL -li\\\""
osascript -e 'tell application "iTerm2"' \
-e "tell current window" \
-e "create tab with default profile command \"ssh $SSH_OPTIONS $SSH_HOST\"" \
-e "end tell" \
-e "end tell"
elif [ "$APP" == "Apple_Terminal" ]; then
SSH_OPTIONS="$SSH_OPTIONS -o RequestTTY=yes -o RemoteCommand=\\\"cd $SSH_DIR; $SHELL -li\\\""
osascript -e 'tell application "Terminal" to activate' \
-e 'tell application "System Events" to keystroke "t" using {command down}' \
-e "tell application \"Terminal\" to do script \"exec ssh $SSH_OPTIONS $SSH_HOST\" in front window" \
elif [ "$APP" == "Guake" ]; then
# TODO: untested
SSH_OPTIONS="$SSH_OPTIONS -o RequestTTY=yes -o RemoteCommand=\"cd $SSH_DIR; $SHELL -li\""
guake -n foo -e "\ssh $SSH_OPTIONS $SSH_HOST; exit" -r "${USER}@${SSH_HOST}"
elif [ "$APP" == "Gnome" ]; then
# TODO: untested
SSH_OPTIONS="$SSH_OPTIONS -o RequestTTY=yes -o RemoteCommand=\"cd $SSH_DIR; $SHELL -li\""
gnome-terminal --tab-with-profile=SSH -- \ssh $SSH_OPTIONS $SSH_HOST
#gnome-terminal --tab -- \ssh $SSH_OPTIONS $SSH_HOST
else
test $DEBUG && echo -e "# No Terminal detected: only connecting $SSH_HOST"
## FIXME: doesn't support multiple ssh hosts, not sure there's a way to make that work.
exec \ssh -o RequestTTY=yes -o RemoteCommand="cd $SSH_DIR; $SHELL -li" "$SSH_HOST"
fi
done
# changed to using osascript -e because I don't like having the EOF break the indents
# osascript -i << EOF
# tell application "iTerm2"
# tell current window
# # what profile is set to doesn't matter as iTerm Automatic Profile Switching will override it
# create tab with default profile command "ssh $SSH_OPTIONS $SSH_HOST"
# end tell
# end tell
#EOF
## Don't need to do this because iTerm Automatic Profile Switching can do it
#RESTORE_PROFILE=${ITERM_PROFILE}
# echo -e "\033]50;SetProfile=ssh_profile\a" # moved to _bash_profile because it has to run after sshing
# don't need to reset because happens in new tab now
# echo -e "\033]50;SetProfile=${RESTORE_PROFILE}\a"
# echo -e "\033];${RESTORE_PROFILE}\007"
## not using autossh anymore but should i want to again ...
# if [ "${1//.*/}" == "maxx" -a -x "$(which autossh)" -a ! -e $SOCKET ]; then
# # ! -e $SOCKET helps make sure that autossh only runs once
# SOCKET="/Users/adam/.ssh/sockets/adam@maxx.shmoo.com:22"
# exec autossh -M 2000 $SSH_OPTIONS $*