Difference between
modified post 94a8efad6a by Anonymous on Wed 15th Feb 2023 12:38
original post 28082dfc0 by Anonymous on Fri 10th Feb 2023 11:58
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/usr/bin/ksh93 | |
| 2 | 2 | # | |
| 3 | 3 | # sshnfs - remote login client with NFSv4 forwarding | |
| 4 | 4 | # | |
| 5 | 5 | # Example usage: | |
| 6 | 6 | # $ ksh sshnfs.ksh -o NFSURL=nfs://localhost/export/home/rmainz root@10.49.20.207 # | |
| 7 | 7 | # $ ksh sshnfs.ksh -o NFSURL=nfs://localhost/export/home/rmainz -o NFSJumphost=derfwpc5131 root@10.49.20.207 | |
| 8 | 8 | # | |
| 9 | 9 | # Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 10 | 10 | # | |
| 11 | 11 | ||
| 12 | 12 | # | |
| 13 | 13 | # parse url | |
| 14 | 14 | # | |
| 15 | 15 | # returns: | |
| 16 | 16 | # data.protocol | |
| 17 | 17 | # data.host | |
| 18 | 18 | # data.port (optional) | |
| 19 | 19 | # data.path | |
| 20 | 20 | # | |
| 21 | 21 | function parse_url | |
| 22 | 22 | { | |
| 23 | 23 | typeset url="$2" | |
| 24 | 24 | typeset leftover | |
| 25 | 25 | nameref data="$1" | |
| 26 | 26 | ||
| 27 | 27 | # ~(E) is POSIX extended regular expression matching (instead of | |
| 28 | 28 | # shell pattern) | |
| 29 | - | leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/x}" | |
| 29 | + | leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/X}" | |
| 30 | 30 | ||
| 31 | 31 | # All parsed data should be captured via eregex in .sh.match - if | |
| 32 | - | # there is anything left then the input string did not properly match | |
| 32 | + | # there is anything left (except the 'X') then the input string did not | |
| 33 | - | # the eregex | |
| 33 | + | # properly match the eregex | |
| 34 | - | [[ "$leftover" == 'x' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; } | |
| 34 | + | [[ "$leftover" == 'X' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; } | |
| 35 | 35 | ||
| 36 | 36 | data.protocol="${.sh.match[1]}" | |
| 37 | 37 | data.host="${.sh.match[2]}" | |
| 38 | 38 | # bug: should be [[ -v .sh.match[3] }}, but ksh93u has bugs | |
| 39 | 39 | [[ "${.sh.match[3]}" != '' ]] && integer data.port="${.sh.match[3]}" | |
| 40 | 40 | data.path="${.sh.match[4]}" | |
| 41 | + | ||
| 41 | 42 | return 0 | |
| 42 | 43 | } | |
| 43 | 44 | ||
| 44 | 45 | function parse_nfs_url | |
| 45 | 46 | { | |
| 46 | 47 | typeset url="$2" | |
| 47 | 48 | nameref data="$1" | |
| 48 | 49 | ||
| 49 | 50 | parse_url data "$url" || return 1 | |
| 50 | 51 | ||
| 51 | 52 | [[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; } | |
| 52 | 53 | [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; } | |
| 53 | 54 | [[ "${data.path}" != '' ]] || { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; } | |
| 54 | 55 | ||
| 55 | 56 | if [[ ! -v data.port ]] ; then | |
| 57 | + | # use # default NFSv4 TCP port number (see | |
| 56 | 59 | integer data.port=2049 | |
| 57 | 60 | fi | |
| 58 | 61 | ||
| 59 | 62 | return 0 | |
| 60 | 63 | } | |
| 61 | 64 | ||
| 62 | 65 | ||
| 63 | 66 | function main | |
| 64 | 67 | { | |
| 65 | 68 | integer i | |
| 66 | - | compound c | |
| 67 | - | compound c.nfs_data | |
| 68 | - | integer c.forward_port=34049 | |
| 69 | 69 | integer retval | |
| 70 | + | compound c=( | |
| 70 | 73 | ||
| 74 | + | # TCP port on destination machine where we forward the | |
| 73 | 77 | ||
| 78 | + | # fixme: "/tmp" vs. "${XDG_RUNTIME_DIR:-/tmp}" ? | |
| 75 | 81 | ||
| 82 | + | #set -o xtrace | |
| 76 | 88 | for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do | |
| 77 | 89 | if [[ "${c.args[i]}" == '-o' ]] ; then | |
| 78 | 90 | case "${c.args[i+1]-}" in | |
| 79 | 91 | 'NFSURL='*) | |
| 80 | - | unset c.nfs_data | |
| 92 | + | unset c.nfs_server | |
| 93 | + | compound c.nfs_server | |
| 81 | 94 | typeset c.url="${c.args[i+1]#NFSURL=}" | |
| 82 | - | parse_nfs_url c.nfs_data "${c.url}" || return 1 | |
| 95 | + | parse_nfs_url c.nfs_server "${c.url}" || return 1 | |
| 83 | 96 | unset c.args[$i] c.args[$((i+1))] | |
| 84 | 97 | ((i++)) | |
| 85 | 98 | ;; | |
| 86 | 99 | 'NFSJumphost='*) | |
| 87 | 100 | [[ ! -v c.ssh_jumphost_args ]] && typeset -a c.ssh_jumphost_args | |
| 88 | 101 | c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" ) | |
| 89 | 102 | unset c.args[$i] c.args[$((i+1))] | |
| 90 | 103 | ((i++)) | |
| 91 | 104 | ;; | |
| 92 | 105 | esac | |
| 93 | 106 | fi | |
| 94 | 107 | done | |
| 95 | 108 | ||
| 96 | - | # bug: should be [[ -v c.nfs_data ]], but ksh93u has bugs | |
| 109 | + | if [[ -v c.nfs_server ]] ; then | |
| 98 | 110 | # Forward NFS port from server to local machine | |
| 99 | 111 | # Notes: | |
| 100 | 112 | # - We use $ ssh -M ... # here as a way to terminate the port | |
| 101 | 113 | # forwarding process later using "-O exit" without the need | |
| 102 | 114 | # for a pid | |
| 103 | - | printf $"# Please enter the password for NFS server (%s)\n" \ | |
| 115 | + | print -u2 -f $"# Please enter the password for NFS server (%s)\n" \ | |
| 104 | - | "root@${c.nfs_data.host}" | |
| 116 | + | "root@${c.nfs_server.host}" | |
| 105 | 117 | ssh \ | |
| 106 | - | -L "${c.forward_port}:localhost:${c.nfs_data.port}" \ | |
| 118 | + | -L "${c.local_forward_port}:localhost:${c.nfs_server.port}" \ | |
| 107 | - | -M -S "$PWD/ssh-control-socket" \ | |
| 119 | + | -M -S "${c.ssh_control_socket_name}" \ | |
| 108 | 120 | -N \ | |
| 109 | - | -f -o ExitOnForwardFailure=yes \ | |
| 121 | + | -f -o 'ExitOnForwardFailure=yes' \ | |
| 110 | 122 | "${c.ssh_jumphost_args[@]}" \ | |
| 111 | - | "root@${c.nfs_data.host}" | |
| 123 | + | "root@${c.nfs_server.host}" | |
| 112 | 124 | if (( $? != 0 )) ; then | |
| 113 | - | printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $? | |
| 125 | + | print -u2 -f $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $? | |
| 114 | 126 | return 1 | |
| 115 | 127 | fi | |
| 117 | 128 | ||
| 118 | - | printf $"# Use this to mount the directory:\n" | |
| 129 | + | # debug | |
| 119 | - | printf $"# $ mkdir /mnt_nfs\n" | |
| 130 | + | ${mydebug} && ssh -S "${c.ssh_control_socket_name}" -O 'check' "root@${c.nfs_server.host}" | |
| 123 | 131 | ||
| 132 | + | print -u2 -f $"# Use this to mount the directory:\n" | |
| 124 | 138 | # add NFS forwarding options to main ssh argument list | |
| 125 | - | c.args=( "-R" "33049:localhost:${c.forward_port}" "${c.args[@]}" ) | |
| 139 | + | c.args=( | |
| 140 | + | '-R' "${c.destination_nfs_port}:localhost:${c.local_forward_port}" | |
| 126 | 144 | fi | |
| 127 | 145 | ||
| 146 | + | ${mydebug} && print -v c | |
| 129 | 147 | ||
| 130 | - | if [[ -v c.nfs_data.host ]] ; then | |
| 148 | + | ssh "${c.args[@]}" ; (( retval=$? )) | |
| 133 | 149 | ||
| 150 | + | if [[ -v c.nfs_server ]] ; then | |
| 135 | 153 | ||
| 154 | + | wait | |
| 136 | 156 | return $retval | |
| 137 | 157 | } | |
| 138 | 158 | ||
| 139 | 159 | main "$@" | |
| 160 | + | exit $? | |
| 140 | 161 | ||
| 141 | 162 | # EOF. | |
| 142 | 163 |
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.
rovema.kpaste.net RSS