Menu

[Solved]-Trouble Assignment Honestly Idea Start Professor Included Samba Configuration File Really Q37245117

I am having trouble with this assignment I honestly have no ideawhere to start. My professor included the samba configuration filebut I really have no idea what to do with it. I am also using thevisual studio editor for python.

Scenarios are quite common where an IT professional is taskedwith modifying a file on many computer systems or many files on asingle computer or a combination of the two. For example, a largesoftware may need the copyright notice comment to be changed onevery source file in a project. Or, a configuration file may needto be modified on every server in a multi-server deployment. Doingthis task manually is not a viable way to approach the problem.Instead, a better solution is to use a scriptable environment toperform search and replace functionality on the files in question.Python is an excellent tool for this kind of task.

Description

Samba is a unix/linux file sharing suite of programs designed toprovide Windows interoperable file and print sharing services. Muchof the configuration for the Samba daemon is provided by the textfile smb.conf (renamed to smb.txt for this assignment). After anupdate was deployed to three dozen linux servers it was discoveredthat 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 aviable solution. To fix the problem, the IT department must connectto each server, edit the smb.conf file, and restart the smbddaemon. To automate this task, each server will have a Pythonscript copied to it. Then an SSH session will be initiated to eachserver from which the script will be executed. Your task is towrite 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 anywhite space in quotes.
  • “to” is the text the searched text is to be replaced with.

Testing the script is your responsibility. However a good testcase is:

python modify.py smb.txt “password sync = yes” “password sync =no”

Hints

Accessing command line arguments in Python requires the sysmodule. Consequently, your script must have the “import sys”directive. To access individual arguments, use sys.argv[x] where xis the argument number. I.e. the first argument would be accessedby sys.argv[1]. Note that sys.argv[0] is the name of the script andnot used in this assignment.

Specifying command line arguments from an IDE differs dependingon the IDE. For PyCharm CE, select Run -> Edit Configurationsand place them on the Parameters field. For example:

Also, recognize that you will open two files, one for readingand one for writing. I highly recommend that you append a “.new” tothe file to be written while debugging. Otherwise, you will corruptyour input file while testing.

How do you open a file in Python for reading or writing? It isvery easy, to open for reading:

inPointer = open(fileSpec, ‘r’)

where “fileSpec” is the filename and ‘r’ tells Python that youwant to read the file. The variable inFile is sometimes called a“pointer” to the file. Essentially it is a reference that allowsone 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’ tellsPython 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 forall available methods but the following will read the next line ofa file into a string.

line = inFile.readline()      # readsthe next line of text into a string

Once you read the contents, you need to search for occurrencesof “from” and replace them with “to”. How? Strings have a replacemethod such that you can loop through the lines and do a search andreplace for each occurrence. Next write the modified line to a newfile.

Once you get to the end of the file, close both the read fileand the write file and you’re done!

Just to get you started, the following takes the arguments fromthe 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 tosearch for

replaceText = sys.argv[3]     # text toreplace with

outFile = fileSpec + ‘.new’   # write file = read filewith .new appended

outPointer = open(outFile,’w’)      #open write file

inPointer = open(fileSpec, ‘r’)     # openread file

Expert Answer


Answer to I am having trouble with this assignment I honestly have no idea where to start. My professor included the samba configu… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *