Skip to content

Instantly share code, notes, and snippets.

@sumitsaiwal
Created November 17, 2017 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumitsaiwal/d299874f99df19d9959ec7e917b6fbae to your computer and use it in GitHub Desktop.
Save sumitsaiwal/d299874f99df19d9959ec7e917b6fbae to your computer and use it in GitHub Desktop.
import boto
import boto.iam #**************Importing the Identity and access management****
import win32com.client as win32 #******Importing windows application module***
import random
import string
import time
#******************for SMTP integration**************************
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#******************************Variable Declaration******************************
Location= 'ap-south-1'
aws_access_key_id='AKIAI7PY7MMGYCBRPM7Q'
aws_secret_access_key='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'
User_names=[]
print('Enter the User_name')
u=input()
while len(u)==0:
print('Enter a valid user name')
u = input()
User_names.append(str(u))
print('Enter the Email ID')
Email_id=input()
while len(Email_id)==0:
print('Enter a valid Email_id')
Email_id = input()
print('Choose the Product\n1\n2\n3\n4\n5')
Product=input()
prod_list=[str(1),str(2),str(3),str(4),str(5)]
while Product not in prod_list:
print('Enter a valid Product')
Product = input()
print('Choose the Business_Unit\n1\n2\n3\n4\n5')
Business_Unit=input()
buss_unit_list=[str(1),str(2),str(3),str(4),str(5)]
while Business_Unit not in buss_unit_list:
print('Choose a valid Business_Unit')
Business_Unit = input()
group_name="GRP"+"_"+Business_Unit+"_"+Product
usr_lst=[]
grp_lst=[]
#*****************************Connecting to AWS account**************************
conn=boto.iam.connect_to_region(Location, aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
#***Looping the create_user function and calling it on different User_names******
a=conn.get_all_users()
b=conn.get_all_groups()
#***Collecting List of available users
for user in a.list_users_result.users:
usr_lst.append(str(user.user_name))
#***Collecting List of available groups
for grp in b.list_groups_result.groups:
grp_lst.append(str(grp.group_name))
try:
for i in User_names:
if i in usr_lst:
print('The user name ' + i + ' already exists.The user names are case sensitive.')
else:
if group_name not in grp_lst:
conn.create_group(group_name)
conn.create_user(i)
conn.add_user_to_group(group_name, i)
print ("The user is created and assigned to a group")
#password generation
password = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for i in range(8))
conn.create_login_profile(i,password)
#time
t = time.localtime()
try:
#email integration
sender = "admin@amazon.com" #Sender's valid email
msg = MIMEMultipart('alternative')
msg['Subject'] = "AWS user account details"
msg['From'] = sender
msg['To'] = Email_id
#mail.To =Email_id
#mail.Subject = 'AWS user account details'
#mail.body = 'Message body'
html = """\
<html>
<head></head>
<body>
<p>Requested by %s <br>
Requested at : %s <br>
A user account %s has been created <br />
The credentials for the AWS account are: <br />
Username: %s <br />
Passowrd: %s <br />
Console login link: https://455237796341.signin.aws.amazon.com/console'
</p>
</body>
</html>
""" %(Email_id, str(time.asctime(t)), i, i, password)
#mail.HTMLBody ='Requested by ' +Email_id + '<br /> Requested at : ' + str(time.asctime(t)) +' <br /> A user account '+ i + ' has been created <br /> The credentials for the AWS account are: <br /> Username: ' + i +'<br /> Passowrd: '+ password + '<br /> Console login link: https://455237796341.signin.aws.amazon.com/console'
#mail.send
part = MIMEText(html, 'html')
msg.attach(part)
server = smtplib.SMTP('smtp3.hp.com', 25)
server.sendmail(sender, Email_id, msg.as_string())
server.quit()
print("Process completted successfully.")
except Exception as er:
print('The Email ID is Invalid')
except Exception as e:
print(e.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment