def installed(package): """Confirm that a package is installed""" # TODO: Make this work with apt/dpkg try: return grepc(execute("yum list installed %s" % package), package) >0 except Exception, _e: return False
def running(processname): """Look for processname in the list of running processes""" try: return int(execute("ps wax | grep -v grep | grep -c '%s'" % (processname))) > 0 except Exception, e: # Pipeline exits nonzero if it doesn't find process, so return False # instead of letting the exception take out the test. return False
def members(groupname): """Return the members of group groupname""" try: members = execute("getent group %s" % groupname).split(":")[3].strip().split(",") if members == [""]: return [] else: return members except: return []
def userCrontask(user, crontask): """Check that user has a specific entry in their crontab.""" try: raw = execute("sudo crontab -u %s -l" % (user)) except: return False if raw.find(crontask) >= 0: return True else: return False
def hasRunLevel(serviceName, runLevel): """Parse chkconfig output and return True if serviceName is set to run at that runLevel""" try: raw = execute("chkconfig --list %s" % (serviceName)).split() except: # chkconfig will exit non-zero if there are no entries for serviceName return False for stanza in raw[1:]: level = int(stanza[:1]) status = stanza[2:] if runLevel == level and status == "on": return True return False
def shell(username): """Return username's shell""" try: return execute("getent passwd %s" % (username)).split(':')[6] except: return None
def home(username): """Return username's home directory""" try: return execute("getent passwd %s" % (username)).split(':')[5] except: return None
def gecos(username): """Return the gecos info for username""" try: return execute("getent passwd %s" % (username)).split(':')[4] except: return None
def gid(username): """Return the gid for username""" try: return int(execute("getent passwd %s" % (username)).split(':')[3]) except: return -1
def exists(username): """Check if username exists""" try: return int(execute("getent passwd %s | wc -l" % (username))) > 0 except: return False
def gid(groupname): """Return the gid associated with group groupname""" try: return execute("getent group %s" % (groupname)).split(":")[2] except: return None
def exists(groupname): """Return True if groupname exists on the system""" try: return int(execute("getent group %s | wc -l" % (groupname))) > 0 except: return False
def stdoutContains(command, required): """Run command, check for string required in the output""" try: return execute(command).find(required) >= 0 except: return False