Difference between
modified post eeb6493 by Anonymous on Mon 17th Jul 2023 15:25
original post 56a4a5f43 by Anonymous on Tue 11th Jul 2023 16:40
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/usr/bin/ksh93 | |
| 2 | + | ||
| 2 | 3 | # | |
| 3 | 4 | # sshnfs - remote login client with NFSv4 forwarding | |
| 4 | 5 | # | |
| 6 | + | ||
| 5 | 8 | # Example usage: | |
| 9 | + | # $ ksh sshnfs.ksh -o NFSURL=ssh+nfs://localhost/export/home/rmainz root@10.49.28.10 # | |
| 6 | 10 | # $ ksh sshnfs.ksh -o NFSURL=nfs://localhost/export/home/rmainz root@10.49.20.207 # | |
| 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 | |
| 11 | + | # $ ksh sshnfs.ksh -o NFSURL=nfs://derfwpc5131/export/home/rmainz root@10.49.28.10 # | |
| 8 | - | # $ ksh sshnfs.ksh -o NFSURL=nfs://derfwpc5131/export/home/rmainz target@fe80::d6f5:27ff:fe2b:8588%enp2s0 | |
| 12 | + | # $ ksh sshnfs.ksh -o NFSURL=ssh+nfs://derfwpc5131/export/home/rmainz -o SSHNFSJumphost=rmainz@derfwpc5131,roland.mainz@derfwnb8353 -J rmainz@derfwpc5131,roland.mainz@derfwnb8353 root@10.49.20.207 | |
| 9 | - | # $ ksh sshnfs.ksh -o NFSServerSSHLoginName=root -o NFSURL=nfs://derfwpc5131/export/home/rmainz root@10.49.28.56 | |
| 13 | + | # $ ksh sshnfs.ksh -o NFSURL=ssh+nfs://derfwpc5131/export/home/rmainz target@fe80::d6f5:27ff:fe2b:8588%enp2s0 | |
| 10 | - | # $ SSHNFS_OPTIONS='-o NFSServerSSHLoginName=root -o NFSURL=nfs://derfwpc5131/export/home/rmainz' sshnfs.ksh root@10.49.28.56 | |
| 14 | + | # $ ksh sshnfs.ksh -o NFSServerSSHLoginName=root -o NFSURL=ssh+nfs://derfwpc5131/export/home/rmainz root@10.49.28.56 | |
| 15 | + | # $ SSHNFS_OPTIONS='-o NFSServerSSHLoginName=root -o NFSURL=ssh+nfs://derfwpc5131/export/home/rmainz' sshnfs.ksh root@10.49.28.56 | |
| 11 | 16 | # | |
| 12 | 17 | # Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 13 | 18 | # | |
| 14 | 19 | ||
| 15 | 20 | # | |
| 16 | 21 | # simple netstat -n parser | |
| 17 | 22 | # | |
| 18 | 23 | function netstat_list_connections | |
| 19 | 24 | { | |
| 20 | 25 | set -o nounset | |
| 21 | 26 | nameref data=$1 | |
| 22 | 27 | ||
| 23 | 28 | compound out=( typeset stdout stderr ; integer res ) | |
| 24 | 29 | ||
| 25 | 30 | out.stderr="${ { out.stdout="${ LC_ALL='POSIX' PATH='/usr/bin:/bin' netstat -a -n ; (( out.res=$? )) ; }" ; } 2>&1 ; }" | |
| 26 | 31 | if (( out.res != 0 )) || [[ ${out.stderr} != '' ]] ; then | |
| 27 | 32 | return 1 | |
| 28 | 33 | fi | |
| 29 | 34 | ||
| 30 | 35 | typeset -a data.connections | |
| 31 | 36 | typeset l | |
| 32 | 37 | integer dci=0 # data.connections array index | |
| 33 | 38 | ||
| 34 | 39 | while read l ; do | |
| 35 | 40 | leftover="${l/~(Elrx) | |
| 36 | 41 | (?: # non-capturing group | |
| 37 | 42 | # | |
| 38 | 43 | # regex group for tcp,udp | |
| 39 | 44 | # | |
| 40 | 45 | (tcp|tcp6|udp|udp6|raw|raw6|sctp|sctp6) # Proto | |
| 41 | 46 | [[:space:]]+ | |
| 42 | 47 | ([[:digit:]]+) # Recv-Q | |
| 43 | 48 | [[:space:]]+ | |
| 44 | 49 | ([[:digit:]]+) # Send-Q | |
| 45 | 50 | [[:space:]]+ | |
| 46 | 51 | ([^[:space:]]+) # Local Address | |
| 47 | 52 | [[:space:]]+ | |
| 48 | 53 | ([^[:space:]]+) # Foreign Address | |
| 49 | 54 | (?: | |
| 50 | 55 | | | |
| 51 | 56 | [[:space:]]+ | |
| 52 | 57 | ([^[:space:]]*?) # State (Optional) | |
| 53 | 58 | ) | |
| 54 | 59 | | | |
| 55 | 60 | # | |
| 56 | 61 | # regex for unix | |
| 57 | 62 | # | |
| 58 | 63 | (unix) # Proto | |
| 59 | 64 | [[:space:]]+ | |
| 60 | 65 | ([[:digit:]]+) # RefCnt | |
| 61 | 66 | [[:space:]]+ | |
| 62 | 67 | (\[.+?\]) # Flags | |
| 63 | 68 | [[:space:]]+ | |
| 64 | 69 | ([^[:space:]]+) # Type | |
| 65 | 70 | [[:space:]]+ | |
| 66 | 71 | ([^[:space:]]*?) # State (optional) | |
| 67 | 72 | [[:space:]]+ | |
| 68 | 73 | ([[:digit:]]+) # I-Node | |
| 69 | 74 | (?: | |
| 70 | 75 | | | |
| 71 | 76 | [[:space:]]+ | |
| 72 | 77 | ([^[:space:]]+) # Path (optional) | |
| 73 | 78 | ) | |
| 74 | 79 | ) | |
| 75 | 80 | /X}" | |
| 76 | 81 | ||
| 77 | 82 | # If the regex above did not match then .sh.match | |
| 78 | 83 | # remains untouched, so we might see data from the | |
| 79 | 84 | # previous round. | |
| 80 | 85 | # So we check the "leftover" var whether it just | |
| 81 | 86 | # contains the dummy value of "X" to indicate a | |
| 82 | 87 | # successful regex match | |
| 83 | 88 | if [[ "$leftover" == 'X' ]] ; then | |
| 84 | 89 | #print -v .sh.match | |
| 85 | 90 | ||
| 86 | 91 | if [[ "${.sh.match[1]-}" != '' ]] ; then | |
| 87 | 92 | nameref dcn=data.connections[$dci] | |
| 88 | 93 | ||
| 89 | 94 | typeset dcn.proto="${.sh.match[1]}" | |
| 90 | 95 | typeset dcn.recv_q="${.sh.match[2]}" | |
| 91 | 96 | typeset dcn.send_q="${.sh.match[3]}" | |
| 92 | 97 | typeset dcn.local_address="${.sh.match[4]}" | |
| 93 | 98 | typeset dcn.foreign_address="${.sh.match[5]}" | |
| 94 | 99 | typeset dcn.state="${.sh.match[6]}" | |
| 95 | 100 | ((dci++)) | |
| 96 | 101 | elif [[ "${.sh.match[7]-}" != '' ]] ; then | |
| 97 | 102 | nameref dcn=data.connections[$dci] | |
| 98 | 103 | ||
| 99 | 104 | typeset dcn.proto="${.sh.match[7]}" | |
| 100 | 105 | typeset dcn.refcnt="${.sh.match[8]}" | |
| 101 | 106 | typeset dcn.flags="${.sh.match[9]}" | |
| 102 | 107 | typeset dcn.type="${.sh.match[10]}" | |
| 103 | 108 | [[ "${.sh.match[11]}" != '' ]] && typeset dcn.state="${.sh.match[11]}" | |
| 104 | 109 | typeset dcn.inode="${.sh.match[12]}" | |
| 105 | 110 | [[ "${.sh.match[13]}" != '' ]] && typeset dcn.path="${.sh.match[13]}" | |
| 106 | 111 | ((dci++)) | |
| 107 | 112 | fi | |
| 108 | 113 | else | |
| 109 | 114 | true | |
| 110 | 115 | #printf $"leftover=%q\n" "${leftover}" | |
| 111 | 116 | fi | |
| 112 | 117 | done <<<"${out.stdout}" | |
| 113 | 118 | ||
| 114 | 119 | return 0 | |
| 115 | 120 | } | |
| 116 | 121 | ||
| 117 | 122 | function netstat_list_active_local_tcp_connections | |
| 118 | 123 | { | |
| 119 | 124 | set -o nounset | |
| 120 | 125 | nameref ar=$1 | |
| 121 | 126 | compound c | |
| 122 | 127 | integer port | |
| 123 | 128 | integer i | |
| 124 | 129 | ||
| 125 | 130 | netstat_list_connections c || return 1 | |
| 126 | 131 | #print -v c | |
| 127 | 132 | ||
| 128 | 133 | [[ -v ar ]] || integer -a ar | |
| 129 | 134 | ||
| 130 | 135 | for i in "${!c.connections[@]}" ; do | |
| 131 | 136 | nameref n=c.connections[$i] | |
| 132 | 137 | ||
| 133 | 138 | # look for only for TCP connections which match | |
| 134 | 139 | # 127.0.*.* or IPv6 ::1 for localhost | |
| 135 | 140 | # 0.0.0.0 or IPv6 :: for all addresses (e.g. servers) | |
| 136 | 141 | if [[ "${n.proto}" == ~(El)tcp && \ | |
| 137 | 142 | "${n.local_address}" == ~(Elr)((127\.0\..+|::1)|(::|0\.0\.0\.0|)):[[:digit:]]+ ]] ; then | |
| 138 | 143 | ||
| 139 | 144 | port="${n.local_address##*:}" | |
| 140 | 145 | #printf $"port = %d\n" port | |
| 141 | 146 | ||
| 142 | 147 | (( ar[port]=1 )) | |
| 143 | 148 | fi | |
| 144 | 149 | done | |
| 145 | 150 | ||
| 146 | 151 | return 0 | |
| 147 | 152 | } | |
| 148 | 153 | ||
| 149 | 154 | function netstat_find_next_free_local_tcp_port | |
| 150 | 155 | { | |
| 151 | 156 | set -o nounset | |
| 152 | 157 | compound c=( integer -a ar ) | |
| 153 | 158 | nameref ret_free_port=$1 | |
| 154 | 159 | integer start_port | |
| 155 | 160 | integer end_port | |
| 156 | 161 | integer i | |
| 157 | 162 | ||
| 158 | 163 | netstat_list_active_local_tcp_connections c.ar || return 1 | |
| 159 | 164 | ||
| 160 | 165 | #print -v c | |
| 161 | 166 | ||
| 162 | 167 | (( start_port=$2 )) | |
| 163 | 168 | if (( $# > 2 )) ; then | |
| 164 | 169 | (( end_port=$3 )) | |
| 165 | 170 | else | |
| 166 | 171 | (( end_port=65535 )) | |
| 167 | 172 | fi | |
| 168 | 173 | ||
| 169 | 174 | for ((i=start_port ; i < end_port ; i++ )) ; do | |
| 170 | 175 | if [[ ! -v c.ar[i] ]] ; then | |
| 171 | 176 | (( ret_free_port=i )) | |
| 172 | 177 | return 0 | |
| 173 | 178 | fi | |
| 174 | 179 | done | |
| 175 | 180 | ||
| 176 | 181 | return 1 | |
| 177 | 182 | } | |
| 178 | 183 | ||
| 179 | 184 | # | |
| 180 | 185 | # parse url | |
| 181 | 186 | # | |
| 182 | 187 | # returns: | |
| 183 | 188 | # data.protocol | |
| 184 | 189 | # data.host | |
| 185 | 190 | # data.port (optional) | |
| 186 | 191 | # data.path | |
| 187 | 192 | # | |
| 188 | 193 | function parse_url | |
| 189 | 194 | { | |
| 190 | 195 | typeset url="$2" | |
| 191 | 196 | typeset leftover | |
| 192 | 197 | nameref data="$1" | |
| 193 | 198 | ||
| 194 | 199 | # ~(E) is POSIX extended regular expression matching (instead of | |
| 195 | 200 | # shell pattern) | |
| 196 | 201 | leftover="${url//~(Elr)(.+?):\/\/(.+?)(?:|:([[:digit:]]+))(?:\/(.*?))?/X}" | |
| 197 | 202 | ||
| 198 | 203 | # All parsed data should be captured via eregex in .sh.match - if | |
| 199 | 204 | # there is anything left (except the 'X') then the input string did not | |
| 200 | 205 | # properly match the eregex | |
| 201 | 206 | [[ "$leftover" == 'X' ]] || { print -u2 -f $"%s: Parser error\n" "$0" ; return 1 ; } | |
| 202 | 207 | ||
| 203 | 208 | data.protocol="${.sh.match[1]}" | |
| 204 | 209 | data.host="${.sh.match[2]}" | |
| 205 | 210 | # bug: should be [[ -v .sh.match[3] ]], but ksh93u has bugs | |
| 206 | 211 | [[ "${.sh.match[3]}" != '' ]] && integer data.port="${.sh.match[3]}" | |
| 207 | 212 | data.path="${.sh.match[4]}" | |
| 208 | 213 | ||
| 209 | 214 | return 0 | |
| 210 | 215 | } | |
| 211 | 216 | ||
| 212 | - | function parse_nfs_url | |
| 217 | + | function parse_sshnfs_url | |
| 213 | 218 | { | |
| 214 | 219 | typeset url="$2" | |
| 215 | 220 | nameref data="$1" | |
| 216 | 221 | ||
| 217 | 222 | parse_url data "$url" || return 1 | |
| 218 | 223 | ||
| 219 | - | [[ "${data.protocol}" == 'nfs' ]] || { print -u2 -f $"%s: Not a NFS url\n" "$0" ; return 1 ; } | |
| 224 | + | [[ "${data.protocol}" == ~(Elr)(ssh\+nfs|nfs) ]] || \ | |
| 220 | - | [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; } | |
| 225 | + | { print -u2 -f $"%s: Not a nfs:// or ssh+nfs:// url\n" "$0" ; return 1 ; } | |
| 221 | - | [[ "${data.path}" != '' ]] || { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; } | |
| 226 | + | [[ "${data.host}" != '' ]] || { print -u2 -f $"%s: NFS hostname missing\n" "$0" ; return 1 ; } | |
| 227 | + | [[ "${data.path}" != '' ]] || { print -u2 -f $"%s: NFS path missing\n" "$0" ; return 1 ; } | |
| 222 | 228 | ||
| 223 | 229 | if [[ ! -v data.port ]] ; then | |
| 224 | 230 | # use # default NFSv4 TCP port number (see | |
| 225 | 231 | # $ getent services nfs #) | |
| 226 | 232 | integer data.port=2049 | |
| 227 | 233 | fi | |
| 228 | 234 | ||
| 229 | 235 | return 0 | |
| 230 | 236 | } | |
| 231 | 237 | ||
| 232 | 238 | ||
| 233 | 239 | function main | |
| 234 | 240 | { | |
| 235 | 241 | set -o nounset | |
| 236 | 242 | typeset mydebug=false # fixme: should be "bool" for ksh93v | |
| 237 | 243 | integer i | |
| 238 | 244 | integer retval | |
| 239 | 245 | compound c=( | |
| 240 | 246 | # User name for SSH login to NFS server | |
| 241 | 247 | typeset nfsserver_ssh_login_name="$LOGNAME" | |
| 248 | + | ) | |
| 242 | 249 | ||
| 244 | - | integer local_forward_port | |
| 245 | 250 | ||
| 246 | - | # TCP port on destination machine where we forward the | |
| 247 | - | # NFS port from the server | |
| 248 | - | integer destination_nfs_port=33049 | |
| 249 | 251 | ||
| 250 | - | typeset ssh_control_socket_name="/tmp/sshnfs_ssh-control-socket_logname${LOGNAME}_ppid${PPID}_pid$$" | |
| 251 | - | ) | |
| 252 | - | ||
| 252 | + | # |
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