Difference between
modified post d39479 by Anonymous on Wed 8th Feb 2023 14:54
original post 42df74a0 by Anonymous on Wed 8th Feb 2023 13:33
Download diff
Show
old version |
new version |
both versions
| 1 | + | #!/usr/bin/ksh93 | |
| 1 | 2 | # | |
| 2 | 3 | # sshnfs - remote login client with NFSv4 forwarding | |
| 3 | 4 | # | |
| 5 | + | # Example usage: | |
| 5 | 7 | # | |
| 8 | + | # Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 7 | 9 | # | |
| 10 | + | ||
| 8 | 14 | # returns: | |
| 9 | 15 | # data.protocol | |
| 10 | 16 | # data.host | |
| 11 | 17 | # data.port (optional) | |
| 12 | 18 | # data.path | |
| 13 | 19 | # | |
| 14 | 20 | function parse_url | |
| 15 | 21 | { | |
| 16 | 22 | typeset url="$2" | |
| 17 | 23 | typeset leftover | |
| 18 | 24 | nameref data="$1" | |
| 19 | 25 | ||
| 20 | 26 | leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/x}" | |
| 21 | 27 | ||
| 22 | 28 | [[ "$leftover" == 'x' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; } | |
| 23 | 29 | ||
| 24 | 30 | #print -v .sh.match | |
| 25 | 31 | ||
| 26 | 32 | data.protocol="${.sh.match[1]}" | |
| 27 | 33 | data.host="${.sh.match[2]}" | |
| 28 | 34 | # bug: should be [[ -v .sh.match[3] }}, but ksh93u has bugs | |
| 29 | 35 | [[ "${.sh.match[3]}" != '' ]] && integer data.port="${.sh.match[3]}" | |
| 30 | 36 | data.path="${.sh.match[4]}" | |
| 31 | 37 | return 0 | |
| 32 | 38 | } | |
| 33 | 39 | ||
| 34 | 40 | function parse_nfs_url | |
| 35 | 41 | { | |
| 36 | 42 | typeset url="$2" | |
| 37 | 43 | nameref data="$1" | |
| 38 | 44 | ||
| 39 | 45 | parse_url data "$url" || return 1 | |
| 40 | 46 | ||
| 41 | - | [[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; } | |
| 47 | + | [[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; } | |
| 48 | + | [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; } | |
| 42 | 50 | ||
| 43 | 51 | if [[ ! -v data.port ]] ; then | |
| 44 | 52 | integer data.port=2049 | |
| 45 | 53 | fi | |
| 46 | 54 | ||
| 47 | 55 | return 0 | |
| 48 | 56 | } | |
| 49 | 57 | ||
| 50 | 58 | ||
| 51 | 59 | function main | |
| 52 | 60 | { | |
| 61 | + | integer i | |
| 53 | 62 | compound c | |
| 54 | 63 | compound c.nfs_data | |
| 55 | 64 | integer c.forward_port=4049 | |
| 56 | - | set -x | |
| 57 | - | url="$1" | |
| 58 | - | shift | |
| 59 | - | parse_nfs_url c.nfs_data "$url" || return 1 | |
| 60 | 65 | ||
| 61 | - | # $ ssh -f ... # puts the ssh process into the bachground, but | |
| 66 | + | set -o xtrace | |
| 62 | - | # we have no way to determinate the process id. The "fix" is | |
| 67 | + | set -o nounset | |
| 70 | 68 | ||
| 71 | - | ssh -Y -R "3049:localhost:${c.forward_port}" root@10.49.20.207 | |
| 69 | + | url="$1" # fixme: we should use sshnfs -o NFSURL=nfs:// ... | |
| 70 | + | shift | |
| 72 | 72 | ||
| 73 | - | kill -s TERM "${c.forward_pid}" | |
| 73 | + | parse_nfs_url c.nfs_data "$url" || return 1 | |
| 75 | 74 | ||
| 76 | - | return 0 | |
| 75 | + | # | |
| 77 | - | } | |
| 76 | + | # extract jump host args | |
| 78 | - | ||
| 77 | + | # | |
| 79 | - | main "$@" | |
| 78 | + | typeset -a c.ssh_jumphost_args | |
| 80 | - | ||
| 79 | + | for ((i=0 ; i < ${#c.args[@]} ; i++)) ; do | |
| 81 | - | # EOF. | |
| 80 | + | if [[ "${c.args[i]}" == '-o' ]] ; then | |
| 82 | - | ||
| 81 | + | if [[ "${c.args[i+1]}" == ~(Elr)NFSJumphost=.+ ]] ; then | |
| 83 | - | ||
| 82 | + | c.ssh_jumphost_args+=( "-J" "${c.args[i+1]#NFSJumphost=}" ) | |
| 84 | - | ||
| 83 | + | unset c.args[$i] | |
| 85 | - | ||
| 84 | + | unset c.args[$((i+1))] | |
| 86 | - | ||
| 85 | + | fi | |
| 87 | - | ||
| 86 | + | fi | |
| 88 | - | ||
| 87 | + | done | |
| 89 | 88 | ||
| 90 | - | ||
| 89 | + | # Forward NFS port from server to local machine | |
| 91 | - | ||
| 90 | + | # Notes: | |
| 92 | - | ||
| 91 | + | # - We use $ ssh -M ... # here as a way to terminate the port | |
| 93 | - | ||
| 92 | + | # forwarding process later using "-O exit" without the need | |
| 94 | - | ||
| 93 | + | # for a pid | |
| 95 | - | ||
| 94 | + | printf $"# Please enter the password for NFS server (%s)\n" \ | |
| 96 | - | ||
| 95 | + | "root@${c.nfs_data.host}" | |
| 97 | - | ||
| 96 | + | ssh \ | |
| 98 | - | ||
| 97 | + | -L "${c.forward_port}:localhost:${c.nfs_data.port}" \ | |
| 99 | - | ||
| 98 | + | -M -S "$PWD/ssh-control-socket" \ | |
| 100 | - | ||
| 99 | + | -N \ | |
| 101 | - | ||
| 100 | + | -f -o ExitOnForwardFailure=yes \ | |
| 102 | - | ||
| 101 | + | "${c.ssh_jumphost_args[@]}" \ | |
| 103 | - | ||
| 102 | + | "root@${c.nfs_data.host}" | |
| 104 | - | ||
| 103 | + | (( $? == 0 )) || { printf $"%s: NFS forwarding ssh failed with error code %d\n" "$0" $? ; return 1 ; } | |
| 105 | - | ||
| 104 | + | #ssh -S "$PWD/ssh-control-socket" -O 'check' "root@${c.nfs_data.host}" | |
| 106 | 105 | ||
| 107 | - | ||
| 106 | + | printf $"# Use this to mount the directory:\n" | |
| 108 | - | ||
| 107 | + | printf $"# $ mkdir /mnt_nfs\n" | |
| 109 | - | ||
| 108 | + | printf $"# mount -vvv -t nfs -o vers=4,port=%d localhost:/%s /mnt_nfs\n" \ | |
| 110 | - | ||
| 109 | + | 3049 \ | |
| 111 | - | ||
| 110 | + | "${c.nfs_data.path}" | |
| 112 | 111 | ||
| 113 | - | ||
| 112 | + | ssh -R "3049:localhost:${c.forward_port}" "${c.args[@]}" # root@10.49.20.207 | |
| 114 | 113 | ||
| 115 | - | ||
| 114 | + | ssh -S "$PWD/ssh-control-socket" -O 'exit' "root@${c.nfs_data.host}" | |
| 116 | - | ||
| 115 | + | wait | |
| 117 | 116 | ||
| 118 | - | ||
| 117 | + | return 0 | |
| 119 | - | ||
| 118 | + | } | |
| 120 | 119 | ||
| 121 | - | ||
| 120 | + | main "$@" | |
| 122 | 121 | ||
| 123 | - | ||
| 122 | + | # EOF. | |
| 124 | 123 |
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