That’s indubitably a molecule of authority, but the adroitness of it is emit dealing with the hide, so it’s in actuality not profoundly Byzantine. This program starts up with a menu oblation four choices:
Add a drug account
Restart the Apache spider’s web server
Display around disk rank
Exit.
It doesn’t signification if you don’t victual Apache installed – this is well-deserved to evince how you can swell the program into a more protean config gizmo.
The inception note lets us regulate the program without having to individualize the Python interpreter collateralize to clap (python usermaster.py). Then we victual two raunchy lines: the inception gives us access to Python’s method use authority, which lets us bump overcome exterior programs (ie binaries you’d discover in /usr/bin etc), while the another rolls in curses as described ahead of. More on that in a shimmy.
Next we victual two functions.
The inception takes a prevail upon line parameter, and then asks the drug in gain a line ahead of returning it resources.
The another function, for the shimmy, executes a use authority on the method. Basically, in human-speak, it says, “Tell me what preposterous to disconcert on the hide, then I’ll become aware of some hornbook from the drug and send it resources.” We permission this function later in the authority to become aware of dope in gain the useradd use authority.
It takes a line parameter, clears the hide, and then executes the use authority in this note:
a = system(cmd_string)
Now, what’s that a doing there? Well, we longing to identify whether the use authority was executed successfully. When we permission Python’s method unvaried to regulate a program, it returns 0 if the program ran successfully, or another loads if it encountered an error. Later on, you may swell UserMaster to do other tasks, some of which may not construction any well-defined achieve (eg event a cron job), but you’ll until now identify if it worked or messed up. Thanks to this, we can disconcert out revealed a message to exhibit whether the use authority worked – in gain illustration, whether the user-adding answer succeeded or failed.
So, those two functions keep the foundations in gain our OS.
Python command start executing the program at the x = 0 note, which well-deserved sets up a changing in gain the next note, which checks to know if x contains the native to 4. We shelling up ncurses, decamp the hide, invariable a form and then disconcert out some options strings to the hide. Well, at this the opera house, we haven’t got any input from the drug as yet – but we command in a doubtless steps. Then we victual:
x = hide.getch()
The drug has a enter of commands to come to a judgement from; whatever he/she enters is stored in the x changing. So, the following if lines position b analyse to know what’s in x: if it’s pick, we’re in user-adding mode; if it’s 2, we restart Apache; 3, and we exhibit disk space; 4, and we cut revealed. As mentioned earlier, authority blocks in Python are notable with lappet indents, so after our program has done checking in gain the 3 important to be pressed, it goes resources to the start of the noose – the while note.
But grab b hold-up on a instant, where are we checking in gain 4 there? Well, we’re doing that at the start of the noose, in this note:
while x initially!= ord(‘4′):
If the drug enters anything other than 1, 2 or 3, foremost jumps resources to the while note, which then stops executing the authority nonplus. It does this because the following (and final) note, the endwin(), is not indented and wherefore not business of the while noose authority nonplus. So the exhibit up of the indents shows where the authority nonplus finishes.
Depending on what the drug entered, we regulate a specified program using our execute_cmd() function.
No buts, lone ifs
Let’s look at those if statements. For options 2 and 3, we absolutely disable curses (so that it doesn’t chime in with our program output), and then regulate the program.
execute_cmd() tells us if the program executed correctly, ahead of handing foremost resources to the crucial authority – so we know the menu again. Here, we permission our funky get_param() function to coruscate a series of prompts, getting input from the drug each outdated.
But it’s the inception election that’s the most exciting. We entreaty the drug in gain an account hero, non-payment fork out and so forth; nothing too taxing.
Then we combine all these bits of info together and dine them to the system’s regulatory useradd use authority, with bootee command-line parameters (for benchmark -d in gain conversant with directory).
Grab usermaster.py, procreate it to your conversant with directory, then shelling up a depot and divert to encourage (sudo bash in Ubuntu or su in others). Hence this is why the execute_cmd() note here is profoundly desire – it joins together all the strings and flags that the use authority needs in edict to stir. Enter:
chmod +x usermaster.py
mv usermaster.py /usr/sbin/usermaster
Now usermaster is installed into the method avenue (for administrator commands), so you can regulate it absolutely collateralize to entering usermaster. Note that you desperate straits to regulate it as encourage granted, as general users can’t clap down other drug accounts! You can in a minute in a while scrutinize creating a drug via election 1 of the program – don’t distress thither location up the stringent nobility groups in a minute in a while.
When you’ve created a drug via our gizmo, you’ll also desperate straits to invariable up a password as follows:
passwd nate
Replace nate with the username that you created.
The finished offshoot: a lowly Python Ncurses menu method that automates sysadmin commands. To victual the authority listing scanty, we haven’t included this ladies’ chamber in UserMaster, but it’s an representation inception mini-project in gain you: adding a password setting/changing ladies’ chamber! You’d well-deserved desperate straits to associate with another election to the menu, become aware of the account hero via a get_param() phone, then bump overcome passwd on that hero.
The stringent details of users and groups and how they are allied varies indubitably distinctly from linux deployment to deployment, so you’ll longing to fashion UserMaster in gain your own needs.
Cultivating curses
In this tutorial we’re using some of curses’s most blatant routines – clearing the hide, layout a form, getting input etc. As your general drug account, proffer groups in a depot to know which groups a regulatory drug belongs to, and also know the useradd guide coating (man useradd) to learn more thither the options we main to the use authority. But it’s a masses more gifted than that, providing a superior invariable of window exceed brass routines.
Yes, it seems unsuccessfully to victual the concept of windows at a hornbook depot, but it lets you procreate and execute panels (eg preposterous dialogs) while keeping the hornbook guerrillas unimpaired. If you longing to do more with curses, your best clothes shudder is the Linux Documentation Project’s NCURSES Programming How To – it was to begin with written with C coders in be safe, but you’ll discover divers hinder of the until now and all function calls in the Python bindings. You can perpendicularly interface with the mouse in X terminals!
Annoyingly, documentation in gain the Python curses bindings is degree indistinct at present; the documented Python documentation initially lone has a scanty chapter on the library and there’s not much else doing the rounds.