- #!/usr/bin/ksh93
- #
- # sshnfs - remote login client with NFSv4 forwarding
- #
- # Example usage:
- # $ ksh sshnfs.ksh -o NFSURL=nfs://localhost/export/home/rmainz root@10.49.20.207 #
- # $ 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
- #
- # Written by Roland Mainz <roland.mainz@nrubsig.org>
- #
- #
- # parse url
- #
- # returns:
- # data.protocol
- # data.host
- # data.port (optional)
- # data.path
- #
- function parse_url
- {
- typeset url="$2"
- typeset leftover
- nameref data="$1"
- # ~(E) is POSIX extended regular expression matching (instead of
- # shell pattern)
- leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/X}"
- # All parsed data should be captured via eregex in .sh.match - if
- # there is anything left (except the 'X') then the input string did not
- # properly match the eregex
- [[ "$leftover" == 'X' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; }
- data.protocol="${.sh.match[1]}"
- data.host="${.sh.match[2]}"
- # bug: should be [[ -v .sh.match[3] }}, but ksh93u has bugs
- [[ "${.sh.match[3]}" != '' ]] && integer data.port="${.sh.match[3]}"
- data.path="${.sh.match[4]}"
- return 0
- }
- function parse_nfs_url
- {
- typeset url="$2"
- nameref data="$1"
- parse_url data "$url" || return 1
- [[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; }
- [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; }
- [[ "${data.path}" != '' ]] || { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; }
- if [[ ! -v data.port ]] ; then
- # use # default NFSv4 TCP port number (see
- # $ getent services nfs #)
- integer data.port=2049
- fi
- return 0
- }
- function main
- {
- integer i
- integer retval
- compound c=(
- # port on THIS machine
- integer local_forward_port=34049
- # TCP port on destination machine where we forward the
- # NFS port from the server
- integer destination_nfs_port=33049
- # fixme: "/tmp" vs. "${XDG_RUNTIME_DIR:-/tmp}" ?
- typeset ssh_control_socket_name="/tmp/sshnfs_ssh-control-socket_${LOGNAME}_${PPID}_$$"
- )
- #set -o xtrace
- typeset mydebug=false # fixme: should be "bool" for ksh93v
- set -o nounset
- typeset c.args=( "$@" )
- for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do
- if [[ "${c.args[i]}" == '-o' ]] ; then
- case "${c.args[i+1]-}" in
- 'NFSURL='*)
- unset c.nfs_server
- compound c.nfs_server
- typeset c.url="${c.args[i+1]#NFSURL=}"
- parse_nfs_url c.nfs_server "${c.url}" || return 1
- unset c.args[$i] c.args[$((i+1))]
- ((i++))
- ;;
- 'NFSJumphost='*)
- [[ ! -v c.ssh_jumphost_args ]] && typeset -a c.ssh_jumphost_args
- c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" )
- unset c.args[$i] c.args[$((i+1))]
- ((i++))
- ;;
- esac
- fi
- done
- if [[ -v c.nfs_server ]] ; then
- # Forward NFS port from server to local machine
- # Notes:
- # - We use $ ssh -M ... # here as a way to terminate the port
- # forwarding process later using "-O exit" without the need
- # for a pid
- print -u2 -f $"# Please enter the login data for NFS server (%s):\n" \
- "root@${c.nfs_server.host}"
- ssh \
- -L "${c.local_forward_port}:localhost:${c.nfs_server.port}" \
- -M -S "${c.ssh_control_socket_name}" \
- -N \
- -f -o 'ExitOnForwardFailure=yes' \
- "${c.ssh_jumphost_args[@]}" \
- "root@${c.nfs_server.host}"
- if (( $? != 0 )) ; then
- print -u2 -f $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $?
- return 1
- fi
- # debug
- ${mydebug} && ssh -S "${c.ssh_control_socket_name}" -O 'check' "root@${c.nfs_server.host}"
- print -u2 -f $"# Use this to mount the directory:\n"
- print -u2 -f $"# $ mkdir /mnt_nfs\n"
- print -u2 -f $"# $ mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \
- c.destination_nfs_port \
- "${c.nfs_server.path}"
- # add NFS forwarding options to main ssh argument list
- c.args=(
- '-R' "${c.destination_nfs_port}:localhost:${c.local_forward_port}"
- '-o' 'ExitOnForwardFailure=yes'
- "${c.args[@]}"
- )
- fi
- ${mydebug} && print -v c
- print -u2 -f $"# ssh login data for destination machine:\n"
- ssh "${c.args[@]}" ; (( retval=$? ))
- if [[ -v c.nfs_server ]] ; then
- ssh -S "${c.ssh_control_socket_name}" -O 'exit' "root@${c.nfs_server.host}"
- fi
- wait
- return $retval
- }
- main "$@"
- exit $?
- # EOF.
sshnfs - ssh with nfs forwarding
Posted by Anonymous on Wed 15th Feb 2023 12:56
raw | new post
view followups (newest first): sshnfs - ssh with nfs forwarding by Anonymous
modification of post by Anonymous (view diff)
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.