ubuntuusers.de

Anhang: SMBsession-Gast

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#! /bin/bash -e

# Nautilus helper script:
#	~/.local/share/nautilus/scripts/'SMBsession Gast'
#	~/.local/share/nautilus/scripts/'Was ist das?'

# Browse SMB networks and shares
# don't require SMBv1/CIFS for access

# SPDX-License-Identifier: GPL-2.0-or-later
# (c) Copyright 2020 kB @ ubuntuusers.de

readonly W0=3 dW=8 Wmax=1000 H0=5 dH=24 Hmax=600
LOG()	{ echo "${0##*/}: $*" ;}  >&2

#gio mount network:///	|| true					# Hack!
gio info smb:/// >/dev/null || gio mount smb:/// || true	# Hack!

sharelist()
  for P in SMB2 NT1 CORE
  do	smbclient -N -U '' -L $1				\
	  --option="client min protocol = $P" && break || continue
  done

Filter()
  { sed '1,/^\t-/ d ; /^[^\t]/,$ d ; s/^\t//' | sed 's/   */\t/g' ;}

shopt -s lastpipe
init_Dialog()
{ W=${#URI} H=0
  while read x ; do (( H++ , ${#x} < W )) || W=${#x} ; done
}
Dialog()
{ (( H += H0 , H *= dH , H < Hmax )) || H=$Hmax
  (( W += W0 , W *= dW , W < Wmax )) || W=$Wmax
  zenity "$@" --width=$W --height=$H --title=$URI
}

F=$(mktemp) && trap "rm -f $F" EXIT ERR
INFO()
{ { echo "$*" ; cat - ;} | tee $F | init_Dialog
  Dialog --info --text="$(cat $F)" --timeout=60 || true # wg. timeout
}
WS=0 ;	for col in Sharename       Type      Comment
	 do columns+=" --column=$col " ; (( WS += 8 )) ; done
LIST()
{ tee $F | init_Dialog ; (( W += WS )) ; <$F sed 's/\t/\n/g'	|
  Dialog --list $columns --print-column=1 --multiple --separator=$'\n'
}

FORMS()
{ init_Dialog ; (( W *= 2 ))
  Dialog "$@" --forms --cancel-label=Abbruch --ok-label=Weiter
}

Group=$(testparm -s -v 2>/dev/null | grep workgroup ) ; Group=${Group##* }
UName=${0#$( sed 's/[A-Z0-9a-z]*$//' <<< "$0" )} PWord=
noUN=$USER noPW='{}'	# Ersatzwerte wenn Benutzername bzw. Passwort leer

Username()
{ UName=$(FORMS --text="Anmeldung als …" --add-entry=Benutzername) ;}

Password()
{ X='Benutzer ändern'
  until	PWord=$(FORMS	 --text="SMB-Passwort von $UName:"	\
			 --add-password=Passwort		\
			 --extra-button="$X"		 )
     do	test "$PWord" = "$X" && Username || return 1 ; done
}
retry()
{ init_Dialog ; Dialog --question --text='Fehlschlag!'		\
    --cancel-label=Abbruch --ok-label=Weiter --timeout=10
}

Mount()
{ Server=${1#*//} ; Server=${Server%/} ; Server=${Server%%.*}
  LOG "$URI --> $1 --> $Server"
  makeURI()	{ URI="smb://$Group;${UName:=$noUN}@$Server/$1" ;}
  sharelist //$Server | Filter | LIST | readarray
  for z in ${MAPFILE[@]}
  do	N=3 ;	makeURI $z
	until	gio open $URI && LOG $URI eingehängt || (( N-- < 1 ))
	do	Password && makeURI $z || break
		gio mount $URI <<< ${PWord:-$noPW} || retry || break
	done	|| true
  done
}

for URI in ${NAUTILUS_SCRIPT_SELECTED_URIS:-$@}
do action= ; Info=$(gio info $URI) || continue
  z=$( grep 'standard::target-uri' <<< "$Info" || true ) ; z=${z#*: }
  case $URI
    in (smb:*)		case $( grep 'id::filesystem: ' <<< "$Info" )
			  in (*smb-server*)	action=mount
			  ;; (*smb-net*)	action=list
			esac
    ;; (network:*)	case $URI
			  in (*dnssd-domain*)	action=mount
			  ;; (*smb-server*)	action=mount
			  ;; (*smb-root)	action=list
			esac
  esac
  case $action
    in (mount)	Mount $z
    ;; (list)	gio list $z | INFO
    ;; (*)	INFO <<< "$Info"
  esac
done
LOG Das ist das Ende
Anhang herunterladen

Skript SMBsession für den UU-Artikel Nautilus/Skripte/SMB-Session

Es wird auch in Samba_Client/Windows-Netzwerk verwendet und kann auch als nichttriviales Beispiel für zenity dienen.

Lizenz: GPLv2 or later, (c) Copyright 2020 kB @ UbuntuUsers.de

Diese Revision wurde am 16. Juni 2020 12:58 von ubuntuusers erstellt.