def remove_from_groups(username, groups): for group in groups: shell('sudo gpasswd -d {} {}'.format(username, group)) should = [] for ag in api.parsers.users.get_groups(username): should.append(ag['name']) if not all(name in should for name in groups): return api.parsers.users.parse_single(username)
def add_to_groups(username, groups): groups_string = ','.join(groups) shell('sudo usermod -a -G {} {}'.format(groups_string, username)) should = [] for ag in api.parsers.users.get_groups(username): should.append(ag['name']) if all(name in should for name in groups): return api.parsers.users.parse_single(username)
def delete(username, remove_home=True): if remove_home: shell('sudo deluser --remove-home {}'.format(username)) else: shell('sudo deluser {}'.format(username)) if api.parsers.users.exists(username): return False return True
def unlock(username): # sudo usermod -U <username> # use sudo passwd --status <username> to check the status of the lock # <username> LK 2019-05-30 7 90 7 -1 (Password locked.) shell('sudo usermod -U {}'.format(username)) status = shell('sudo passwd -S {}'.format(username)) if re.findall('\s(L)\s', status): raise RuntimeError('cannot unlock user {}'.format(username)) return api.parsers.users.parse_single(username)
def create(username, password=None, groups=[]): # TODO(everdrone): make this global cwd = '/home/telegram/test/pi-json-api/scripts' env = { 'username': username, } if password: env['password'] = password if groups: env['groups'] = ','.join(groups) print(env) shell('bash adduser.sh', cwd, env) if api.parsers.users.exists(username): return api.parsers.users.parse_single(username) return False
def sessions(): lines = shell('w -h') lines = filter(None, lines.split('\n')) out = [] for line in lines: col = line.split() out.append({ 'user': col[0], 'tty': col[1], 'from': col[2], 'login_time': col[3], 'idle': col[4], 'jcpu': col[5], 'pcpu': col[6] }) return out
def filesystem(): out = {} lines = shell('df -P') lines = filter(None, lines.split('\n')) df_p = {} for line in lines: match = re.match('^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$', line) if match: mg = match.groups() key = '{},{}'.format(mg[0], mg[5]) df_p[key] = {} df_p[key]['device'] = mg[0] df_p[key]['kb_size'] = mg[1] df_p[key]['kb_used'] = mg[2] df_p[key]['kb_available'] = mg[3] df_p[key]['percent_used'] = mg[4] df_p[key]['mount'] = mg[5] # merge into out object out.update(df_p) # filesystem inode data lines = shell('df -iP') lines = filter(None, lines.split('\n')) df_ip = {} for line in lines: match = re.match('^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$', line) if match: mg = match.groups() key = '{},{}'.format(mg[0], mg[5]) df_ip[key] = {} df_ip[key]['device'] = mg[0] df_ip[key]['total_inodes'] = int(mg[1]) df_ip[key]['inodes_used'] = int(mg[2]) df_ip[key]['inodes_available'] = int(mg[3]) df_ip[key]['inodes_percent_used'] = int(mg[4][:-1]) df_ip[key]['mount'] = mg[5] # merge into out obj out.update(df_ip) # grab mount data lines = shell('mount') lines = filter(None, lines.split('\n')) mount = {} for line in lines: match = re.match('^(.+?) on (.+?) type (.+?) \((.+?)\)$', line) if match: mg = match.groups() key = '{},{}'.format(mg[0], mg[1]) mount[key] = {} mount[key]['device'] = mg[0] mount[key]['mount'] = mg[1] mount[key]['fs_type'] = mg[2] mount[key]['mount_options'] = mg[3].split(',') # merge into out out.update(mount) # grab from /proc/mounts lines = api.utils.fs.read_lines('/proc/mounts') # don't merge directly into out # check if the data is missing, if it is, then merge proc = {} for line in lines: match = re.match('^(\S+) (\S+) (\S+) (\S+) \S+ \S+$', line) if match: mg = match.groups() key = '{},{}'.format(mg[0], mg[1]) proc[key] = {} proc[key]['device'] = mg[0] proc[key]['mount'] = mg[1] proc[key]['fs_type'] = mg[2] proc[key]['mount_options'] = mg[3].split(',') # update only if missing if key not in out: out[key] = proc[key] # grab lsblk data lines = shell('lsblk -n -P -o NAME,UUID,LABEL,FSTYPE') lines = filter(None, lines.split('\n')) lsblk = [] for line in lines: match = re.match( 'NAME="(\S+).*?" UUID="(\S*)" LABEL="(\S*)" FSTYPE="(\S*)"', line) if match: mg = match.groups() dev = mg[0] if not dev.startswith('/'): dev = _find_device(dev) uuid = mg[1] label = mg[2] fs_type = mg[3] obj = { 'dev': dev, 'uuid': uuid, 'label': label, 'fs_type': fs_type } lsblk.append(obj) # update out = _update_missing(out, obj) # grab blkid data lines = shell('blkid') lines = filter(None, lines.split('\n')) blkid = [] for line in lines: device_name = line.split(':')[0] obj = {'dev': device_name} parts = line.split()[1:] for part in parts: match = re.match('(\S+)="(\S+)"', part) if match: mg = match.groups() key = mg[0].lower() if key == 'type': key = 'fs_type' obj[key] = mg[1] # update out = _update_missing(out, obj) return out
def create(name): shell('sudo groupadd {}'.format(name)) return api.parsers.groups.parse_single(name)
def delete(name): shell('sudo groupdel {}'.format(name)) if api.parsers.groups.exists(name): return False return True
def lock(username): shell('sudo usermod -L {}'.format(username)) status = shell('sudo passwd -S {}'.format(username)) if not re.findall('\s(L)\s', status): raise RuntimeError('cannot lock user {}'.format(username)) return api.parsers.users.parse_single(username)