Difference between
modified post dad69503 by Anonymous on Wed 15th Feb 2023 13:04
original post a86b4205 by Anonymous on Wed 15th Feb 2023 12:56
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://derfwpc5131/export/home/rmainz -o NFSJumphost=rmainz@derfwpc5131,roland.mainz@derfwnb8353 -J rmainz@derfwpc5131,roland.mainz@derfwnb8353 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 | 29 | leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/X}" | |
| 30 | 30 | ||
| 31 | 31 | # All parsed data should be captured via eregex in .sh.match - if | |
| 32 | 32 | # there is anything left (except the 'X') then the input string did not | |
| 33 | 33 | # properly match the eregex | |
| 34 | 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 | 42 | return 0 | |
| 43 | 43 | } | |
| 44 | 44 | ||
| 45 | 45 | function parse_nfs_url | |
| 46 | 46 | { | |
| 47 | 47 | typeset url="$2" | |
| 48 | 48 | nameref data="$1" | |
| 49 | 49 | ||
| 50 | 50 | parse_url data "$url" || return 1 | |
| 51 | 51 | ||
| 52 | 52 | [[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; } | |
| 53 | 53 | [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; } | |
| 54 | 54 | [[ "${data.path}" != '' ]] || { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; } | |
| 55 | 55 | ||
| 56 | 56 | if [[ ! -v data.port ]] ; then | |
| 57 | 57 | # use # default NFSv4 TCP port number (see | |
| 58 | 58 | # $ getent services nfs #) | |
| 59 | 59 | integer data.port=2049 | |
| 60 | 60 | fi | |
| 61 | 61 | ||
| 62 | 62 | return 0 | |
| 63 | 63 | } | |
| 64 | 64 | ||
| 65 | 65 | ||
| 66 | 66 | function main | |
| 67 | 67 | { | |
| 68 | 68 | integer i | |
| 69 | 69 | integer retval | |
| 70 | 70 | compound c=( | |
| 71 | 71 | # port on THIS machine | |
| 72 | 72 | integer local_forward_port=34049 | |
| 73 | 73 | ||
| 74 | 74 | # TCP port on destination machine where we forward the | |
| 75 | 75 | # NFS port from the server | |
| 76 | 76 | integer destination_nfs_port=33049 | |
| 77 | 77 | ||
| 78 | 78 | # fixme: "/tmp" vs. "${XDG_RUNTIME_DIR:-/tmp}" ? | |
| 79 | 79 | typeset ssh_control_socket_name="/tmp/sshnfs_ssh-control-socket_${LOGNAME}_${PPID}_$$" | |
| 80 | 80 | ) | |
| 81 | 81 | ||
| 82 | 82 | #set -o xtrace | |
| 83 | 83 | typeset mydebug=false # fixme: should be "bool" for ksh93v | |
| 84 | 84 | set -o nounset | |
| 85 | 85 | ||
| 86 | 86 | typeset c.args=( "$@" ) | |
| 87 | 87 | ||
| 88 | 88 | for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do | |
| 89 | 89 | if [[ "${c.args[i]}" == '-o' ]] ; then | |
| 90 | 90 | case "${c.args[i+1]-}" in | |
| 91 | 91 | 'NFSURL='*) | |
| 92 | 92 | unset c.nfs_server | |
| 93 | 93 | compound c.nfs_server | |
| 94 | 94 | typeset c.url="${c.args[i+1]#NFSURL=}" | |
| 95 | 95 | parse_nfs_url c.nfs_server "${c.url}" || return 1 | |
| 96 | 96 | unset c.args[$i] c.args[$((i+1))] | |
| 97 | 97 | ((i++)) | |
| 98 | 98 | ;; | |
| 99 | 99 | 'NFSJumphost='*) | |
| 100 | 100 | [[ ! -v c.ssh_jumphost_args ]] && typeset -a c.ssh_jumphost_args | |
| 101 | 101 | c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" ) | |
| 102 | 102 | unset c.args[$i] c.args[$((i+1))] | |
| 103 | 103 | ((i++)) | |
| 104 | 104 | ;; | |
| 105 | 105 | esac | |
| 106 | 106 | fi | |
| 107 | 107 | done | |
| 108 | 108 | ||
| 109 | 109 | if [[ -v c.nfs_server ]] ; then | |
| 110 | 110 | # Forward NFS port from server to local machine | |
| 111 | 111 | # Notes: | |
| 112 | 112 | # - We use $ ssh -M ... # here as a way to terminate the port | |
| 113 | 113 | # forwarding process later using "-O exit" without the need | |
| 114 | 114 | # for a pid | |
| 115 | 115 | print -u2 -f $"# Please enter the login data for NFS server (%s):\n" \ | |
| 116 | 116 | "root@${c.nfs_server.host}" | |
| 117 | 117 | ssh \ | |
| 118 | 118 | -L "${c.local_forward_port}:localhost:${c.nfs_server.port}" \ | |
| 119 | 119 | -M -S "${c.ssh_control_socket_name}" \ | |
| 120 | 120 | -N \ | |
| 121 | 121 | -f -o 'ExitOnForwardFailure=yes' \ | |
| 122 | 122 | "${c.ssh_jumphost_args[@]}" \ | |
| 123 | 123 | "root@${c.nfs_server.host}" | |
| 124 | 124 | if (( $? != 0 )) ; then | |
| 125 | 125 | print -u2 -f $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $? | |
| 126 | 126 | return 1 | |
| 127 | 127 | fi | |
| 128 | 128 | ||
| 129 | 129 | # debug | |
| 130 | 130 | ${mydebug} && ssh -S "${c.ssh_control_socket_name}" -O 'check' "root@${c.nfs_server.host}" | |
| 131 | 131 | ||
| 132 | 132 | print -u2 -f $"# Use this to mount the directory:\n" | |
| 133 | 133 | print -u2 -f $"# $ mkdir /mnt_nfs\n" | |
| 134 | 134 | print -u2 -f $"# $ mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \ | |
| 135 | 135 | c.destination_nfs_port \ | |
| 136 | 136 | "${c.nfs_server.path}" | |
| 137 | 137 | ||
| 138 | 138 | # add NFS forwarding options to main ssh argument list | |
| 139 | 139 | c.args=( | |
| 140 | 140 | '-R' "${c.destination_nfs_port}:localhost:${c.local_forward_port}" | |
| 141 | 141 | '-o' 'ExitOnForwardFailure=yes' | |
| 142 | 142 | "${c.args[@]}" | |
| 143 | 143 | ) | |
| 144 | 144 | fi | |
| 145 | 145 | ||
| 146 | + | # debug: print application data (compound c) | |
| 146 | 147 | ${mydebug} && print -v c | |
| 147 | 148 | ||
| 148 | 149 | print -u2 -f $"# ssh login data for destination machine:\n" | |
| 149 | 150 | ssh "${c.args[@]}" ; (( retval=$? )) | |
| 150 | 151 | ||
| 151 | 152 | if [[ -v c.nfs_server ]] ; then | |
| 152 | 153 | ssh -S "${c.ssh_control_socket_name}" -O 'exit' "root@${c.nfs_server.host}" | |
| 153 | 154 | fi | |
| 154 | 155 | ||
| 155 | 156 | wait | |
| 156 | 157 | ||
| 157 | 158 | return $retval | |
| 158 | 159 | } | |
| 159 | 160 | ||
| 160 | 161 | main "$@" | |
| 161 | 162 | exit $? | |
| 162 | 163 | ||
| 163 | 164 | # EOF. | |
| 164 | 165 |
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