pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post a86b4205 by Anonymous on Wed 15th Feb 2023 12:56
original post 94a8efad6a by Anonymous on Wed 15th Feb 2023 12:38
Download diff
Show old version | new version | both versions

    
11
#!/usr/bin/ksh93
22
#
33
# sshnfs - remote login client with NFSv4 forwarding
44
#
55
# Example usage:
66
# $ ksh sshnfs.ksh -o NFSURL=nfs://localhost/export/home/rmainz root@10.49.20.207 #
7-
# $ ksh sshnfs.ksh -o NFSURL=nfs://localhost/export/home/rmainz -o NFSJumphost=derfwpc5131 root@10.49.20.207
7+
# $ ksh sshnfs.ksh -o NFSURL=nfs://derfwpc5131/export/home/rmainz -o NFSJumphost=rmainz@derfwpc5131,roland.mainz@derfwnb8353 -J rmainz@derfwpc5131,roland.mainz@derfwnb8353 root@10.49.20.207
88
#
99
# Written by Roland Mainz <roland.mainz@nrubsig.org>
1010
#
1111
1212
#
1313
# parse url
1414
#
1515
# returns:
1616
# data.protocol
1717
# data.host
1818
# data.port (optional)
1919
# data.path
2020
#
2121
function parse_url
2222
{
2323
	typeset url="$2"
2424
	typeset leftover
2525
	nameref data="$1"
2626
2727
	# ~(E) is POSIX extended regular expression matching (instead of
2828
	# shell pattern)
2929
	leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/X}"
3030
3131
	# All parsed data should be captured via eregex in .sh.match - if
3232
	# there is anything left (except the 'X') then the input string did not
3333
	# properly match the eregex
3434
	[[ "$leftover" == 'X' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; }
3535
3636
	data.protocol="${.sh.match[1]}"
3737
	data.host="${.sh.match[2]}"
3838
	# bug: should be [[ -v .sh.match[3] }}, but ksh93u has bugs
3939
	[[ "${.sh.match[3]}" != '' ]] && integer data.port="${.sh.match[3]}"
4040
	data.path="${.sh.match[4]}"
4141
4242
	return 0
4343
}
4444
4545
function parse_nfs_url
4646
{
4747
	typeset url="$2"
4848
	nameref data="$1"
4949
5050
	parse_url data "$url" || return 1
5151
5252
	[[ "${data.protocol}" == 'nfs' ]]	|| { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; }
5353
	[[ "${data.host}" != '' ]]		|| { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; }
5454
	[[ "${data.path}" != '' ]]		|| { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; }
5555
5656
	if [[ ! -v data.port ]] ; then
5757
		# use # default NFSv4 TCP port number (see
5858
		# $ getent services nfs #)
5959
		integer data.port=2049
6060
	fi
6161
6262
	return 0
6363
}
6464
6565
6666
function main
6767
{
6868
	integer i
6969
	integer retval
7070
	compound c=(
7171
		# port on THIS machine
7272
		integer local_forward_port=34049
7373
7474
		# TCP port on destination machine where we forward the
7575
		# NFS port from the server
7676
		integer destination_nfs_port=33049
7777
7878
		# fixme: "/tmp" vs. "${XDG_RUNTIME_DIR:-/tmp}" ?
7979
		typeset ssh_control_socket_name="/tmp/sshnfs_ssh-control-socket_${LOGNAME}_${PPID}_$$"
8080
	)
8181
8282
	#set -o xtrace
8383
	typeset mydebug=false # fixme: should be "bool" for ksh93v
8484
	set -o nounset
8585
8686
	typeset c.args=( "$@" )
8787
8888
	for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do
8989
		if [[ "${c.args[i]}" == '-o' ]] ; then
9090
			case "${c.args[i+1]-}" in
9191
				'NFSURL='*)
9292
					unset c.nfs_server
9393
					compound c.nfs_server
9494
					typeset c.url="${c.args[i+1]#NFSURL=}"
9595
					parse_nfs_url c.nfs_server "${c.url}" || return 1
9696
					unset c.args[$i] c.args[$((i+1))]
9797
					((i++))
9898
					;;
9999
				'NFSJumphost='*)
100100
					[[ ! -v c.ssh_jumphost_args ]] && typeset -a c.ssh_jumphost_args
101101
					c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" )
102102
					unset c.args[$i] c.args[$((i+1))]
103103
					((i++))
104104
					;;
105105
			esac
106106
		fi
107107
	done
108108
109109
	if [[ -v c.nfs_server ]] ; then
110110
		# Forward NFS port from server to local machine
111111
		# Notes:
112112
		# - We use $ ssh -M ... # here as a way to terminate the port
113113
		# forwarding process later using "-O exit" without the need
114114
		# for a pid
115-
		print -u2 -f $"# Please enter the password for NFS server (%s)\n" \
115+
		print -u2 -f $"# Please enter the login data for NFS server (%s):\n" \
116116
			"root@${c.nfs_server.host}"
117117
		ssh \
118118
			-L "${c.local_forward_port}:localhost:${c.nfs_server.port}" \
119119
			-M -S "${c.ssh_control_socket_name}" \
120120
			-N \
121121
			-f -o 'ExitOnForwardFailure=yes' \
122122
			"${c.ssh_jumphost_args[@]}" \
123123
			"root@${c.nfs_server.host}"
124124
		if (( $? != 0 )) ; then
125125
			print -u2 -f $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $?
126126
			return 1
127127
		fi
128128
129129
		# debug
130130
		${mydebug} && ssh -S "${c.ssh_control_socket_name}" -O 'check' "root@${c.nfs_server.host}"
131131
132132
		print -u2 -f $"# Use this to mount the directory:\n"
133133
		print -u2 -f $"# $ mkdir /mnt_nfs\n"
134134
		print -u2 -f $"# $ mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \
135135
			c.destination_nfs_port \
136136
			"${c.nfs_server.path}"
137137
138138
		# add NFS forwarding options to main ssh argument list
139139
		c.args=(
140140
			'-R' "${c.destination_nfs_port}:localhost:${c.local_forward_port}"
141141
			'-o' 'ExitOnForwardFailure=yes'
142142
			"${c.args[@]}"
143143
		)
144144
	fi
145145
146146
	${mydebug} && print -v c
147147
148+
	print -u2 -f $"# ssh login data for destination machine:\n"
148149
	ssh "${c.args[@]}" ; (( retval=$? ))
149150
150151
	if [[ -v c.nfs_server ]] ; then
151152
		ssh -S "${c.ssh_control_socket_name}" -O 'exit' "root@${c.nfs_server.host}"
152153
	fi
153154
154155
	wait
155156
156157
	return $retval
157158
}
158159
159160
main "$@"
160161
exit $?
161162
162163
# EOF.
163164

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at