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