pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 28082dfc0 by Anonymous on Fri 10th Feb 2023 11:58
original post 80a634c by Anonymous on Fri 10th Feb 2023 11:05
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)
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 then the input string did not properly match
3333
	# 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
	return 0
4242
}
4343
4444
function parse_nfs_url
4545
{
4646
	typeset url="$2"
4747
	nameref data="$1"
4848
4949
	parse_url data "$url" || return 1
5050
5151
	[[ "${data.protocol}" == 'nfs' ]]	|| { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; }
5252
	[[ "${data.host}" != '' ]]		|| { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; }
5353
	[[ "${data.path}" != '' ]]		|| { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; }
5454
5555
	if [[ ! -v data.port ]] ; then
5656
		integer data.port=2049
5757
	fi
5858
5959
	return 0
6060
}
6161
6262
6363
function main
6464
{
6565
	integer i
6666
	compound c
6767
	compound c.nfs_data
6868
	integer c.forward_port=34049
6969
	integer retval
7070
7171
	#set -o xtrace
7272
	set -o nounset
7373
7474
	typeset c.args=( "$@" )
7575
7676
	for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do
7777
		if [[ "${c.args[i]}" == '-o' ]] ; then
7878
			case "${c.args[i+1]-}" in
7979
				'NFSURL='*)
8080
					unset c.nfs_data
8181
					typeset c.url="${c.args[i+1]#NFSURL=}"
8282
					parse_nfs_url c.nfs_data "${c.url}" || return 1
8383
					unset c.args[$i] c.args[$((i+1))]
8484
					((i++))
8585
					;;
8686
				'NFSJumphost='*)
8787
					[[ ! -v c.ssh_jumphost_args ]] && typeset -a c.ssh_jumphost_args
8888
					c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" )
8989
					unset c.args[$i] c.args[$((i+1))]
9090
					((i++))
9191
					;;
9292
			esac
9393
		fi
9494
	done
9595
9696
	# bug: should be [[ -v c.nfs_data ]], but ksh93u has bugs
9797
	if [[ -v c.nfs_data.host ]] ; then
9898
		# Forward NFS port from server to local machine
9999
		# Notes:
100100
		# - We use $ ssh -M ... # here as a way to terminate the port
101101
		# forwarding process later using "-O exit" without the need
102102
		# for a pid
103103
		printf $"# Please enter the password for NFS server (%s)\n" \
104104
			"root@${c.nfs_data.host}"
105105
		ssh \
106106
			-L "${c.forward_port}:localhost:${c.nfs_data.port}" \
107107
			-M -S "$PWD/ssh-control-socket" \
108108
			-N \
109109
			-f -o ExitOnForwardFailure=yes \
110110
			"${c.ssh_jumphost_args[@]}" \
111111
			"root@${c.nfs_data.host}"
112112
		if (( $? != 0 )) ; then
113113
			printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $?
114114
			return 1
115115
		fi
116116
		#ssh -S "$PWD/ssh-control-socket" -O 'check' "root@${c.nfs_data.host}"
117117
118118
		printf $"# Use this to mount the directory:\n"
119119
		printf $"# $ mkdir /mnt_nfs\n"
120-
		printf $"# mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \
120+
		printf $"# $ mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \
121121
			33049 \
122122
			"${c.nfs_data.path}"
123123
124124
		# add NFS forwarding options to main ssh argument list
125125
		c.args=( "-R" "33049:localhost:${c.forward_port}" "${c.args[@]}" )
126126
	fi
127127
128128
	ssh "${c.args[@]}" ; (( retval=$? ))
129129
130130
	if [[ -v c.nfs_data.host ]] ; then
131131
		ssh -S "$PWD/ssh-control-socket" -O 'exit' "root@${c.nfs_data.host}"
132132
	fi
133133
134134
	wait
135135
136136
	return $retval
137137
}
138138
139139
main "$@"
140140
141141
# EOF.
142142

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