HSR-Mountscript
Aus alt.comp.hsr
Wie man im Artikel SMB-Shares-Shell sehen kann, können die Samba-Shares mithilfe von smbfs direkt ins Dateisystem eingebunden werden. Wenn dies in einem Bash-Script gemacht wird, wird das Passwort im Klartext gespeichert, was sicherheitstechnisch nicht so toll ist.
Deshalb hier ein Python-Script mit Passwortabfrage ohne Anzeige des Passwords bei der Eingabe.
Inhaltsverzeichnis
Voraussetzungen
python und cifs-utils müssen installiert sein.
Installation
- Quellcode kopieren und in einer Datei z.B. im Homeverzeichnis abspeichern, z.B.
~/hsrmount.py - Im Script HSRUSERNAME durch den HSR-Benutzernamen und LOCALUSERNAME durch den Namen des lokalen Linux-Users ersetzen
- Ausführ-Berechtigungen setzen:
chmod +x ~/hsrmount.py
Benutzung
Das Script muss mit Root-Berechtigungen ausgeführt werden, beispielsweise mit $ sudo ./hsrmount.py
Script
#!/usr/bin/python
#
# Simple script to mount shares at HSR
# v1.0 (20.09.2010)
#
# Author: Danilo Bargen <gezuru@gmail.com>
# Some code stolen from http://sel.pastebin.com/9kCpmA4y
#
# License:
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
############################
# Set the following options
############################
username = "HSRUSERNAME" # HSR username
localuser = "LOCALUSERNAME" # The local user that should have write access to the mounted share
server = "vf3.hsr.ch" # Server providing the following shares
shares = ["skripte", "tmp", username] # The shares on the server to mount
##############
# Main script
##############
import getpass, os, sys, subprocess
# Check for root permissions
if os.getuid():
print "You need root permissions to mount smb shares."
sys.exit(0)
# Get HSR password
password = getpass.getpass("HSR Password: ")
try:
retval = ""
print ""
for entry in shares:
retval = subprocess.check_call(["mkdir", "-p", "/mnt/%s" % entry])
retval = retval + subprocess.check_call(["mount", "-t", "cifs", "//%s/%s" % (server, entry), "-o", "user=%s,pass=%s" % (username, password), "-o", "uid=%s" % localuser, "/mnt/%s" % entry])
print "Mounted /mnt/%s" % (entry)
print "\nDone mounting."
except:
print "\nAn error occured. Blame the monkeys."