Browse Source

Adds test and final script

Jose Reyes 6 years ago
parent
commit
565dfcf53c
2 changed files with 96 additions and 0 deletions
  1. 29
    0
      emailTest.py
  2. 67
    0
      importUsers.py

+ 29
- 0
emailTest.py View File

@@ -0,0 +1,29 @@
1
+import smtplib
2
+import sys
3
+
4
+gmail_user = 'jose.reyes46@upr.edu'  
5
+gmail_password = sys.argv[1]
6
+
7
+sent_from = gmail_user  
8
+to = "emmanuel.nieves@upr.edu"
9
+subject = 'Usuario y password para ADA'  
10
+body = 'Hey, what\'s up?\n\n- You'
11
+
12
+email_text = """
13
+From: %s  
14
+To: %s  
15
+Subject: %s
16
+
17
+%s
18
+""" % (sent_from, to, subject, body)
19
+
20
+try:  
21
+    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
22
+    server.ehlo()
23
+    server.login(gmail_user, gmail_password)
24
+    server.sendmail(sent_from, to, email_text)
25
+    server.close()
26
+
27
+    print 'Email sent!'
28
+except:  
29
+    print 'Something went wrong...'

+ 67
- 0
importUsers.py View File

@@ -0,0 +1,67 @@
1
+import os
2
+import sys
3
+
4
+EXPIRE="2018/12/31" # YY/MM/DD
5
+
6
+if len(sys.argv) < 2:
7
+	print "Usage: %s /path/to/moodle/user/file/ email_pass" % sys.argv[0]
8
+	sys.exit(0)
9
+
10
+users = open(sys.argv[1]).readlines()
11
+
12
+def buildPassword():
13
+        """Build a random password"""
14
+        import string
15
+        from random import choice
16
+        characters = string.ascii_letters  + string.digits
17
+        password =  "".join(choice(characters) for x in range(10))
18
+        return password
19
+
20
+def sendEmail(user, email, passwd):
21
+	import smtplib
22
+
23
+	gmail_user = 'jose.reyes46@upr.edu'  
24
+	gmail_password = sys.argv[2]
25
+
26
+	sent_from = gmail_user  
27
+	to = email
28
+	subject = 'Usuario y password para ADA'  
29
+	body = """Le he creado una cuenta en ada.uprrp.edu para su clase de CCOM3030.
30
+El username es %s y su password es %s.\n
31
+Si ya tenia una cuenta, esta ha sido reactivada. Ignoren el password aqui mencionado, deben dar login con el que usaban. Si lo han olvidado me escriben para cambiarlo.\n
32
+Para dar login al servidor, deberan usar el comando ssh en su terminal:
33
+ssh %s@ada.uprrp.edu
34
+
35
+Deberan escribir yes cuando les pida confiar en el servidor, y luego entrar su password. Inmediatamente despues de acceder les debe pedir que cambien el password.\n
36
+Cualquier duda que tengan me esciben.\n
37
+Jose Reyes""" % (user, passwd, user)
38
+
39
+	email_text = """From: %s\nTo: %s\nSubject: %s\n\n%s
40
+	""" % (sent_from, to, subject, body)
41
+
42
+	try:  
43
+	    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
44
+	    server.ehlo()
45
+	    server.login(gmail_user, gmail_password)
46
+	    server.sendmail(sent_from, to, email_text)
47
+	    server.close()
48
+
49
+	    print 'Email sent!'
50
+	except:  
51
+	    print 'Something went wrong...'
52
+
53
+for user in users:
54
+	fields = user.split(",")
55
+	email = fields[2].split("\n")[0]
56
+	user = email.split("@")[0]
57
+	passwd = buildPassword()
58
+
59
+	if os.path.exists("/home/estudiantes/%s" % user):
60
+		os.system("chage -E %s %s" % (EXPIRE, user))
61
+		print "%s expiration day changed" % user
62
+	else:	
63
+		os.system("useradd -d /home/estudiantes/%s -m %s -e %s -s /bin/bash" % (user, user, EXPIRE))
64
+		os.system("echo %s:%s | chpasswd" % (user, passwd))
65
+		os.system("chage -d 0 %s" % user)
66
+		print "%s user created" % user
67
+	sendEmail(user,email,passwd)