CSIS 375
File Manipulation with Python Assignment
3 years ago
10
FileManipulationwithPythonAssignmentInstructions.docx
smbd.txt
FileManipulationwithPythonAssignmentInstructions.docx
CSIS 354
File Manipulation with Python Assignment Instructions
Overview
Scenarios are quite common where an IT professional is tasked with modifying a file on many computer systems or many files on a single computer or a combination of the two. For example, a large software may need the copyright notice comment to be changed on every source file in a project. Or, a configuration file may need to be modified on every server in a multi-server deployment. Doing this task manually is not a viable way to approach the problem. Instead, a better solution is to use a scriptable environment to perform search and replace functionality on the files in question. Python is an excellent tool for this kind of task.
Instructions
Samba is a unix/linux file sharing suite of programs designed to provide Windows interoperable file and print sharing services. Much of the configuration for the Samba daemon is provided by the text file smb.conf (renamed to smb.txt for this assignment). After an update was deployed to three dozen linux servers it was discovered that a couple of configured parameters were incorrect. Unfortunately, since there are unique aspects to each server, copying the contents of a single file to each server is not a viable solution. To fix the problem, the IT department must connect to each server, edit the smb.conf file, and restart the smbd daemon. To automate this task, each server will have a Python script copied to it. Then an SSH session will be initiated to each server from which the script will be executed. Your task is to write the Python script.
The invocation of the script must be as follows:
python modify.py fileSpec “from” “to”
where,
· modify.py is the name of your script
· fileSpec is the name of the file to be modified (smb.txt)
· “from” is the text to be searched for (be sure to enclose any white space in quotes)
· “to” is the text the searched text is to be replaced with.
Testing the script is your responsibility. However a good test case is:
python modify.py smb.txt “password sync = yes” “password sync = no”
Hints
Accessing command line arguments in Python requires the sys module. Consequently, your script must have the “import sys” directive. To access individual arguments, use sys.argv[x] where x is the argument number. I.e. the first argument would be accessed by sys.argv[1]. Note that sys.argv[0] is the name of the script and not used in this assignment.
Specifying command line arguments from an IDE differs depending on the IDE. For PyCharm CE, select Run -> Edit Configurations and place them on the Parameters field. For example:
Also, recognize that you will open two files, one for reading and one for writing. I highly recommend that you append a “.new” to the file to be written while debugging. Otherwise, you will corrupt your input file while testing.
How do you open a file in Python for reading or writing? It is very easy, to open for reading:
inPointer = open(fileSpec, 'r')
where “fileSpec” is the filename and ‘r’ tells Python that you want to read the file. The variable inFile is sometimes called a “pointer” to the file. Essentially it is a reference that allows one to operate on the file.
Similarly, to open a file for writing:
outPointer = open(fileSpec,'w')
where fileSpec is the same as the reading example and ‘w’ tells Python the file is for writing. Just as inPointer, outPointer is a “pointer” to the opened file to write.
Now what? To read from the file you can read one line at a time, all the lines, or the entire file into a string. See python.org for all available methods but the following will read the next line of a file into a string.
line = inFile.readline() # reads the next line of text into a string
Once you read the contents, you need to search for occurrences of “from” and replace them with “to”. How? Strings have a replace method such that you can loop through the lines and do a search and replace for each occurrence. Next write the modified line to a new file.
Once you get to the end of the file, close both the read file and the write file and you’re done!
Just to get you started, the following takes the arguments from the command line and opens the input file and an output file:
import sys
fileSpec = sys.argv[1] # read file name first argument
searchText = sys.argv[2] # text to search for
replaceText = sys.argv[3] # text to replace with
outFile = fileSpec + '.new' # write file = read file with .new appended
outPointer = open(outFile,'w') # open write file
inPointer = open(fileSpec, 'r') # open read file
Test your code completely. At each step of the test take a screen shot and embed it into a Word document. Submit the Word document to the appropriate assignment.
Submit your Python source files and any other relevant material.
Page 3 of 3
image1.png
smbd.txt
# This is the main Samba configuration file. You should read the # smb.conf(5) manual page in order to understand the options listed # here. Samba has a huge number of configurable options most of which # are not shown in this example # # Some options that are often worth tuning have been included as # commented-out examples in this file. # - When such options are commented with ";", the proposed setting # differs from the default Samba behaviour # - When commented with "#", the proposed setting is the default # behaviour of Samba but the option is considered important # enough to be mentioned here # # NOTE: Whenever you modify this file you should run the command # "testparm" to check that you have not made any basic syntactic # errors. #======================= Global Settings ======================= [global] ## Browsing/Identification ### # Change this to the workgroup/NT-domain name your Samba server will part of workgroup = WORKGROUP # server string is the equivalent of the NT Description field server string = Toaster Data security = user map to guest = Bad User # This will prevent nmbd to search for NetBIOS names through DNS. dns proxy = no #### Networking #### # The specific set of interfaces / networks to bind to # This can be either the interface name or an IP address/netmask; # interface names are normally preferred # interfaces = 0.0.0.0/32 # Only bind to the named interfaces and/or networks; you must use the # 'interfaces' option above to use this. # It is recommended that you enable this feature if your Samba machine is # not protected by a firewall or is a firewall itself. However, this # option cannot handle dynamic or non-broadcast interfaces correctly. bind interfaces only = yes #### Debugging/Accounting #### # This tells Samba to use a separate log file for each machine # that connects log file = /var/log/samba/log.%m # Cap the size of the individual log files (in KiB). max log size = 1000 ####### Authentication ####### # Server role. Defines in which mode Samba will operate. Possible # values are "standalone server", "member server", "classic primary # domain controller", "classic backup domain controller", "active # directory domain controller". # # Most people will want "standalone sever" or "member server". # Running as "active directory domain controller" will require first # running "samba-tool domain provision" to wipe databases and create a # new domain. server role = standalone server # If you are using encrypted passwords, Samba will need to know what # password database type you are using. passdb backend = tdbsam obey pam restrictions = yes # This boolean parameter controls whether Samba attempts to sync the Unix # password with the SMB password when the encrypted SMB password in the # passdb is changed. unix password sync = yes # For Unix password sync to work on a Debian GNU/Linux system, the following # parameters must be set (thanks to Ian Kahan <<[email protected]> for # sending the correct chat script for the passwd program in Debian Sarge). passwd program = /usr/bin/passwd %u passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* . # This boolean controls whether PAM will be used for password changes # when requested by an SMB client instead of the program listed in # 'passwd program'. The default is 'no'. pam password change = yes ########## Domains ########### # # The following settings only takes effect if 'server role = primary # classic domain controller', 'server role = backup domain controller' # or 'domain logons' is set # # It specifies the location of the user's # profile directory from the client point of view) The following # required a [profiles] share to be setup on the samba server (see # below) ; logon path = \\%N\profiles\%U # Another common choice is storing the profile in the user's home directory # (this is Samba's default) # logon path = \\%N\%U\profile # The following setting only takes effect if 'domain logons' is set # It specifies the location of a user's home directory (from the client # point of view) ; logon drive = H: # logon home = \\%N\%U # The following setting only takes effect if 'domain logons' is set # It specifies the script to run during logon. The script must be stored # in the [netlogon] share # NOTE: Must be store in 'DOS' file format convention ; logon script = logon.cmd # This allows Unix users to be created on the domain controller via the SAMR # RPC pipe. The example command creates a user account with a disabled Unix # password; please adapt to your needs ; add user script = /usr/sbin/adduser --quiet --disabled-password --gecos "" %u # This allows machine accounts to be created on the domain controller via the # SAMR RPC pipe. # The following assumes a "machines" group exists on the system ; add machine script = /usr/sbin/useradd -g machines -c "%u machine account" -d /var/lib/samba -s /bin/false %u # This allows Unix groups to be created on the domain controller via the SAMR # RPC pipe. ; add group script = /usr/sbin/addgroup --force-badname %g ############ Misc ############ # Using the following line enables you to customise your configuration # on a per machine basis. The %m gets replaced with the netbios name # of the machine that is connecting ; include = /home/samba/etc/smb.conf.%m # Some defaults for winbind (make sure you're not using the ranges # for something else.) ; idmap uid = 10000-20000 ; idmap gid = 10000-20000 ; template shell = /bin/bash # Setup usershare options to enable non-root users to share folders # with the net usershare command. # Maximum number of usershare. 0 (default) means that usershare is disabled. ; usershare max shares = 100 # Allow users who've been granted usershare privileges to create # public shares, not just authenticated ones # usershare allow guests = yes #======================= Share Definitions ======================= # Un-comment the following (and tweak the other settings below to suit) # to enable the default home directory shares. This will share each # user's home directory as \\server\username ;[homes] ; comment = Home Directories ; browseable = no # By default, the home directories are exported read-only. Change the # next parameter to 'no' if you want to be able to write to them. ; read only = yes # File creation mask is set to 0700 for security reasons. If you want to # create files with group=rw permissions, set next parameter to 0775. ; create mask = 0700 # Directory creation mask is set to 0700 for security reasons. If you want to # create dirs. with group=rw permissions, set next parameter to 0775. ; directory mask = 0700 # By default, \\server\username shares can be connected to by anyone # with access to the samba server. # Un-comment the following parameter to make sure that only "username" # can connect to \\server\username # This might need tweaking when using external authentication schemes ; valid users = %S # Un-comment the following and create the netlogon directory for Domain Logons # (you need to configure Samba to act as a domain controller too.) ;[netlogon] ; comment = Network Logon Service ; path = /home/samba/netlogon ; guest ok = yes ; read only = yes # Un-comment the following and create the profiles directory to store # users profiles (see the "logon path" option above) # (you need to configure Samba to act as a domain controller too.) # The path below should be writable by all users so that their # profile directory may be created the first time they log on ;[profiles] ; comment = Users profiles ; path = /home/samba/profiles ; guest ok = no ; browseable = no ; create mask = 0600 ; directory mask = 0700 ;[printers] ; comment = All Printers ; browseable = no ; path = /var/spool/samba ; printable = yes ; guest ok = no ; read only = yes ; create mask = 0700 # Windows clients look for this share name as a source of downloadable # printer drivers ;[print$] ; comment = Printer Drivers ; path = /var/lib/samba/printers ; browseable = yes ; read only = yes ; guest ok = no # Uncomment to allow remote administration of Windows print drivers. # You may need to replace 'lpadmin' with the name of the group your # admin users are members of. # Please note that you also need to set appropriate Unix permissions # to the drivers directory for these users to have write rights in it ; write list = root, @lpadmin [workdir] path = /workdir public = yes case sensitive = yes map archive = no only guest = yes writable = yes force user = smbuser force group = smbuser
- Edu 3103 Week 1 Disscussion
- week 3 essays
- QNT 351 Week 4, Learning Team Reflection
- ANT101_Wk5_D2
- FOR HIFSA ONLY
- gscm
- BCG Matrix Research a company of your choice and determine which of the four quadrants of the BCG Matrix you feel it fits into. Justify your response using information about the company. Your paper should be a minimum of 500 words. The requirements belo
- answer 6 questions
- Econ Discussion Question
- Walt Disney company questions