Skip to content

Instantly share code, notes, and snippets.

View nitesh8860's full-sized avatar

Nitesh Chauhan nitesh8860

View GitHub Profile
@nitesh8860
nitesh8860 / telnet_report.py
Last active May 23, 2021 06:22
script tests if there is a connection between source IP, dest IP and dest port. it requires a central server which is able to login to all source servers. (ansible master).
'''
Example format for sdp.csv file.
source,dest,port
10.0.x.x,10.0.x.y,80
10.0.x.x,10.0.x.y,800
'''
import csv
import base64
import paramiko
@nitesh8860
nitesh8860 / random-awscli.sh
Created May 23, 2021 03:59
creating inventory with tag values, filter.json
aws2 ec2 create-tags --resources $i --tags Key=SSM,Value=yes
aws2 ec2 associate-iam-instance-profile --instance-id $i --iam-instance-profile Name=SSMec2-Instance-Profile
aws ec2 associate-iam-instance-profile --instance-id i-0xxxxxxxxxx --iam-instance-profile Name=SSMec2-Instance-Profile
aws iam add-role-to-instance-profile --role-name AmazonEC2RoleforSSM --instance-profile-name SSMec2-Instance-Profile
aws iam remove-role-from-instance-profile --instance-profile-name SSMec2-Instance-Profile --role-name AmazonSSMRoleForInstancesQuickSetup
aws iam add-role-to-instance-profile --role-name AmazonEC2RoleforSSM --instance-profile-name SSMec2-Instance-Profile
aws2 ec2 describe-instances --filters Name=network-interface.addresses.private-ip-address,Values=[$i] \
--query 'Reservations[*].Instances[*].{IP:PrivateIpAddress,Name:Tags[?Key==`Name`]|[0].Value}' \
--output text
@nitesh8860
nitesh8860 / elasticsearch-smtp.py
Last active May 23, 2021 03:41
this usecase is related to app logs where if some message fails trying multiple times, the script will identify that message and then collect all related logs to that error and send it to email.
'''
example data:
name: someText aFewMoreRandomThings aggregate_id: someNumber
if this error is found, the script will search for all related logs to this aggregate_id and the text Retries exceeded (RMQ specific).
script will then collect all this data and send it to email
error type: {}
aggregateid: {}\n
mq deadfinal log: \n{}
related log : \n{}
#to print column number:
STDIN | awk '{print $COLUMN_NUMBER}'
@nitesh8860
nitesh8860 / awscli-deleteLogGroupPatterns.sh
Created May 23, 2021 03:26
deletes log groups in bulk from cloudwatch
export AWS_DEFAULT_REGION=eu-west-1
aws logs describe-log-groups --query 'logGroups[*].logGroupName' --output table | \
awk '{print $2}' | grep ^/aws/rds | while read x; do echo "deleting $x" ; aws logs delete-log-group --log-group-name $x; done
@nitesh8860
nitesh8860 / timeit-list.py
Last active June 15, 2020 05:57
calculating time taken for list growth with different methods using timeit
# List comprehensions are the fastest.
def test1():
l = []
for i in range(1000):
l = l + [i]
def test2():
l = []
for i in range(1000):
from datetime import datetime
t1='Sat 02 May 2015 19:54:36 +0530'
t2='Fri 01 May 2015 13:54:36 -0000'
t1=datetime.strptime(t1,'%a %d %b %Y %H:%M:%S %z')
t2=datetime.strptime(t2,'%a %d %b %Y %H:%M:%S %z')
# print(t1-t2)
#> 1 day, 0:30:00
print ((t1-t2).total_seconds())
@nitesh8860
nitesh8860 / reduce.py
Created June 12, 2020 15:15
using reduce function
from functools import reduce
l=[1,2,3,4]
print(reduce(lambda x,y: x*y, l, -1))
#> -24
#reduce(func with 2 params, iterable, initial value )
def fun(s):
# return True if s is a valid email, else return False
import re
rex = re.compile("^[\w\-\_]+\@[a-zA-Z0-9]+\.\w{1,3}$")
if rex.match(s):
return True
else:
return False
@nitesh8860
nitesh8860 / secondHighest.py
Created June 12, 2020 11:03
getting the second highest number in the list with help of filter in python
max_arr=max(arr)
arr=list(filter(lambda x: x!=max_arr, arr))
print(max(arr))