-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh-host
executable file
·169 lines (161 loc) · 3.2 KB
/
ssh-host
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
166
167
168
169
#!/usr/bin/env bash
#
# Synopsis:
# ssh into a host derived from symbolic link to ssh-host
# Usage:
# ln -s /usr/local/jmscott/bin/ssh-host bin/skytel
# bin/skytel [-l host_user] [-e <remote exec>]
# Description:
# Login to a host, deriving the host name from the name
# of the script. This script is never invoked directly.
# Instead, a link to this script is made, where the link
# file name is the remote ssh host, and the linked
# script is invoked from the command.
#
# Prior to invoking ssh the remote host name is set as the
# "title" of the terminal by sending the ANSI escape sequence.
# This might fail on non ANSI complient terminals, like sunterm.
# Options:
# -l login_user
# -n no strict host checking
# -p port
# -h remote_host
# -X X11 forwarding
# -e remote exec argv
# -t title of terminal
# Blame:
# https://github.com/jmscott
# See:
# xtitle in repo https://github.com/jmscott/work
# GNU or BSD hostname
# Note:
# Would be nice to reset to previous value of title by somehow querying
# the terminal.
#
# Add a --help option, dear god.
#
PROG=$(basename $0)
HOST=$PROG
SSH_ARGS=''
tty >/dev/null
IS_TTY=$?
LOGIN_USER=$USER
#
# Reset the title to this value after ssh exits.
# Would be nice to query the xterm for the current title value.
#
LOCAL_TITLE="$USER@$(hostname | sed 's/\..*$//')"
log()
{
#
# Log to standard error, in case redirecting standard output
# of remote shell exec
#
echo "$PROG: $@" >&2
}
die()
{
echo "ERROR: $PROG: $@" >&2
exit 1
}
#
# Process the command arguments.
#
# -l login_user
# -n no strict host checking
# -p port
# -h remote_host
# -X X11 forwarding
# -e remote exec
# -t title of terminal
#
while [ -n "$1" ]; do
case "$1" in
-p|--port)
shift
case "$1" in
'')
die "missing port number for option -p"
;;
[1-9]*)
log "connecting on port #$1"
SSH_ARGS="$SSH_ARGS -p $1"
;;
*)
die "funny port number for option -p: $1"
;;
esac
;;
-n|--no-strict)
log "no strict host key check, ignore possible warning"
SSH_ARGS="$SSH_ARGS \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no"
;;
-l|--login)
shift
case "$1" in
'')
die "-l option missing user login name"
;;
*)
LOGIN_USER=$1
log "logging in as user $LOGIN_USER"
SSH_ARGS="$SSH_ARGS -l $LOGIN_USER"
;;
esac
;;
-h|--host)
shift
case "$1" in
'')
die "-h missing host name"
;;
[a-zA-Z0-9]*)
log "remote host is $1"
;;
*)
die "option -h: unexpected host name: $1"
esac
;;
-X|--X11-forward)
log "X11 forwarding enabled"
SSH_ARGS="$SSH_ARGS -X"
;;
#
# Remain arguments are remote ssh execution.
#
-e|--exec)
shift
#
#
#
while [ -n "$1" ]; do
SSH_REMOTE_EXEC="$SSH_REMOTE_EXEC $1"
shift
done
log "remote ssh exec:$SSH_REMOTE_EXEC"
break
;;
-t)
shift
TITLE="$1"
;;
*)
die "unknown option: $1"
;;
esac
shift
done
TITLE=${TITLE:="$LOGIN_USER@$HOST"}
#
# Write directly to the tty in case we are redirecting the
# output of a remote execution.
#
test $IS_TTY = 0 && xtitle "$TITLE" >/dev/tty
ssh $SSH_ARGS $HOST $SSH_REMOTE_EXEC
#
# Reset the terminal title.
# Would be nice to reset to previous value by querying the terminal.
#
test $IS_TTY = 0 && xtitle "$LOCAL_TITLE"