What are the applications of Python in Linux system operations?

Source: Internet
Author: User
Tags saltstack
Self-taught for a while Python, it may be my learning posture is not quite right. It's always a sense that Python prefers web development over shell and Perl. The operation and management of Linux is weak.
Aside from this kind of Python-developed OPS tools like fabric. What can you do with Python in your daily operations? Where is the convenience or efficiency of a shell script?
Now only Python has written a partial log parsing script and a simple script that extracts the UID generation link from the page and then tests the action such as playback.
Others don't know what to do.
I would like to ask the older generation to give some pointers, if you can have some concrete examples is better.

The same question was also raised on V2ex. http://www. v2ex.com/t/75193

Thank you all.

Reply content:

I prefer to use "system management" to describe the work of "operation and maintenance". OPS sounds a work of partial execution. The advantages of Python in system management are with strong development capabilities and a complete toolchain. Python's engineering development capabilities are strong, far stronger than a variety of shell and Perl. Easy to read and write, both object-oriented and functional style, but also good meta-programming ability. Through a systematic combination of various management tools, the above-mentioned various types of tools to develop two times, to form a unified server management system.
Ruby, similar to Python, is also well-suited for writing system management software, but it is much worse than Python in related libraries and tools.

A chestnut: A cow, proficient in Bash,python,perl,ruby, wrote hundreds of single-function scripts (any language) to perform daily operations, covering monitoring, deployment, network configuration, log analysis, security detection and many other aspects, all-encompassing. What he did was just the operation, and did not put the system management to work well. No more scripts, just the "operation" of this behavior to the extreme. Writing more scripts does not translate into a company's valuable assets in system management.

Making the system easy to manage is a project. Only automated management tools like Puppet (Ruby), Saltstack (python) can fully describe the entire system and bring all aspects of system management into a unified system, rather than a bunch of scripts.
Python is very rich in server management tools, configuration management (Saltstack) batch execution (fabric, saltstack) monitoring (Zenoss, Nagios plug-in) virtualization Management (PYTHON-LIBVIRT) process management ( Supervisor) cloud Computing (OpenStack) ... And most of the system C libraries have python bindings.
For the process to determine the matter, eventually must be included in the system management systems, written as a program, as part of the system. Instead of being able to reuse various scripts that are free and monolithic.
With the advent of the cloud era, small and medium-sized companies do not need to be operational. Large companies, without engineering development capability of operations, is not competitive. Not perl now? Thank you for your invitation. You're right, Python is weaker than Perl and bash for everyday operations. Python is also a lot more troublesome than PHP to do web development. I personally feel that Python is more Java-like and not suitable for comparison with Perl/bash.
My most commonly used operations tools are bash-based, Python is mainly used to do some algorithmic calculus. Because my daily maintenance tasks are based on file system maintenance, the comparison language should be combined with the scene.

Most of the application needs, all languages can be completed, but the degree of difficulty is different, the choice of language depends on the developer's proficiency and the complexity of the requirements.
If it's just a "grep" requirement, it's easiest for you to choose Bash for grep, and if it's a bit more complicated, use awk and then use Perl. In a more general language, it is thankless.
If it is a matrix computing requirement, the Perl array can toss you to death, and Bash's pipeline can kill the machine.

My next point of view is bound to be controversial (especially phper), and I tend to divide high-level languages into three categories:
1) lower-level C + +
It is possible to write the operating system, operating the hardware. Can solve all problems, but most Internet projects should not be used.
2) Neutral java/python/ruby/. NET Series
This is the main language for doing projects, balancing development efficiency and execution efficiency. There are no obvious bright spots and no obvious shortcomings.
3) Php/bash/perl of special features
In some specific areas, these languages should be chosen with special features. As the demand becomes more and more comprehensive, the more the language's short board is used, the more it should control diffusion. If you put aside the big killing device such as fabric and func ... My current feeling is that any one-time script that isn't "burned after reading", especially if you need to modify the adjusted, slightly logical script later, you should use Python instead of Bash. Think "I have a bunch of hosts, I want to separate ..." this array operation. Think about the great bash string substitution. Think about quoting. Bash perfectly inherits Perl's "Write-only" attribute.

Non-ops, a man in his own lab with a stack of small machines passing by.

@ Feng Wei just executing efficiency Bash high? "Software Engineer" in your signature is that a joke?

Python_vs_bash_benchmark.zip


    • The Python program, this example is about 6 times faster than, the BASH program.
    • Python is a little slower in this example as it's only 3 times faster than BASH
    • Python is roughly 5.5 times faster than BASH in this test.
    • With Python lists being times faster and python dictionaries being 23.7 times faster using BASH forany kind of Sequenti Al data manipulation is unacceptable (unless it is very small amounts of data).
    • Since in the calling program Python are 11.77 times faster than bash, bash is thereforerestricted to Linea R program whereas Python are only 48% (verses the 476% of BASH) slower than the non function calling program and for that R Eason is a very good structured programming language (keep Inmind, 10000 function calls were made in both programs).


In fact, there is no need to look at any tests. A large number of operations rely on interprocess communication, there is no JIT and median optimization, support for complex data structures is approximately zero Bash, and Python is performing any comparison of function and efficiency./xxx.py or simply write a C program. Bash's cooked words are good, but the advantage of Python is that there are many web frameworks that visualize the operations process and results
This is not comparable in terms of user friendliness.
How many OPS scripts do you write in one months? Maintain several OPS scripts? These two add up to less than 10, and there's no difference between bash and python.

P.S. Python biased to the web? It's a pleasure. I thought I would say PHP's ... Not PY SA is not qualified SA, once used expect management of 200 servers, now feel too two.

The first two days just put Miller's kernel nic interrupt patch script with less than 10 lines of py fix still salt. The simplicity of the py is visible.

Operation and maintenance automation configuration is still the main py and Ruby if you leave the fabric, even saltstack, ansible this operation tool, it is difficult to demonstrate the power of Python.
You can read the Python UNIX and Linux System Administration Guide Feel it, please.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.