Created
August 27, 2014 07:25
-
-
Save dangtrinhnt/bc9646883c7be0ad699f to your computer and use it in GitHub Desktop.
massreset.php python wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| import subprocess | |
| import csv | |
| PHP_SCRIPT_DIR = 'massreset.php' | |
| CSV_DIR = 'accounts.csv' | |
| def get_data_from_csv(csv_file_path): | |
| csv_file = open(csv_file_path, 'rb') | |
| csv_file.seek(0) | |
| sniffdialect = csv.Sniffer().sniff(csv_file.read(10000), delimiters='\t,;') | |
| csv_file.seek(0) | |
| dict_reader = csv.DictReader(csv_file, dialect=sniffdialect) | |
| csv_file.seek(0) | |
| dict_data = [] | |
| for record in dict_reader: | |
| dict_data.append(record) | |
| csv_file.close() | |
| return dict_data | |
| def reset_one(username): | |
| command = 'php ' + PHP_SCRIPT_DIR + ' ' + username | |
| proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | |
| result = proc.communicate()[0] | |
| print "... --> PHP script output:\n %s" % result | |
| if not ('error' in result.lower()): | |
| return True | |
| return False | |
| def reset_all(): | |
| data = get_data_from_csv(CSV_DIR) | |
| for student in data: | |
| print "===> Reset password of %s (%s)\n" % (student['username'], student['student_number']) | |
| result = reset_one(student['username'].strip()) | |
| print "---> Result: %s\n" % result | |
| if __name__ == "__main__": | |
| reset_all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment