Posts

Showing posts from 2018

Add hosts entry using python

#Task : To add hosts entry to a server hosts file using python script. # To whomsoever wondering why, this is a task which can be easily achieved via shell script, but I went forward with Python to learn python. #Script to change the remote IP across the hosts file. #How to run this script : python scriptname IP_that_we_need_to_set_hosts_entry #Importing system library for passing arguments during runtime to incorporate as a jenkins job. import sys #The function add_remote add the remote entry to the hosts file and shows us the new entry that we added. def add_remote():  with open("/etc/hosts", "a") as myfile:   myfile.write( str(entered_IP) + ' remote_server_hostname' + '\n' )  with open("/etc/hosts", "r") as myfile:   for line in myfile.readlines():    if 'remote_server_hostname' in line:     print(line)              #The function fun checks if the hosts entry for remote is added in the server. [Main function] def fu

Shell script to add a public key to server and provide sudo privillege for that user.

#!/bin/bash #function which contains the ssh public key for user admin to add in the servers and another function to check if key already exists #If you want to add your user key, replace the admin with your username and add your ssh key to the section shown below. ################################################################## admin_keyexist_check() { if grep -q "******ssh key goes here******admin@<serverhostname>" /home/admin/.ssh/authorized_keys; then     echo " The ssh key already exists and is shown below...!!"        grep -i "admin" /home/admin/.ssh/authorized_keys     echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"     echo "Removing this shell script now...!!"     rm -fv $0     exit else     echo " The ssh key doesn't exists...!! We will add it...!!"     echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" fi } ####

EC2 instance listing using python boto3

#Purpose : To list all the AWS EC2 instances in the specified region. #The script assume that you have a working aws cli configured with privilege to access the aws ec2 instances. #!/usr/bin/env python import boto3 import sys import time region_name = raw_input('Enter aws region for which you want to get the details of the EC2 Instance [sample : us-east-1]: ') print (region_name, type(region_name)) time.sleep(5) ec2 = boto3.resource('ec2', globals()['region_name']) for instance in ec2.instances.all():     print instance.id, instance.state, instance.private_ip_address, instance.public_ip_address, instance.tags

AWS Command line Tasks : Create EC2 instance and EC2 instance with 500GB root volume.

To create an EC2 instance from command line. aws ec2 run-instances  --image-id ami-efe09bf8 --count 1 --instance-type m4.4xlarge --key-name mysshkey --security-group-ids sg-126adasd2--subnet-id subnet-148fd971 --block-device-mapping /dev/sda1=:500:false --region us-east-1 --image-id : The AMI ID which we are going to create the instance. --count : The number of instances that we need to create. --instance-type : The type of EC2 instance that we are going to use it. --key-name : The pem that we are going to use to connect to this server. --secrutiy group : The security group this instance is getting used. --subnet-id : The subnet in which we want the ec2 instance. --block device mapping - device:space in gb : to terminate volume [true or false.] To create an EC2 instance with 500GB root volume. aws ec2 run-instances --image-id ami-efe09bf8 --block-device-mappings  '[{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":500,"DeleteO

AWS command line tasks - Create 500 GB EBS Volume, to attach and detach the volume

To create a volume of 500 GB   aws ec2 --region us-east-1 create-volume --size 500 --availability-zone us-east-1d --volume-type gp2 size : in GB availability zone : This needs to be in the same availability zone as that of the instance that we are going to attach the EBS volume. To attach an EBS volume to the ec2 instance. aws  ec2 attach-volume --volume-id vol-9dfjhsdkfj93 --instance-id i-1234567899oasd  --device /dev/xvdb --region us-east-1 After attaching format the drive and mount it like what we do for a normal hard disk drive. To detach an EBS volume from the ec2 instance. aws ec2 detach-volume --volume-id vol-9dfjhsdkfj93 --region us-east-1

AWS Command line Tasks - Create SSH key pair, security group, Allow connection to a port

We need a working aws cli configured to use the following commands. To create an SSH pem file. aws ec2 create-key-pair --region us-east-1 --key-name mysshkey --output text > mysshkey.pem  region :  The region in which the ssh pem file is getting created. Here we are saving the pem file in our linux machine as the name mysshkey.pem  Modify the permission of the pem file. chmod 400 mysshkey.pem  To create a security group. aws ec2 create-security-group --region us-east-1 --group-name work_project --description "Project_for_work" --vpc-id vpc-12345  group name - It is the name that we are going to give for the security group description - The description that we are going to give for the security group To allow connection to a port aws ec2 --region us-east-1 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --port 22 --cidr 10.100.0.0/16 aws ec2 --region us-east-1 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --p