blank_program's Profile User Rating: -----

Reputation: 11 Tradesman
Group:
Contributors
Active Posts:
282 (0.15 per day)
Joined:
22-July 09
Profile Views:
8,030
Last Active:
User is offline Jun 02 2014 06:59 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Pontiac
Dream Kudos:
100

Latest Visitors

Icon   blank_program has not set their status

Posts I've Made

  1. In Topic: Code check/help

    Posted 2 Jun 2014

    Does this look any better? Email functionality based on Python documentation added but not tested. commented out for testing. Tested on Debian and seems to work, except email code I did not add, but I know I need error handling.
    #!/usr/bin/python3
    
    from email.mime.text import MIMEText
    import pwd
    import random
    import smtplib
    import string
    import subprocess as sp
    
    class User:
    	def __init__(self, name):
    		self.fullname = name
    		self.username = ''
    		self.password = ''
    		self.email_addr = ''
    		
    	def chuserid(self):
    		#attempt to check if user name already exists by checking /etc/passwd for username
    		#just continue if user does not exist as this is non-fatal
    		userchk = ''
    		try:
    			userchk = pwd.getpwnam(self.username).pw_name
    		except:
    			pass
    		
    		#if user exists then simply reset the password for the specified user
    		#create user if they do not exist
    		if userchk == self.username:
    			print("User found. Setting new password.")
    			self.setpassword()
    		else:
    			print("User not found. Creating account.")
    			self.createuser()
    			
    	def createuser(self):
    		#create user and set comment field to full name for documentation purposes
    		sp.check_call(["/usr/sbin/useradd", "-c" + self.fullname, "-m", self.username.lower()])
    		print(self.username.lower() + " added successfully")
    		
    		self.setpassword()
    		
    	#function that accepts a users full name and builds a username and email address
    	def geninfo(self):
    		#split full name to form email address and username
    		#user name should be first initial then full last name
    		splname = self.fullname.split(' ')
    		
    		#input should be at least first and last name but may include middle initial to prevent duplication
    		if len(splname) == 2:
    			#email address is first.last@example.com
    			self.email_addr = splname[0] + "." + splname[1] + "@example.com"
    			self.email_addr = self.email_addr.lower()
    			
    			#username is first initial followed by last name
    			self.username = splname[0][0] + splname[1]
    		elif len(splname) == 3:
    			#placeholder address until standard address format is set for name collisions
    			self.email_addr = "@example.com"
    			
    			#DO NOT USE - disabled until standard is set, left in as template for future use
    			#append middle initial to end of first name of format first.last@example.com
    			#self.email_addr = splname[0] + "." + splname[1][0] + "." + splname[2] + "@example.com"
    			
    			self.email_addr = self.email_addr.lower()
    			
    			#username is first initial followed by last name
    			self.username = splname[0][0] + splname[1][0] + splname[2]
    		#if name is not first and last or first middle last then prompt again
    		else:
    			geninfo()
    		
    		#force userid to lowercase due to case sensitivity
    		self.username = self.username.lower()
    		
    		#check if userid currently exists
    		self.chuserid()
    		
    	#set password for specified user account using psuedo-random number appended to userid
    	#NOT SECURE but password set for ease of use by end user
    	def setpassword(self):	
    		#generate random password for user based on userid and random set of digits
    		#NOT SECURE but designed for ease of use by end user
    		self.password = self.username + '.' + ''.join(random.choice(string.digits) for _ in range(5))
    		
    		#temporarily print password for testing purposes
    		print(self.password)
    		
    		#hash password and assign it to new user account using built in functions
    		passin = sp.Popen(("mkpasswd", "-m", "sha-512", self.password), stdout=sp.PIPE)
    		hpass = passin.communicate(self.password.encode())[0].strip()
    		hpass = hpass.decode()
    		sp.check_call(["usermod", "-p", str(hpass), self.username])
    		
    		self.emailnotice()
    
    	#email user from template notifying them of the set login credentials
    	def emailnotice(self):
    			notice_template = "/root/notice.txt"
    	
    			addr_from = ''
    			addr_to	= self.email_addr
    			
    			msg_body = ''
    			msg_subject = ''
    			
    			smtp_user = "example@example.com"
    			smtp_password = ''
    			smtp_server = ''
    			
    			#read SMTP password from file with restricted access
    			with open("/root/secret.txt", 'r') as sf:
    				smtp_password = sf.read()
    			
    			#read notice message from file with restricted access
    			with open(notice_template, 'r') as nf:
    				msg_body = nf.read()
    			
    			#replace template variable text with information specific to user
    			msg_body = msg_body.replace('ppppp', self.password)
    			msg_body = msg_body.replace('uuuuu', self.username)
    			
    			#build email message
    			notice = MIMEText(msg_body)
    			notice['From'] = addr_from
    			notice['Subject'] = msg_subject
    			notice['To'] = addr_to
    			
    			print(addr_to)
    			#s = smtplib.SMTP(smtp_server)
    			#s.login(smtp_user, smtp_pass)
    			#s.send_message(notice)
    			#s.quit()
    		
    def main():
    	inname = input("Enter user's fullname: ")
    	user = User(inname)
    	user.geninfo()
    
    if __name__ == "__main__":
    	main()
    
  2. In Topic: Reuse panel question

    Posted 5 Feb 2014

    I would like to use one window to encompass controls for various options. Rather than have tons of windows I just want to alter my single window.
  3. In Topic: What version control system to use

    Posted 13 Apr 2013

    I have decided to use Mercurial but now it is a matter of finding out where I want to host my project. Either Google Code or Sourceforge.
  4. In Topic: How to reprint a line in the console

    Posted 26 Mar 2013

    View PosttlhIn`toq, on 26 March 2013 - 05:27 PM, said:

    Please show your entire loop code


    Here you go. My class is pretty big and I would rather nto be judged on poor code but this is the only place that calls ReportProgress. This is a full console application only no real GUI.
            internal void HashData(string HashType = "md5")
            {
                if (FullFileList != null)
                {
                    for (int i = 0; i < FullFileList.Count; i++)
                    {
                        if (i == 0 && HashType.Length > 0)
                        {
                            DataLogger.WriteApplicationLog("\nHashing data as " + HashType.ToUpper());
                        }
                        else if (i == 0 && HashType.Length == 0)
                        {
                            DataLogger.WriteApplicationLog("Hashing data as MD5");
                        }
    
                        if (File.Exists(FullFileList[i].FullName))
                        {
                            switch (HashType)
                            {
                                case "md5":
                                    Hasher.HashFileMD5(FullFileList[i].FullName);
                                    break;
                                case "sha1":
                                    Hasher.HashFileSHA1(FullFileList[i].FullName);
                                    break;
                                case "sha256":
                                    Hasher.HashFileSHA256(FullFileList[i].FullName);
                                    break;
                                case "sha512":
                                    Hasher.HashFileSHA512(FullFileList[i].FullName);
                                    break;
                                default:
                                    Hasher.HashFileMD5(FullFileList[i].FullName);
                                    break;
                            }
    
                            if (i != 0)
                            {
                                ReportProgress(i + 1, FullFileList[i].FullName);
                            }
                            else
                            {
                                Console.WriteLine();
                                ReportProgress(i, FullFileList[i].FullName);
                            }
                        }
                    }
    
                    DataLogger.WriteApplicationLog("Data hashing complete.");
                }
            }
    
  5. In Topic: How to reprint a line in the console

    Posted 26 Mar 2013

    Example output:
    Progress 99.00%     Files remaining: 100
    
    


    But then once it decrements files remaining by 1 you then get:
    Progress 99.00%     Files remaining: 990
    
    

My Information

Member Title:
D.I.C Regular
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private
Website URL:
Website URL  http://