简体   繁体   English

将脚本从Linux更改为Python-自动化

[英]Changing Scripts from Linux to Python - Automation

i use the following commands and form a script to automate servers using Linux now i would like to automate it in python,i dont know how to write commands for these as i am new to python. 我现在使用Linux使用以下命令并形成脚本来自动化服务器,现在我想在python中实现自动化,我不知道如何为这些命令编写命令,因为我是python的新手。

    echo "DISK SPACE"
    echo "--------------------- "
    df -kh

    echo "CPU UTILIZATION"
    prstat 1 1

    echo "MEMORY UTILIZATION"
    echo "---------------------    "
    vmstat 

    echo "LINK STATUS"
    echo "---------------------    "
    egrep -i "link||service" logs/septel.log|tail -20

    echo "Restart log"
    echo "---------------------    "
    tail -10  //newlogs/restart.log

    echo "MENU"
    echo "---------------------    "
    echo "0"|/operations/Menu.pl

    dat=`date +%Y%m%d`
    echo "==================================   "
    echo "EOD "
    cat /b-eod/eodclearlog.log

    echo "mart STP"
    echo "---------------------    "
    tail /logs/mart-stp.log

    echo "TNSPING STATUS"
    echo "=========================================   "
    tnsping rwdb

You have different way to solve your problem. 您有不同的方法来解决您的问题。 If you need a quick one, you need to know that any shell entry can be used through python subprocess: 如果需要快速入门,则需要知道可以通过python子进程使用任何shell条目:

from subprocess import call
call(["ls", "-l"])

But if you have time, and you want to power the python API, you should give a look to the OS and SYS libraries: 但是,如果您有时间,并且想增强python API的功能,那么应该看一下OS和SYS库:

https://docs.python.org/2/library/os.html https://docs.python.org/2/library/sys.html https://docs.python.org/2/library/os.html https://docs.python.org/2/library/sys.html

Anything written with these lib should be usable from Unix or windows. 用这些lib编写的所有内容都可以在Unix或Windows上使用。

And to replicate the behaviour or more fancy command, like tail , you will need to look a bit around. 而要复制行为或更复杂的命令,例如tail ,则需要四处看看。 For example this question provide several suggestions: How to implement a pythonic equivalent of tail -F? 例如,该问题提供了一些建议: 如何实现python-tail -F的等效项?

Like using the library tailf . 就像使用库tailf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM