pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 94a8efad6a by Anonymous on Wed 15th Feb 2023 12:38
original post 28082dfc0 by Anonymous on Fri 10th Feb 2023 11:58
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 #
77
# $ ksh sshnfs.ksh -o NFSURL=nfs://localhost/export/home/rmainz -o NFSJumphost=derfwpc5131 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)
29-
	leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/x}"
29+
	leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/X}"
3030
3131
	# All parsed data should be captured via eregex in .sh.match - if
32-
	# there is anything left then the input string did not properly match
32+
	# there is anything left (except the 'X') then the input string did not
33-
	# the eregex
33+
	# properly match the eregex
34-
	[[ "$leftover" == 'x' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; }
34+
	[[ "$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]}"
41+
4142
	return 0
4243
}
4344
4445
function parse_nfs_url
4546
{
4647
	typeset url="$2"
4748
	nameref data="$1"
4849
4950
	parse_url data "$url" || return 1
5051
5152
	[[ "${data.protocol}" == 'nfs' ]]	|| { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; }
5253
	[[ "${data.host}" != '' ]]		|| { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; }
5354
	[[ "${data.path}" != '' ]]		|| { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; }
5455
5556
	if [[ ! -v data.port ]] ; then
57+
		# use # default NFSv4 TCP port number (see
5659
		integer data.port=2049
5760
	fi
5861
5962
	return 0
6063
}
6164
6265
6366
function main
6467
{
6568
	integer i
66-
	compound c
67-
	compound c.nfs_data
68-
	integer c.forward_port=34049
6969
	integer retval
70+
	compound c=(
7073
74+
		# TCP port on destination machine where we forward the
7377
78+
		# fixme: "/tmp" vs. "${XDG_RUNTIME_DIR:-/tmp}" ?
7581
82+
	#set -o xtrace
7688
	for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do
7789
		if [[ "${c.args[i]}" == '-o' ]] ; then
7890
			case "${c.args[i+1]-}" in
7991
				'NFSURL='*)
80-
					unset c.nfs_data
92+
					unset c.nfs_server
93+
					compound c.nfs_server
8194
					typeset c.url="${c.args[i+1]#NFSURL=}"
82-
					parse_nfs_url c.nfs_data "${c.url}" || return 1
95+
					parse_nfs_url c.nfs_server "${c.url}" || return 1
8396
					unset c.args[$i] c.args[$((i+1))]
8497
					((i++))
8598
					;;
8699
				'NFSJumphost='*)
87100
					[[ ! -v c.ssh_jumphost_args ]] && typeset -a c.ssh_jumphost_args
88101
					c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" )
89102
					unset c.args[$i] c.args[$((i+1))]
90103
					((i++))
91104
					;;
92105
			esac
93106
		fi
94107
	done
95108
96-
	# bug: should be [[ -v c.nfs_data ]], but ksh93u has bugs
109+
	if [[ -v c.nfs_server ]] ; then
98110
		# Forward NFS port from server to local machine
99111
		# Notes:
100112
		# - We use $ ssh -M ... # here as a way to terminate the port
101113
		# forwarding process later using "-O exit" without the need
102114
		# for a pid
103-
		printf $"# Please enter the password for NFS server (%s)\n" \
115+
		print -u2 -f $"# Please enter the password for NFS server (%s)\n" \
104-
			"root@${c.nfs_data.host}"
116+
			"root@${c.nfs_server.host}"
105117
		ssh \
106-
			-L "${c.forward_port}:localhost:${c.nfs_data.port}" \
118+
			-L "${c.local_forward_port}:localhost:${c.nfs_server.port}" \
107-
			-M -S "$PWD/ssh-control-socket" \
119+
			-M -S "${c.ssh_control_socket_name}" \
108120
			-N \
109-
			-f -o ExitOnForwardFailure=yes \
121+
			-f -o 'ExitOnForwardFailure=yes' \
110122
			"${c.ssh_jumphost_args[@]}" \
111-
			"root@${c.nfs_data.host}"
123+
			"root@${c.nfs_server.host}"
112124
		if (( $? != 0 )) ; then
113-
			printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $?
125+
			print -u2 -f $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $?
114126
			return 1
115127
		fi
117128
118-
		printf $"# Use this to mount the directory:\n"
129+
		# debug
119-
		printf $"# $ mkdir /mnt_nfs\n"
130+
		${mydebug} && ssh -S "${c.ssh_control_socket_name}" -O 'check' "root@${c.nfs_server.host}"
123131
132+
		print -u2 -f $"# Use this to mount the directory:\n"
124138
		# add NFS forwarding options to main ssh argument list
125-
		c.args=( "-R" "33049:localhost:${c.forward_port}" "${c.args[@]}" )
139+
		c.args=(
140+
			'-R' "${c.destination_nfs_port}:localhost:${c.local_forward_port}"
126144
	fi
127145
146+
	${mydebug} && print -v c
129147
130-
	if [[ -v c.nfs_data.host ]] ; then
148+
	ssh "${c.args[@]}" ; (( retval=$? ))
133149
150+
	if [[ -v c.nfs_server ]] ; then
135153
154+
	wait
136156
	return $retval
137157
}
138158
139159
main "$@"
160+
exit $?
140161
141162
# EOF.
142163

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