Skip to content

Instantly share code, notes, and snippets.

@gmastrokostas
gmastrokostas / system_info.py
Created November 25, 2015 09:30
Python – psutil module – Gather system info with dynamically generated histograms
#!/usr/bin/python
import psutil
def Virtual_memory_usage():
#VIRTUAL MEMORY INFORMATION
virtu_full_num = psutil.virtual_memory().percent
virtnum_int = int(float(psutil.virtual_memory().percent))
print "VRT MEM USAGE: ", virtu_full_num
#print "0----------------100%" ##from initial version of script
@gmastrokostas
gmastrokostas / maxclient.py
Created November 25, 2015 09:30
Python – Find RAM usage for highest HTTP process and calculate MAXClients setting.
#!/usr/bin/python
import os
import psutil
import subprocess
import time
'''
if os.exists("/etc/redhat-release"):
process = "httpd"
else:
@gmastrokostas
gmastrokostas / find_banned_users.py
Created November 25, 2015 09:29
Find logged in black listed users
import psutil
#Generate a list of logged users.
users = psutil.users()
#list of banned users
banned_users = ['root','gmastrokostas', 'test', 'test1', 'test3']
#empty list to insert the logged users.
logged_users = []
#Will used to insert the results between the banned_users and logged_users.
#This will distinguish users who are in the banned list Vs those who are not.
banned_logged_users = []
@gmastrokostas
gmastrokostas / search_text.py
Created November 25, 2015 09:27
Python – Searching for text in multiple files within a directory OR one file in a directory
src_dict = ("/etc/yum.repos.d/") #Specify base directory
pattern = re.compile ('http\S+') #CPatter to search for
for yum_files in os.listdir(src_dict): # obtain list of files in directory
files = os.path.join(src_dict, yum_files) #join the full path with the names of the files.
strng = open(files) #We need to open the files
for lines in strng.readlines(): #We then need to read the files
if re.search(pattern, lines): #If we find the pattern we are looking for
print re.split(r'=', lines)[1] #We split using as a delimeter the = sign.
#INSERT WHATEVER CODE YOU WANT
@gmastrokostas
gmastrokostas / smrt_file_renamer.py
Created November 25, 2015 09:26
Python – Change one specific extension type of files in a directory that contains multiple types of extensions
import os
import re
src_drct='/home/gmastrokostas/tmp'
for files in os.listdir(src_drct):
if files.endswith('.txt'):
oldF = os.path.join(src_drct, files)
#midF = re.split(r'\.', files)#This works too.
@gmastrokostas
gmastrokostas / file_extension_renamer.py
Created November 25, 2015 09:25
Python – Search for specific types of files and rename the files or part of the files
import os
import re
src_drct='/home/gmastrokostas/tmp'
for files in os.listdir(src_drct):
if files.endswith('.txt'): #Select all files with the .txt ext
oldF = os.path.join(src_drct, files) #Join full path with files found
midF = re.split(r'\_', files) #split files that contain underscore
@gmastrokostas
gmastrokostas / check_IP_DNS_SSH.py
Created November 25, 2015 09:25
Python: Check IPs for DNS entries and see if host is UP or DOWN, check SSH login. Export info to a CSV file
import socket
import subprocess
import netifaces
import csv
import paramiko
def checkPING(IP):
try:
ping = subprocess.check_output(['ping', '-c1', ip])
return "Host is UP"
@gmastrokostas
gmastrokostas / directory_size.py
Created November 25, 2015 09:24
Python – Get a listing of all subdirectories and their size on a Linux system
#!/usr/bin/python
import humanize
import os, sys
from os.path import join,getsize
import humanize
import pwd
def dir_list():
list = []
drct = raw_input(":Enter directory name. Use full path: ")
@gmastrokostas
gmastrokostas / proc_finder.py
Created November 25, 2015 09:23
Python – Get list of processes, their owners and RAM usage from a Linux system.
import os, sys
from os.path import join,getsize
import humanize
import pwd
import psutil
pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()]
for elements in pids:
p = psutil.Process(elements)
proc_name = p.name()
@gmastrokostas
gmastrokostas / file_age.py
Created November 25, 2015 09:23
Python – Compare modification dates between files and find time difference
#!/usr/bin/python
#The script searches in a specific location for files and it gets in POSIX the time of modification of all files.
#It also creates a temp file (and write text in it) within the same directory (which gets deleted once the script exits)
#This temp file is used to get today's modification date. Then all POSIX dates are converted into human readable format
#and a comparison is done between the temp file and the files we are examining. If the files we are examining are
#three months or older, then .... you can enter what ever custom action you want.
import os.path
import tempfile
import datetime