pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post d39479 by Anonymous on Wed 8th Feb 2023 14:54
original post 42df74a0 by Anonymous on Wed 8th Feb 2023 13:33
Download diff
Show old version | new version | both versions

    
1+
#!/usr/bin/ksh93
12
#
23
# sshnfs - remote login client with NFSv4 forwarding
34
#
5+
# Example usage:
57
#
8+
# Written by Roland Mainz <roland.mainz@nrubsig.org>
79
#
10+
814
# returns:
915
# data.protocol
1016
# data.host
1117
# data.port (optional)
1218
# data.path
1319
#
1420
function parse_url
1521
{
1622
	typeset url="$2"
1723
	typeset leftover
1824
	nameref data="$1"
1925
2026
	leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/x}"
2127
2228
	[[ "$leftover" == 'x' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; }
2329
2430
	#print -v .sh.match
2531
2632
	data.protocol="${.sh.match[1]}"
2733
	data.host="${.sh.match[2]}"
2834
	# bug: should be [[ -v .sh.match[3] }}, but ksh93u has bugs
2935
	[[ "${.sh.match[3]}" != '' ]] && integer data.port="${.sh.match[3]}"
3036
	data.path="${.sh.match[4]}"
3137
	return 0
3238
}
3339
3440
function parse_nfs_url
3541
{
3642
	typeset url="$2"
3743
	nameref data="$1"
3844
3945
	parse_url data "$url" || return 1
4046
41-
	[[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; }
47+
	[[ "${data.protocol}" == 'nfs' ]]	|| { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; }
48+
	[[ "${data.host}" != '' ]]		|| { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; }
4250
4351
	if [[ ! -v data.port ]] ; then
4452
		integer data.port=2049
4553
	fi
4654
4755
	return 0
4856
}
4957
5058
5159
function main
5260
{
61+
	integer i
5362
	compound c
5463
	compound c.nfs_data
5564
	integer c.forward_port=4049
56-
set -x
57-
	url="$1"
58-
	shift
59-
	parse_nfs_url c.nfs_data "$url" || return 1
6065
61-
	# $ ssh -f ... # puts the ssh process into the bachground, but
66+
	set -o xtrace
62-
	# we have no way to determinate the process id. The "fix" is
67+
	set -o nounset
7068
71-
	ssh -Y -R "3049:localhost:${c.forward_port}" root@10.49.20.207
69+
	url="$1" # fixme: we should use sshnfs -o NFSURL=nfs:// ...
70+
	shift
7272
73-
	kill -s TERM "${c.forward_pid}"
73+
	parse_nfs_url c.nfs_data "$url" || return 1
7574
76-
	return 0
75+
	#
77-
}
76+
	# extract jump host args
78-
77+
	#
79-
main "$@"
78+
	typeset -a c.ssh_jumphost_args
80-
79+
	for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do
81-
# EOF.
80+
		if [[ "${c.args[i]}" == '-o' ]] ; then
82-
81+
			if [[ "${c.args[i+1]}" == ~(Elr)NFSJumphost=.+ ]] ; then
83-
82+
				c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" )
84-
83+
				unset c.args[$i]
85-
84+
				unset c.args[$((i+1))]
86-
85+
			fi
87-
86+
		fi
88-
87+
	done
8988
90-
89+
	# Forward NFS port from server to local machine
91-
90+
	# Notes:
92-
91+
	# - We use $ ssh -M ... # here as a way to terminate the port
93-
92+
	# forwarding process later using "-O exit" without the need
94-
93+
	# for a pid
95-
94+
	printf $"# Please enter the password for NFS server (%s)\n" \
96-
95+
		"root@${c.nfs_data.host}"
97-
96+
	ssh \
98-
97+
		-L "${c.forward_port}:localhost:${c.nfs_data.port}" \
99-
98+
		-M -S "$PWD/ssh-control-socket" \
100-
99+
		-N \
101-
100+
		-f -o ExitOnForwardFailure=yes \
102-
101+
		"${c.ssh_jumphost_args[@]}" \
103-
102+
		"root@${c.nfs_data.host}"
104-
103+
	(( $? == 0 )) || { printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $? ; return 1 ; }
105-
104+
	#ssh -S "$PWD/ssh-control-socket" -O 'check' "root@${c.nfs_data.host}"
106105
107-
106+
	printf $"# Use this to mount the directory:\n"
108-
107+
	printf $"# $ mkdir /mnt_nfs\n"
109-
108+
	printf $"# mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \
110-
109+
		3049 \
111-
110+
		"${c.nfs_data.path}"
112111
113-
112+
	ssh -R "3049:localhost:${c.forward_port}" "${c.args[@]}" # root@10.49.20.207
114113
115-
114+
	ssh -S "$PWD/ssh-control-socket" -O 'exit' "root@${c.nfs_data.host}"
116-
115+
	wait
117116
118-
117+
	return 0
119-
118+
}
120119
121-
120+
main "$@"
122121
123-
122+
# EOF.
124123

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