- #!/usr/bin/ksh93
- #
- # sshnfs - remote login client with NFSv4 forwarding
- #
- # Example usage:
- # $ ksh sshnfs.ksh nfs://localhost/export/home/rmainz 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"
- leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/x}"
- [[ "$leftover" == 'x' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; }
- #print -v .sh.match
- 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=4049
- set -o xtrace
- set -o nounset
- url="$1" # fixme: we should use sshnfs -o NFSURL=nfs:// ...
- shift
- typeset c.args=( "$@" )
- parse_nfs_url c.nfs_data "$url" || return 1
- #
- # extract jump host args
- #
- typeset -a c.ssh_jumphost_args
- for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do
- if [[ "${c.args[i]}" == '-o' ]] ; then
- if [[ "${c.args[i+1]}" == ~(Elr)NFSJumphost=.+ ]] ; then
- c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" )
- unset c.args[$i]
- unset c.args[$((i+1))]
- fi
- fi
- done
- # 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}"
- (( $? == 0 )) || { printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $? ; return 1 ; }
- #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" \
- 3049 \
- "${c.nfs_data.path}"
- ssh -R "3049:localhost:${c.forward_port}" "${c.args[@]}" # root@10.49.20.207
- ssh -S "$PWD/ssh-control-socket" -O 'exit' "root@${c.nfs_data.host}"
- wait
- return 0
- }
- main "$@"
- # EOF.
sshnfs - ssh with nfs forwarding
Posted by Anonymous on Wed 8th Feb 2023 14:54
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.