-
Notifications
You must be signed in to change notification settings - Fork 13
/
on
executable file
·244 lines (225 loc) · 6.06 KB
/
on
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
# on, @ -- Run commands on another host
#
# This is like 'ssh -t <host>', but preserves the current directory.
#
# - If called as '@' or '@foo', it will just preserve cwd exactly, i.e.
# remote commands will act on remote files.
# (For example, 'cd ~/Dropbox; @ ember wget ...')
#
# - If called as 'on', it will try to access the *local* cwd via NFS,
# i.e. remote commands will still act on local files.
# (For example, 'cd ~src/systemd; on buildbox ninja')
#
# Accepts '@foo' symlinks (hostname in argv[0]) like ssh/rsh.
#
# Kind of inspired by the SunOS /tmp story from:
# https://www-uxsup.csx.cam.ac.uk/misc/horror.txt
#
# Apparently the 'on' command did indeed exist:
# http://www.bitsavers.org/pdf/sun/sunos/3.4/800-1295-04A_Commands_Reference_Manual_198609.pdf#page=290
. lib.bash || exit
progname="on"
progname_prefix=1
usage() {
echo "Usage: on <host> [-HKLNRSvxX] [-C <path>] [<command>]"
echo " @ <host> [-HKLNRSvxX] [-C <path>] [<command>]"
echo
echo "Run an interactive shell or command on the specified host, preserving the"
echo "current working directory. When invoked as '@' will use the remote equivalent"
echo "of the local path; if invoked as 'on' will access the local directory via NFS."
echo
echo "(If the current directory is under /net/<otherhost>, -R will map it to reference"
echo "the same location on the target system, while -L and -N will retain it as-is.)"
echo
echo_opt "-C <path>" "access specified remote directory (relative to remote home)"
echo_opt "-L" "access local current directory via NFS (default for 'on')"
echo_opt "-N" "keep current directory exactly (preserve /net/<thirdhost>)"
echo_opt "-R" "always map current directory to target host (default for '@')"
echo
echo_opt "-x" "expand the command to an NFS path"
echo_opt "-X" "expand paths in arguments to NFS paths"
echo
echo_opt "-K" "allow Kerberos ticket delegation, disable SSH multiplexing"
echo_opt "-S" "connect as superuser (root)"
echo_opt "-v" "verbose (display remote host and path)"
echo
echo "The program may also be invoked through symlinks for specific hosts:"
echo
echo_opt "<host>" "like 'on -C. <host>'"
echo_opt "@<host>" "like 'on -R <host>' or '@ <host>'"
}
arg0=${0##*/}
wd=$PWD
nfsmode=KEEP
host=""
user=""
Kflag=""
qflag="-q"
excomm=0
expaths=0
verbose=0
if [[ $1 == --help ]]; then
usage; exit
elif [[ $arg0 == 'on' && $1 == [!-]* ]]; then
host=$1; shift
nfsmode=USE
elif [[ $arg0 == 'on' ]]; then
nfsmode=USE
elif [[ $arg0 == '@'?* ]]; then
host=${arg0#@}
nfsmode=STRIP
elif [[ $arg0 == '@' && $1 == [!-]* ]]; then
host=$1; shift
nfsmode=STRIP
elif [[ $arg0 != @('@'|'on') ]]; then
host=$arg0
wd=""
fi
while getopts ":C:d:HKLNRSvxX" OPT; do
case $OPT in
# -C is equal to -d for consistency with other tools
C) nfsmode=KEEP; wd=$OPTARG;;
d) nfsmode=KEEP; wd=$OPTARG;;
H) nfsmode=KEEP; wd=".";;
K) Kflag="-K -Snone";;
L) nfsmode=USE;; # default 'on' behavior
N) nfsmode=KEEP;;
R) nfsmode=STRIP;; # default '@' behavior
S) user="root@";;
v) verbose=1;;
x) excomm=1;;
X) expaths=1;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
if [[ ! $host ]]; then
host=$1; shift
if [[ ! $host ]]; then
vdie "host not specified"
fi
fi
if ! klist -s; then
warn "Kerberos tickets not available"
qflag=""
fi
# Translate the local working directory
case $nfsmode in
USE)
# 'on <host>' mode (-L)
#
# Always add /net/$CLIENT (unless already on /net/$OTHER)
# in order to stay wherever the client's working directory is.
if [[ $wd == /net/* ]]; then
# NFS mount - strip if it's for target, keep as-is otherwise
shopt -s extglob
wd=${wd%/}/
wd=/${wd#"/net/$host/"}
wd=${wd%/}
wd=/${wd##+(/)}
shopt -u extglob
elif [[ $wd == /* ]]; then
# Local path - convert to NFS mount
wd="/net/$HOSTNAME$wd"
wd=${wd%/}
if [[ ! -e /proc/fs/nfsd/versions ]]; then
die "NFS service not running on $HOSTNAME"
fi
else
# Semantics undecided
lib:crash "-L with relative paths not implemented"
fi
;;
KEEP)
# Partial-'@' mode (-N), also implied by '-C <path>'
#
# Keep the path intact (map local 1:1 to target; /net/$OTHER
# stays as-is). Only strip /net/$TARGET to make the path nicer
# without functional change.
if [[ $wd == /* ]]; then
shopt -s extglob
wd=${wd%/}/
wd=/${wd#"/net/$host/"}
wd=${wd%/}
wd=/${wd##+(/)}
shopt -u extglob
fi
;;
STRIP)
# '@<host>' mode (-R), also '<host>' (-D)
#
# Always strip /net/* (including /net/$OTHER) and access the
# target host.
#
# Relative paths remain as-is and are assumed to already be
# valid on the target. (In particular, "." used by '<host>'
# aliases should continue to refer to the remote homedir.)
if [[ $wd == /* ]]; then
shopt -s extglob
wd=${wd%/}/
wd=/${wd#/net/*/}
wd=${wd%/}
wd=/${wd##+(/)}
shopt -u extglob
fi
;;
*)
lib:crash "bad nfsmode value"
esac
if (( TEST_ON )); then
echo "$wd"; exit
fi
if (( verbose )); then
vmsg "Running in \"$host:${wd:-~}\""
fi
if [[ $wd ]]; then
q_wd=${wd@Q}
else
q_wd=""
fi
# Optionally expand the command and/or any ./relative paths to NFS paths
if (( $# == 0 )); then
q_cmd='$SHELL'
elif (( $# == 1 )) && [[ $1 == *\ * ]]; then
if (( excomm || expaths )); then
vdie "cannot expand paths in a quoted shell command"
fi
q_cmd=$1
else
if (( excomm )); then
arg=$1; shift
arg=$(which "$arg") || exit
if [[ $arg == /* && $arg != /@(afs|n|net)/* ]]; then
arg="/net/$HOSTNAME$arg"
fi
if (( verbose )); then
vmsg "Using \"$arg\" as remote command"
fi
set -- "$arg" "$@"
fi
if (( expaths )); then
args=()
for arg; do
if [[ $arg == ./* ]]; then
arg=$(realpath -s "$arg")
fi
if [[ $arg == /* && $arg != /@(afs|n|net)/* ]]; then
arg="/net/$HOSTNAME$arg"
fi
debug "Expanding argument to \"$arg\""
args+=("$arg")
done
set -- "${args[@]}"
fi
q_cmd=${@@Q}
fi
if [[ -t 0 && -t 1 && -t 2 ]]; then
tflag="-t"
else
tflag=""
fi
ssh $Kflag $qflag $tflag "$user$host" \
"export SILENT=1;
if [ -e /etc/profile ]; then . /etc/profile; fi;
if [ -e ~/.profile ]; then . ~/.profile; fi;
cd $q_wd && ($q_cmd)"