What scripting language?
I am learning my way around administering a "standard" LAMP server. There are some things I want to do in an automated fashion but am not sure what is available normally in terms of programming languages. I want to write a script that automates some or all of these tedious steps after I create a new account:
- Copies a set of files from one user to another
- Changes ownership
- Changes permissions on certain folders/files
- Finds certain files and then search and replaces either certain lines or parts of lines.
- Copies a database in the mysql server
- Search and replaces certain strings in all fields in all tables in the new database
- Makes a certain user so that they have access to the new database
- Creates a database user
- Grants them all privileges for the new database
If I could create a script that I could open, change certain variable values, upload, execute as root from a terminal and then delete the script that would be fine.
What programing language should I look at?
I'm sorry nobody has replied too you before now.
As for the tasks you want to do there are literally 1000 scripting languages which can do what you want. Some of the ones you will find best suited are
Perl - it was essentially designed for these kinds of tasks and has the advantage that most of what you are asking there are already perl scripts out on the web you can copy and modify slightly to do exactly what you want to do. Perl is very commonly used and there is good support for it.
Python - is a bit more friendlier than PERL. There is also quite a few scripts/examples of what you want to do on the web using Python scripts.
Open Rexx - I like it, though not anywhere near as commonly used as Python or PERL. Pretty easy to use and the Rexx language was designed for sys admin tasks on IBM mainframes so it's well suited to sys admin tasks. Most of what you want to do will have easily found example code.
Bash - I saw nothing in what you wanted to do that could not be done in Bash scripts. Slightly more complex than using a true programming language but a bit easier to maintain. Like the others I mentioned there will be quite a few good examples for what you want to do.
Try all 4 methods and one will eventually turn out to be the language you turn too most often for sys admin scripts. The syntax of all 4 is reasonably similar so most of what you learn on one will be a good bit similar to what you use for the others.
Originally Posted by flycast
Hey...thanks for the reply. I am using bash currently. For the exporting of the database I run a php script. For creating a new database (not written yet) I will run a mysql script. It's a little bit hacked in that I am using a bash script to do most of it and then call a couple of other scripts but it seems to be working.
It beats doing this by hand using FTP and gui. The whole script takes less than a second vs. 1.5 hours doing everything by hand!
It would be nice if I could do all cleanly this with one language.
You can in the first 3 options. Python, Perl and Open Rexx all have database drivers for MySQL. In Bash you can script database work by piping SQL to the mysql command. PHP is also capable of all the tasks you are asking, just not optimal as you create potential security risks when you give PHP scripts shell privileges.
Writing MySQL Scripts with Python DB-API
DatabaseInterfaces - PythonInfo Wiki
Those are 2 of the many ways to talk to MySQL from Python.
Database references using Perl.
Perl DBI - dbi.perl.org
Database Programming with Perl - Perl.com
Access your MySQL Database with Perl
REXX MySQL Library | Download REXX MySQL Library software for free at SourceForge.net
Or you can just pipe too mysql from Rexx
Example of creating a table using Python
# Open database connection
db = MySQLdb.connect("localhost","testuser","test123"," TESTDB" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Drop table if it already exist using execute() method.
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
# Create table as per requirement
sql = """CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
INCOME FLOAT )"""
# disconnect from server
I didn't test it but syntax looks good at a glance. Just swiped it from the web. I almost never write Perl or Python code from scratch. No point. Just Google what your trying to do, grab the example that most closely matches your requirements, modify it and away you go. From there just create functions for commonly used tasks and you can write quite a bit of code in a short time with very little work. Most of the debugging was done before you swiped the code. Only thing you have to debug is your modifications as often as not.
I spent Saturday rewriting what I did with bash to python to learn python. I like python so far. I really like the way that it pays attention to indenting on the statements. I have installed the MySQLdb module but have not used it yet. Thanks for the example.
Where is the best, most visited forum to get python help?
Python is my favorite scripting language. It's powerful, easy to use, lots of good examples abound on the web.
Another language you might enjoy, though it's a bit of overkill for scripting is Gambas.
Gambas - Gambas Almost Means Basic
Great language for more complex tasks.
As for a specific web site, I don't really have one to recommend. Generally what I do is look for examples of what I'm trying to do with Google, find the best match for what I'm already doing and work from there. Python has a decent but not great online syntax reference
Which I often use as a starting point for searching for Python examples. I figure out what it is I want to do, search on the keyword for examples using that keyword then write my code or copy and paste if it's really close to what I'm trying to do.
There are some good developer forums out there which have multi-language support and some good books you can buy on Python as well as free Python programming books you can download.