diff --git a/lab-00/file-copy.py b/lab-00/file-copy.py new file mode 100644 index 0000000..ca084da --- /dev/null +++ b/lab-00/file-copy.py @@ -0,0 +1,8 @@ +#!/usr/bin/python + +f = open("/var/log/system.log") +buf = f.read() +print buf +f1 = open("mylogfile.log") +f1.write(buf) +f1.close() \ No newline at end of file diff --git a/lab-01/file_ops.py b/lab-01/file_ops.py index 37c371c..25b1ab1 100644 --- a/lab-01/file_ops.py +++ b/lab-01/file_ops.py @@ -4,15 +4,32 @@ # Open the license file, read the content into the buffer # then close the file descriptor -f = open('conditions.py') -lines10 = f.read(10) -#alllines = f.read() -print (lines10) -f.close() +# f = open('conditions.py') +# f.seek(15) +# lines10 = f.read(5) +# # line11_20 = f.read(10) +# # f.seek(0) +# # lines10 = f.read(10) +# #alllines = f.read() +# print (lines10) +# f.close() # Open the license file, read the content as "lines" into the # buffer and close it. +# f = open('conditions.py') +# file_content = f.read() +# print file_content +# f.close() + f = open('conditions.py') file_content = f.readlines() -f.close() \ No newline at end of file +print file_content +f.close() + +for line in file_content: + if len(line) !=0: + line_list = line.split() + if len(line_list) !=0: + if line_list[0] == 'print': + print line_list diff --git a/lab-01/list-operations.py b/lab-01/list-operations.py index 2b52b84..610d2ae 100644 --- a/lab-01/list-operations.py +++ b/lab-01/list-operations.py @@ -5,12 +5,45 @@ new_list = [] -for l in mylist: - #print l * 100 - new_list.append(l + 100) - -print new_list -print max(new_list) -print min(new_list) -print sum(new_list) +# print mylist[0] +# print mylist[1] +# print mylist[2] + +# for list_item in mylist: +# new_item = list_item * 100 +# print new_item + +new_list = [] + +# new_list.append(100) +# new_list.append(200) +# new_list.append(300) + +items = [100,200,300] + +for item in items: + new_list.append(item) + print new_list + +# print new_list +# for l in mylist: +# #print l * 100 +# new_list.append(l + 100) + +# print new_list +# print max(new_list) +# print min(new_list) +# print sum(new_list) + +my_range = [0, 1, 2, 3] +my_list = [] + + +for item in range(2,20): + new_item = item + 10 + my_list.append(new_item) + print my_list + + + diff --git a/lab-01/strings-operations.py b/lab-01/strings-operations.py index a894c4f..3d0f9a9 100644 --- a/lab-01/strings-operations.py +++ b/lab-01/strings-operations.py @@ -1,15 +1,31 @@ #!/usr/bin/python -my_string = "My string has many spaces let me see how many" +my_string = "My,string,has,many,spaces,let,me,see,how,many" -print my_string.split() +my_string = "My,string,has,many,spaces,let,me,see,how,many" -print "Lets loop through the strings" +my_list = my_string.split(',') -for s in my_string.split(): - print ("String literal ", s) +print my_list -my_string = "My,string,has,many,commas,let,me,see,how,many" +length_list = len(my_list) + +print length_list + +first_item = my_list[0] + +print first_item + +see_item = my_list[7] + +print see_item + +# print "Lets loop through the strings" + +# for s in my_string.split(): +# print ("String literal ", s) + +# my_string = "My,string,has,many,commas,let,me,see,how,many" # Assignment # Count number of , in the string diff --git a/lab-02/func-03.py b/lab-02/func-03.py new file mode 100644 index 0000000..8c6c649 --- /dev/null +++ b/lab-02/func-03.py @@ -0,0 +1,20 @@ + + + +# Check if filename is less < 4 +def myfunc(filename,nlines): + if filename < 4: + return "error" + + with open(filename) as f: + lines = f.readlines() + + my_lines = lines[:nlines] + + with open('myfiles.txt','w') as f: + for line in my_lines: + f.write(line) + +myfunc("functions.py",3) + + diff --git a/lab-02/functions-args.py b/lab-02/functions-args.py index 497c21b..0ae731e 100644 --- a/lab-02/functions-args.py +++ b/lab-02/functions-args.py @@ -1,7 +1,10 @@ -def myfunction(arg1, arg2,arg3): +def myfunction(arg1, arg2,arg3="Chicago"): print "My Very first python function with Arguments" print "{},{},{}".format(arg1, arg2, arg3) + sum = arg1 + arg2 + arg3 + + #return sum if arg3 == "chicago": second_func(arg1,arg2) @@ -9,7 +12,7 @@ def myfunction(arg1, arg2,arg3): second_func(arg1,arg2,arg3) -# Polymorphism in Funtion names +# # Polymorphism in Funtion names def second_func(arg1,arg2,arg3=None): print("Testing optional argument") print (arg1,arg2,arg3) @@ -21,4 +24,5 @@ def third_func(arg1): if __name__ == "__main__": - myfunction("hello","world","chicago") \ No newline at end of file + my_return = myfunction("Hello", "World","Toronto") + print my_return \ No newline at end of file diff --git a/lab-02/functions.py b/lab-02/functions.py index 91b53ee..e27f3a7 100644 --- a/lab-02/functions.py +++ b/lab-02/functions.py @@ -2,8 +2,9 @@ def myfunction(): print "My Very first python function" +myfunction() -if __name__ == "__main__": - myfunction() +# if __name__ == "__main__": +# myfunction() \ No newline at end of file diff --git a/lab-04/json-parsing.py b/lab-04/json-parsing.py index f0cd39c..92c6e24 100644 --- a/lab-04/json-parsing.py +++ b/lab-04/json-parsing.py @@ -4,6 +4,15 @@ content = f.read() j = json.loads(content) +# print j['changed'] +# print j['instance_ids'] +#print j['instances'][0] +#print json.dumps(j,sort_keys=True,indent=4) -print json.dumps(j,sort_keys=True,indent=4) +for k,v in j.items(): + if k == 'instances': + for instance in v: + for k,v in instance.items(): + if k == "dns_name": + print v diff --git a/lab-05/simple_os.py b/lab-05/simple_os.py new file mode 100644 index 0000000..c878b4d --- /dev/null +++ b/lab-05/simple_os.py @@ -0,0 +1,13 @@ + +import multiprocessing +import os + + + +def print_os_info(): + print multiprocessing.cpu_count() + print os.uname()[0] + print os.system("df -h") + print os.system("uptime") + +print_os_info() \ No newline at end of file diff --git a/lab-06/requests-example.py b/lab-06/requests-example.py index f69a749..cacb98a 100644 --- a/lab-06/requests-example.py +++ b/lab-06/requests-example.py @@ -1,13 +1,28 @@ import requests + +def count_yelp(text): + all_words = text.split() + yelp_count = all_words.count('Yelp') + return yelp_count + +def replace_yelp(text): + return text.replace('yelp','google') + + r = requests.get('http://wwww.yelp.com') if r.status_code == 200: - print r.content + print count_yelp(r.text) + #print replace_yelp(r.text) + # Assignment -# Load the yelp page and replace all occurance of yelp to google +# Load the yelp page count number of types yelp word is there and +# replace all occurance of yelp to google + + # Load the file from http://censusdata.ire.org/36/all_050_in_36.P2.csv and find # the max P002001 diff --git a/lab-07/ec-create-instance.py b/lab-07/ec-create-instance.py index 733e299..e398336 100644 --- a/lab-07/ec-create-instance.py +++ b/lab-07/ec-create-instance.py @@ -6,11 +6,12 @@ def create_instance(): instances = ec2_resource.create_instances(ImageId='ami-49f0762d', MinCount=1, MaxCount=1,InstanceType='t2.micro', SecurityGroupIds=['ansible-node'],KeyName='ansible') + instance_ids = [] for instance in instances: - print instance + instance_ids.append(instance.id) ec2_client = boto3.client('ec2') waiter=ec2_client.get_waiter('instance_running') - waiter.wait(InstanceIds=[instances[0].id]) + waiter.wait(InstanceIds=instance_ids) print ("Instance is Running now!") create_instance() diff --git a/lab-07/ec2-client-list-instances.py b/lab-07/ec2-client-list-instances.py index a07c88e..18c54ae 100644 --- a/lab-07/ec2-client-list-instances.py +++ b/lab-07/ec2-client-list-instances.py @@ -2,4 +2,13 @@ ec2_client = boto3.client('ec2') response = ec2_client.describe_instances() -print(response) \ No newline at end of file +#print(response) + +for k,v in response.items(): + if k == 'Reservations': + for instance in v: + for i,vv in instance.items(): + if i == 'Instances': + for ii in vv: + print ii['PublicDnsName'] + diff --git a/lab-07/ec2-list-volumes.py b/lab-07/ec2-list-volumes.py new file mode 100644 index 0000000..9c75948 --- /dev/null +++ b/lab-07/ec2-list-volumes.py @@ -0,0 +1,13 @@ +import boto3 + +def list_volumes(): + ec2_resource = boto3.resource('ec2') + + instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + for instance in instances: + print(instance.state) + for item in instance.volumes.all(): + print item.id + +list_volumes() \ No newline at end of file diff --git a/lab-07/ec2-resource-list-instances.py b/lab-07/ec2-resource-list-instances.py index 5e20645..fb4640e 100644 --- a/lab-07/ec2-resource-list-instances.py +++ b/lab-07/ec2-resource-list-instances.py @@ -6,9 +6,8 @@ instances = ec2_resource.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) for instance in instances: - print(instance.state) - for item in instance.volumes.all(): - print item.id + print(instance.id,instance.public_dns_name) + # Mac installation tip: diff --git a/lab-07/ec2-start-stop.py b/lab-07/ec2-start-stop.py new file mode 100644 index 0000000..bc2ecb7 --- /dev/null +++ b/lab-07/ec2-start-stop.py @@ -0,0 +1,41 @@ +import sys +import boto3 +from botocore.exceptions import ClientError + + +instance_id = sys.argv[2] +action = sys.argv[1] + +ec2 = boto3.client('ec2') + + +if action == 'start': + # Do a dryrun first to verify permissions + try: + ec2.start_instances(InstanceIds=[instance_id], DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + # Dry run succeeded, run start_instances without dryrun + try: + response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False) + print(response) + except ClientError as e: + print(e) +elif action == 'stop': + # Do a dryrun first to verify permissions + try: + ec2.stop_instances(InstanceIds=[instance_id], DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + # Dry run succeeded, call stop_instances without dryrun + try: + response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False) + print(response) + except ClientError as e: + print(e) +else: + print( "Operation not supported") \ No newline at end of file diff --git a/lab-07/ec2-terminate-instances.py b/lab-07/ec2-terminate-instances.py new file mode 100644 index 0000000..65358d3 --- /dev/null +++ b/lab-07/ec2-terminate-instances.py @@ -0,0 +1,30 @@ +import boto3 +from botocore.exceptions import ClientError + + +def prepare_instance_list(): + mylist = [] + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + for instance in instances: + print(instance.id, instance.instance_type) + mylist.append(instance.id) + return mylist + +def delete_instances(instance_list): + ec2_client = boto3.client('ec2') + try: + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=False) + +instance_list = prepare_instance_list() +if len(instance_list) !=0: + print ( "Deleting Instances", instance_list) + delete_instances(instance_list) +else: + print ( "No Instances to delete ") \ No newline at end of file diff --git a/lab-07/lambda-rules.json b/lab-07/lambda-rules.json new file mode 100644 index 0000000..48c3a10 --- /dev/null +++ b/lab-07/lambda-rules.json @@ -0,0 +1,32 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Resource": "arn:aws:logs:*:*:*" + }, + { + "Effect": "Allow", + "Action": "ec2:Describe*", + "Resource": "*" + }, + { + "Effect": "Allow", + "Action": [ + "ec2:CreateSnapshot", + "ec2:DeleteSnapshot", + "ec2:CreateTags", + "ec2:ModifySnapshotAttribute", + "ec2:ResetSnapshotAttribute" + ], + "Resource": [ + "*" + ] + } + ] +} diff --git a/lab-07/list-and-snapshot.py b/lab-07/list-and-snapshot.py new file mode 100644 index 0000000..57ad336 --- /dev/null +++ b/lab-07/list-and-snapshot.py @@ -0,0 +1,17 @@ +import boto3 + +def list_volumes(): + ec2_resource = boto3.resource('ec2') + + instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + for instance in instances: + print(instance.state) + for item in instance.volumes.all(): + print item.id + snapshot = ec2_resource.create_snapshot(VolumeId=item.id, Description="Taking backup") + +list_volumes() + + +