Difference between
modified post 28082dfc0 by Anonymous on Fri 10th Feb 2023 11:58
original post 80a634c by Anonymous on Fri 10th Feb 2023 11:05
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 | 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 then the input string did not properly match | |
| 33 | 33 | # 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 | return 0 | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | 44 | function parse_nfs_url | |
| 45 | 45 | { | |
| 46 | 46 | typeset url="$2" | |
| 47 | 47 | nameref data="$1" | |
| 48 | 48 | ||
| 49 | 49 | parse_url data "$url" || return 1 | |
| 50 | 50 | ||
| 51 | 51 | [[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; } | |
| 52 | 52 | [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; } | |
| 53 | 53 | [[ "${data.path}" != '' ]] || { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; } | |
| 54 | 54 | ||
| 55 | 55 | if [[ ! -v data.port ]] ; then | |
| 56 | 56 | integer data.port=2049 | |
| 57 | 57 | fi | |
| 58 | 58 | ||
| 59 | 59 | return 0 | |
| 60 | 60 | } | |
| 61 | 61 | ||
| 62 | 62 | ||
| 63 | 63 | function main | |
| 64 | 64 | { | |
| 65 | 65 | integer i | |
| 66 | 66 | compound c | |
| 67 | 67 | compound c.nfs_data | |
| 68 | 68 | integer c.forward_port=34049 | |
| 69 | 69 | integer retval | |
| 70 | 70 | ||
| 71 | 71 | #set -o xtrace | |
| 72 | 72 | set -o nounset | |
| 73 | 73 | ||
| 74 | 74 | typeset c.args=( "$@" ) | |
| 75 | 75 | ||
| 76 | 76 | for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do | |
| 77 | 77 | if [[ "${c.args[i]}" == '-o' ]] ; then | |
| 78 | 78 | case "${c.args[i+1]-}" in | |
| 79 | 79 | 'NFSURL='*) | |
| 80 | 80 | unset c.nfs_data | |
| 81 | 81 | typeset c.url="${c.args[i+1]#NFSURL=}" | |
| 82 | 82 | parse_nfs_url c.nfs_data "${c.url}" || return 1 | |
| 83 | 83 | unset c.args[$i] c.args[$((i+1))] | |
| 84 | 84 | ((i++)) | |
| 85 | 85 | ;; | |
| 86 | 86 | 'NFSJumphost='*) | |
| 87 | 87 | [[ ! -v c.ssh_jumphost_args ]] && typeset -a c.ssh_jumphost_args | |
| 88 | 88 | c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" ) | |
| 89 | 89 | unset c.args[$i] c.args[$((i+1))] | |
| 90 | 90 | ((i++)) | |
| 91 | 91 | ;; | |
| 92 | 92 | esac | |
| 93 | 93 | fi | |
| 94 | 94 | done | |
| 95 | 95 | ||
| 96 | 96 | # bug: should be [[ -v c.nfs_data ]], but ksh93u has bugs | |
| 97 | 97 | if [[ -v c.nfs_data.host ]] ; then | |
| 98 | 98 | # Forward NFS port from server to local machine | |
| 99 | 99 | # Notes: | |
| 100 | 100 | # - We use $ ssh -M ... # here as a way to terminate the port | |
| 101 | 101 | # forwarding process later using "-O exit" without the need | |
| 102 | 102 | # for a pid | |
| 103 | 103 | printf $"# Please enter the password for NFS server (%s)\n" \ | |
| 104 | 104 | "root@${c.nfs_data.host}" | |
| 105 | 105 | ssh \ | |
| 106 | 106 | -L "${c.forward_port}:localhost:${c.nfs_data.port}" \ | |
| 107 | 107 | -M -S "$PWD/ssh-control-socket" \ | |
| 108 | 108 | -N \ | |
| 109 | 109 | -f -o ExitOnForwardFailure=yes \ | |
| 110 | 110 | "${c.ssh_jumphost_args[@]}" \ | |
| 111 | 111 | "root@${c.nfs_data.host}" | |
| 112 | 112 | if (( $? != 0 )) ; then | |
| 113 | 113 | printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $? | |
| 114 | 114 | return 1 | |
| 115 | 115 | fi | |
| 116 | 116 | #ssh -S "$PWD/ssh-control-socket" -O 'check' "root@${c.nfs_data.host}" | |
| 117 | 117 | ||
| 118 | 118 | printf $"# Use this to mount the directory:\n" | |
| 119 | 119 | printf $"# $ mkdir /mnt_nfs\n" | |
| 120 | - | printf $"# mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \ | |
| 120 | + | printf $"# $ mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \ | |
| 121 | 121 | 33049 \ | |
| 122 | 122 | "${c.nfs_data.path}" | |
| 123 | 123 | ||
| 124 | 124 | # add NFS forwarding options to main ssh argument list | |
| 125 | 125 | c.args=( "-R" "33049:localhost:${c.forward_port}" "${c.args[@]}" ) | |
| 126 | 126 | fi | |
| 127 | 127 | ||
| 128 | 128 | ssh "${c.args[@]}" ; (( retval=$? )) | |
| 129 | 129 | ||
| 130 | 130 | if [[ -v c.nfs_data.host ]] ; then | |
| 131 | 131 | ssh -S "$PWD/ssh-control-socket" -O 'exit' "root@${c.nfs_data.host}" | |
| 132 | 132 | fi | |
| 133 | 133 | ||
| 134 | 134 | wait | |
| 135 | 135 | ||
| 136 | 136 | return $retval | |
| 137 | 137 | } | |
| 138 | 138 | ||
| 139 | 139 | main "$@" | |
| 140 | 140 | ||
| 141 | 141 | # EOF. | |
| 142 | 142 |
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