Skip to content
Snippets Groups Projects
Commit 7db88138 authored by Sven Mäder's avatar Sven Mäder :speech_balloon:
Browse files

Init

parents
No related branches found
No related tags found
No related merge requests found
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
#lib/
#lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
#vscode
.vscode/
# python
This repo contains python scripts and modules which may not be available as package.
## installation
Clone the repo:
```
git clone git@gitlab.phys.ethz.ch:core/python.git
cd python
```
Install the requirements.
## requirements
### system
**Required** minimal packages:
```
apt install python3
apt install python3-setuptools
apt install python3-pip
```
**Optional** packages (these packages will probably be outdated in debian/ubuntu, and may break scripts):
```
apt install python3-ldap3
apt install python3-gssapi
apt install python3-tabulate
```
### git / pip
**Required** for modules installation via `pip`:
```
apt install libkrb5-dev # required for `gssapi` module
```
**Required** modules in local lib directory:
```
/usr/bin/pip3 install --upgrade pip
/usr/bin/pip3 install -r requirements-git.txt -t lib/git
/usr/bin/pip3 install -r requirements-pip.txt -t lib/pip
```
**Alternative** install from a clean venv (may be needed if you have any conflicting outdated modules installed as packages):
```
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements-git.txt -t lib/git
pip3 install -r requirements-pip.txt -t lib/pip
deactivate
rm -r venv
```
## modules
Store or load modules in these directories:
- `lib/isg/`: modules developed at isg dphys
- `lib/git/`: modules installed with git (github)
- `lib/pip/`: modules installed with pip
In your script or module the following 2 imports are required in order:
```python
import lib_path
import lib
```
After that you can import modules from the lib paths, searched in the following order `isg` > `git` > `pip` > `system`.`
## scripts
Store scripts here:
- `bin/`
#!/usr/bin/env python3
import os
import sys
print('__file__: ' + __file__)
script_path = os.path.dirname(os.path.realpath(__file__))
print('script_path: ' + script_path)
lib_path = os.path.abspath('/'.join([script_path, '..']))
print('lib_path: ' + lib_path)
sys.path.insert(0, lib_path)
print('sys.path: ')
for path in sys.path:
print(' ' * 4 + path)
#!/usr/bin/env python3
import lib_path
import lib
import argparse
import tabulate
import dphysldap
def main():
parser = argparse.ArgumentParser(
add_help=False, description='Show group members')
parser.add_argument('group', help='The group name (cn)')
parser.add_argument('-a', '--access', dest='access',
action='store_const', const=True,
help='Show access rights column')
parser.add_argument('-b', '--blocked', dest='blocked',
action='store_const', const=True,
help='Show blocked column')
parser.add_argument('-m', '--mail', dest='mail',
action='store_const', const=True,
help='Show mail column')
parser.add_argument('-h', '--help',
action='help',
help='Show this help message and exit')
arg = vars(parser.parse_args())
ldap = dphysldap.Ldap()
group_query = 'cn: {0}'.format(arg['group'])
group_attrs = ['cn', 'gidNumber', 'memberUid']
groups = ldap.get_groups(query=group_query, attributes=group_attrs)
if len(groups) != 1:
sys.exit('error: number of groups matched: {0}'.format(len(groups)))
group = groups[0]
if 'memberUid' not in group:
sys.exit('error: empty group')
members = group['memberUid']
user_query = 'uid:' + ';'.join(members)
user_attrs = ['uid', 'uidNumber', 'gecos']
if arg['blocked']:
user_attrs.append('blocked')
if arg['access']:
user_attrs.append('accessRight')
if arg['mail']:
user_attrs.append('mail')
users = ldap.get_users(query=user_query, attributes=user_attrs)
if not users:
sys.exit('error: no users found')
cn = ','.join(group['cn'])
gid = ','.join([str(gid) for gid in group['gidNumber']])
print('Members of {} ({}):'.format(cn, gid))
table = list()
for u in users:
row = list()
for attr in user_attrs:
row.append(','.join([str(a) for a in u[attr]]))
table.append(row)
print(tabulate.tabulate(table, tablefmt='simple', headers=user_attrs))
if __name__ == "__main__":
main()
git/
pip/
#!/usr/bin/env python3
import os
import sys
print('__file__: ' + __file__)
lib_path = os.path.dirname(os.path.realpath(__file__))
print('lib_path: ' + lib_path)
lib_pip = '/'.join([lib_path, 'pip'])
lib_isg = '/'.join([lib_path, 'git'])
lib_isg = '/'.join([lib_path, 'isg'])
sys.path.insert(0, lib_pip)
sys.path.insert(0, lib_git)
sys.path.insert(0, lib_isg)
print('sys.path: ')
for path in sys.path:
print(' ' * 4 + path)
#!/usr/bin/env python3
import lib_path
import lib
import ldap3
import ssl
SERVERS = ['phd-aa1.ethz.ch', 'phd-aa2.ethz.ch', 'phd-aa3.ethz.ch']
BASE = 'dc=phys,dc=ethz,dc=ch'
CA_CERTS = '/etc/ssl/certs/ca-certificates.crt'
class Ldap(object):
"""
LDAP connection to random server in pool
"""
def __init__(self, server_names=SERVERS, base=BASE, ca_certs_file=CA_CERTS):
self.server_names = server_names
self.base = base
self.tls = ldap3.Tls(
validate=ssl.CERT_REQUIRED,
version=ssl.PROTOCOL_TLSv1_2,
ca_certs_file=ca_certs_file)
self.servers = [ldap3.Server(s, tls=self.tls, get_info=ldap3.ALL) for s in self.server_names]
self.server_pool = ldap3.ServerPool(
self.servers,
pool_strategy=ldap3.RANDOM,
active=True,
exhaust=False)
self.connection = ldap3.Connection(
self.server_pool,
authentication='ANONYMOUS',
auto_bind='NONE',
version=3,
client_strategy='SYNC')
self.connection.open()
self.connection.start_tls()
self.connection.bind()
self.user_classes = ['posixAccount', 'dphysUser', 'inetOrgPerson', 'shadowAccount']
self.group_classes = ['posixGroup', 'dphysGroup']
self.obj_user = None
self.obj_group = None
def get_entries(self, obj, query='', attributes=None):
"""
Returns a list with entries as dict
"""
reader = ldap3.Reader(self.connection, obj, self.base, query, attributes)
reader.search()
return [e.entry_attributes_as_dict for e in reader.entries]
def get_users(self, query='', attributes=None):
"""
Returns a list with users as dict
"""
if not self.obj_user:
self.obj_user = ldap3.ObjectDef(self.user_classes, self.connection)
return self.get_entries(self.obj_user, query=query, attributes=attributes)
def get_groups(self, query='', attributes=None):
"""
Returns a list with groups as dict
"""
if not self.obj_group:
self.obj_group = ldap3.ObjectDef(self.group_classes, self.connection)
return self.get_entries(self.obj_group, query=query, attributes=attributes)
#!/usr/bin/env python3
import os
import sys
print('__file__: ' + __file__)
script_path = os.path.dirname(os.path.realpath(__file__))
print('script_path: ' + script_path)
lib_path = os.path.abspath('/'.join([script_path, '../..']))
print('lib_path: ' + lib_path)
sys.path.insert(0, lib_path)
print('sys.path: ')
for path in sys.path:
print(' ' * 4 + path)
git+https://github.com/skurfer/python-xymon.git#egg=xymon
gssapi
ldap3
tabulate
-r requirements-git.txt
-r requirements-pip.txt
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment