Skip to content

Instantly share code, notes, and snippets.

@gmastrokostas
gmastrokostas / IP_geoip_location.py
Last active November 25, 2015 09:22
The script collects the IP addresses from the apache log file. It then uses the geoip2 database in order to find the geographical location of the IP. More information for the geoip2 database can be found at http://dev.maxmind.com/geoip/geoip2/downloadable/ The module used to capture the IPs from the apache log file requires a CustomLog format. I…
#The script collects the IP addresses from the apache log file.
#It then uses the geoip2 database in order to find the geographical location of the IP.
#More information for the geoip2 database can be found at http://dev.maxmind.com/geoip/geoip2/downloadable/
#The module used to capture the IPs from the apache log file requires a CustomLog format. It needs to be specified in the apache config file and in the script. The string used is
#("%h <<%P>> %t %Dus \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %l %u"
import geoip2.database
import apache_log_parser
#specify the log file we will capture the IP from.
@gmastrokostas
gmastrokostas / get_http.py
Created November 25, 2015 09:21
This script generates simple GET requests from a web server. Threading is used in order to generate multiple GET requests at the same time. Note that this script cannot create significant stress on a Web Server.
import requests
import threading
def get_thrasher():
req_get = requests.get('http://192.168.56.101/')
threads = []
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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 = []