- #!/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://localhost/export/home/rmainz -o NFSJumphost=derfwpc5131 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 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
- integer data.port=2049
- fi
- return 0
- }
- function main
- {
- integer i
- compound c
- compound c.nfs_data
- integer c.forward_port=34049
- integer retval
- #set -o xtrace
- 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_data
- typeset c.url="${c.args[i+1]#NFSURL=}"
- parse_nfs_url c.nfs_data "${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
- # bug: should be [[ -v c.nfs_data ]], but ksh93u has bugs
- if [[ -v c.nfs_data.host ]] ; 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
- printf $"# Please enter the password for NFS server (%s)\n" \
- "root@${c.nfs_data.host}"
- ssh \
- -L "${c.forward_port}:localhost:${c.nfs_data.port}" \
- -M -S "$PWD/ssh-control-socket" \
- -N \
- -f -o ExitOnForwardFailure=yes \
- "${c.ssh_jumphost_args[@]}" \
- "root@${c.nfs_data.host}"
- if (( $? != 0 )) ; then
- printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $?
- return 1
- fi
- #ssh -S "$PWD/ssh-control-socket" -O 'check' "root@${c.nfs_data.host}"
- printf $"# Use this to mount the directory:\n"
- printf $"# $ mkdir /mnt_nfs\n"
- printf $"# mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \
- 33049 \
- "${c.nfs_data.path}"
- # add NFS forwarding options to main ssh argument list
- c.args=( "-R" "33049:localhost:${c.forward_port}" "${c.args[@]}" )
- fi
- ssh "${c.args[@]}" ; (( retval=$? ))
- if [[ -v c.nfs_data.host ]] ; then
- ssh -S "$PWD/ssh-control-socket" -O 'exit' "root@${c.nfs_data.host}"
- fi
- wait
- return $retval
- }
- main "$@"
- # EOF.
sshnfs - ssh with nfs forwarding
Posted by Anonymous on Fri 10th Feb 2023 11:05
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.