forked from arytmw/Python-Sys-Admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import json | ||
|
||
def write_json(data, filename = 'example_2.json'): | ||
with open(filename, 'w') as f: | ||
json.dump(data, f, indent=4) | ||
|
||
with open('example_2.json') as json_file: | ||
data = json.load(json_file) | ||
|
||
temp = ['quiz'] | ||
|
||
y = { 'sale_country': 'India', | ||
'currency': 'INR', | ||
'type': 'fruit' | ||
} | ||
|
||
temp.append(y) | ||
|
||
write_json(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import requests | ||
|
||
url = "https://ia803208.us.archive.org/6/items/j.-r.-r.-tolkien-la-comunidad-del-anillo-i/J.R.R.%20Tolkien%20La%20Comunidad%20del%20anillo%20I.pdf" | ||
|
||
res = requests.get(url, stream = True) | ||
|
||
with open('lotr.pdf','wb') as myfile: | ||
for chunk in res.iter_content(chunk_size=1024): | ||
if chunk: | ||
myfile.write(chunk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import requests | ||
|
||
url = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png" | ||
|
||
res = requests.get(url) | ||
with open('python_logo.png','wb') as myfile: | ||
myfile.write(res.content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import json | ||
|
||
new_data = {"e":5} | ||
|
||
with open("sample.json","r+") as myfile: | ||
data = json.load(myfile) | ||
data.update(new_data) | ||
myfile.seek(0) | ||
json.dump(data,myfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import json | ||
|
||
new_data = {"name":"waseem"} | ||
|
||
with open('sample.json') as myfile: | ||
data = json.load(myfile) | ||
|
||
data.update(new_data) | ||
|
||
with open("sample.json","w") as myfile: | ||
json.dump(data,myfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import tarfile | ||
|
||
filename = 'sample.tar.gz' | ||
|
||
tf = tarfile.open(filename) | ||
tf.extractall() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
from fabric.api import * #applicaation programming interface | ||
|
||
# uname -r | ||
|
||
env.user = 'python' | ||
env.hosts = 'vsftpd' | ||
|
||
def check_kernel(): | ||
run('uname -r') | ||
|
||
def update_all(): | ||
sudo('yum update -y') | ||
|
||
def result(): | ||
sudo('ls -l /var/www/') | ||
|
||
def dir_create(): | ||
local('mkdir /tmp/fabricdir') | ||
|
||
env.roledefs = { | ||
'webservers': ['localhost','10.0.0.8'] | ||
} | ||
|
||
@roles ('webservers') | ||
def check_uptime(): | ||
run('uptime') | ||
|
||
def copy(): | ||
put('/etc/hosts','/home/python/hostsfab', mode=777) | ||
|
||
def port_number(): | ||
prompt('Which port?', default=80, validate=int) | ||
|
||
def reboot(): | ||
reboot(wait=30) | ||
|
||
def download(): | ||
get('/etc/passwd','/tmp/passwdvsftpd') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import glob | ||
|
||
print("Name of a file") | ||
for name in glob.glob('/etc/p*'): | ||
print(name) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import glob | ||
|
||
print("using glob.glob") | ||
files = glob.glob('/etc/**/*', recursive=True) | ||
|
||
for file in files: | ||
print(file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3.6 | ||
import os | ||
from datetime import datetime | ||
|
||
lastmodified = os.stat('myfile.txt').st_mtime | ||
|
||
print(datetime.fromtimestamp(lastmodified)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import pymysql | ||
import json | ||
|
||
file = 'example.json' | ||
|
||
json_data = open(file).read() | ||
json_obj = json.load(json_data) | ||
|
||
def validate_string(val): | ||
if val != None: | ||
if type(val) is int: | ||
return str(val).encode('utf-8') | ||
else: | ||
return val | ||
|
||
con = pymysql.connect(host = 'hostname', user = 'username', password = 'password', db = 'dbname') | ||
con_cur = con.cursor() | ||
|
||
#person, year and client | ||
|
||
for i, item in enumerate(json_obj): | ||
person = validate_string(item.get("person", None)) | ||
year = validate_string(item.get("year", None)) | ||
client = validate_string(item.get("client", None)) | ||
|
||
cursor.execute("INSERT INTO tbname (person, year, client)) VALUES (%s, %s, %s)".(person, year, client)) | ||
|
||
con.commit() | ||
con.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import subprocess | ||
import boto3 | ||
|
||
#mysqldump -h db@192.168.0.1 -u username -p password | ||
|
||
username = 'username' | ||
password = 'password' | ||
database = 'database_url' | ||
db_name = 'db@192.168.0.1' | ||
|
||
with open('mybackup.sql,'w') as myfile: | ||
c = subprocess.Popen(['mysqldumb','-h', db_name, '-u', username, '-p',password], | ||
stdout=myfile, shell=True) | ||
|
||
with open('backup.sql','w') as output: | ||
command = subprocess.Popen(['mysqldump','-u', username, '-p',password, database], | ||
stdout=output, shell=True) | ||
|
||
s3 = boto3.client('s3') | ||
with open('backup.sql', 'rb') as f: | ||
s3.upload_fileobj(f, 'bucket_name', 's3backup.sql') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import json | ||
|
||
with open('example_2.json', 'r') as openfile: | ||
myjson = json.load(openfile) | ||
|
||
print(myjson) | ||
#print(type(json_obj)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import csv | ||
|
||
with open('data.csv', 'r') as myfile: | ||
csvfile = csv.reader(myfile) | ||
|
||
for lines in csvfile: | ||
print(lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import pandas | ||
|
||
csvfile = pandas.read_csv('data.csv') | ||
print(csvfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import csv | ||
|
||
with open('data.csv', 'r') as myfile: | ||
csvfile = csv.DictReader(myfile) | ||
|
||
for lines in csvfile: | ||
print(lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import tarfile | ||
|
||
filename = 'sample.tar.gz' | ||
|
||
tf = tarfile.open(filename) | ||
print(tf.getnames()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
from zipfile import ZipFile | ||
import datetime | ||
|
||
filename = "sample.zip" | ||
|
||
with ZipFile(filename, 'r') as zip: | ||
for info in zip.infolist(): | ||
print(info.filename) | ||
print('\tModified:\t' + str(datetime.datetime(*info.date_time))) | ||
print('\tSystem:\t\t' + str(info.create_system) + '(0 = Windows, 3 = Linux)') | ||
print('\tZip version:\t' + str(info.create_version)) | ||
print('\tCompressed:\t' + str(info.compress_size) + 'bytes') | ||
print('\tUncompressed:\t' + str(info.file_size) + 'bytes' ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#!/usr/bin/env python3.6 | ||
|
||
import psutil | ||
import platform | ||
from datetime import datetime | ||
|
||
|
||
# 1253656 --> 1.20MB | ||
# 1253656678 ---> 1.17 GB | ||
def get_size(bytes, suffix="B"): | ||
factor = 1024 | ||
for unit in ["","K","M","G","T","P"]: | ||
if bytes < factor: | ||
return f"{bytes:2f}{unit}{suffix}" | ||
bytes /= factor | ||
|
||
print("="*40, "System Information", "="*40) | ||
|
||
uname = platform.uname() | ||
print(f"System: {uname.system}") | ||
print(f"Name: {uname.node}") | ||
print(f"Release: {uname.release}") | ||
print(f"Version: {uname.version}") | ||
print(f"Machine: {uname.machine}") | ||
print(f"Processor: {uname.processor}") | ||
#print(f"System: {platform.uname().system}") | ||
|
||
print("="*40, "Boot Time", "="*40) | ||
boot_time_timestamp = psutil.boot_time() | ||
bt = datetime.fromtimestamp(boot_time_timestamp) | ||
|
||
print(f"Boot time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}") | ||
|
||
print("="*40, "CPU Info", "="*40) | ||
print("Physical cores:", psutil.cpu_count()) | ||
|
||
cpufreq = psutil.cpu_freq() | ||
print(f"Current Frequency: {cpufreq.current} Mhz") | ||
#print(f"Maximum Frequency: {cpufreq.max:.2f} Mhz") | ||
|
||
print("="*40, "Memory Information", "="*40) | ||
sysmem = psutil.virtual_memory() | ||
print(f"Total Memory: {get_size(sysmem.total)}") | ||
print(f"Available Memory: {get_size(sysmem.available)}") | ||
print(f"Used Memory: {get_size(sysmem.used)}") | ||
print(f"Percentage: {sysmem.percent}%") | ||
swap = psutil.swap_memory() | ||
print(f"Total Swap: {get_size(swap.total)}") | ||
print(f"Free Swap: {get_size(swap.free)}") | ||
print(f"Used Swap: {get_size(swap.used)}") | ||
print(f"Percentage: {swap.percent}%") | ||
|
||
print("="*40, "Disk Information", "="*40) | ||
part = psutil.disk_partitions() | ||
#partition_usage = psutil.disk_usage(partition.mountpoint) | ||
for partition in part: | ||
print(f"Device: {partition.device}") | ||
print(f"MountPoint: {partition.mountpoint}") | ||
print(f"File System Type: {partition.fstype}") | ||
try: | ||
partition_usage = psutil.disk_usage(partition.mountpoint) | ||
except PermissionError: | ||
continue | ||
print(f"Total Size: {get_size(partition_usage.total)}") | ||
print(f"Used Size: {get_size(partition_usage.used)}") | ||
print(f"Free Size: {get_size(partition_usage.free)}") | ||
print(f"Percentage: {get_size(partition_usage.percent)}%") | ||
|
||
print("="*40, "Network Information", "="*40) | ||
|
||
if_addrs = psutil.net_if_addrs() | ||
|
||
for interface_name, interface_addresses in if_addrs.items(): | ||
for address in interface_addresses: | ||
print(f"Interface: {interface_name}") | ||
if str(address.family) == 'AddressFamily.AF_INET': | ||
print(f"IP Address: {address.address}") | ||
print(f"Netmask: {address.netmask}") | ||
print(f"Broadcast IP: {address.broadcast}") | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|