用python执行Linux命令

例1:在python中包装ls命令

#!/usr/bin/env python
#python wapper for the ls command
import subprocess
subprocess.call(["ls","-l"])

在Linux中执行该命令

[[email protected] python]# python ls.py
total 8
-rwxrwxrwx 1 root root 415 Mar 18 11:40 a.py
-rw-r--r-- 1 root root 103 May 4      22:17 ls.py

下面扩展一下

[[email protected] python]# python ls.py
#!/usr/bin/env python
#python wapper for the ls command
import subprocess
subprocess.call(["cat","/python/ls.py"])
以上是执行Linux下的命令和参数。非常不错的一个例子,好好记住吧

下面一个例子是打印系统信息和磁盘信息的:

#!/usr/bin/env python
#A System Information Gathering Script
import subprocess
#command 1
uname = "uname"
uname_arg = "-a"
print "Gathering system information with %s command:\n" % uname
subprocess.call([uname,uname_arg])
#command 2
diskspace = "df"
diskspace_arg = "-h"
print "Gathering diskspace information %s command:\n" % diskspace
subprocess.call([diskspace,diskspace_arg])

下面是我本地执行打印的数据

[[email protected] python]# python info.py
Gathering system information with uname command:
Linux pydb 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU /Linux
Gathering diskspace information df command:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       37G 3.9G 31G 11% /
/dev/sda1 99M 13M 82M 13% /boot
tmpfs 506M 0 506M 0% /dev/shm
[[email protected] python]#

用python执行Linux命令,布布扣,bubuko.com

时间: 2024-08-06 11:58:32

用python执行Linux命令的相关文章

[Python]执行Linux命令

使用subprocess模块 import subprocess # 防火墙服务存在关闭状态 child1 = subprocess.Popen(["systemctl status firewalld | grep Active"], stdout=subprocess.PIPE, shell=True) print(child1.communicate()) #----执行结果------ (b' Active: inactive (dead)\n', None) # samba服

解决python执行linux查看文件命令后,因为^@特殊符号导致部分内容不显示的问题

问题现象 文件a的内容如下: 查看第2行到第5行的内容:sed -n '2,5p' /tmp/test/a 可见在命令行执行,是没有问题的. 在python中使用paramiko模块执行linux命令,主要代码如下: def toServer(self, str): sys.setdefaultencoding('utf-8') return str.decode('utf-8').encode('GB18030') def fromServer(self, str): sys.setdefau

python执行shell命令

python执行shell命令 #!/usr/bin/python2.7 #coding=utf-8 import shlex import datetime import subprocess import time def execute_command(cmdstring, cwd=None, timeout=None, shell=False): # 执行一个SHELL命令 ## 封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr ## 参数:

python执行mysqldump命令

本文简单讲述如何利用python执行一些sql语句,例如执行mysqldump命令,进行数据库备份,备份成sql文件 #!/usr/bin/python#导入os模块import os#导入时间模块import time#导入sys模块import sys#追加mysql的bin目录到环境变量sys.path.append('C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin')#如果不存在backup文件,新建一个if not os.path.e

PHP执行Linux命令行小例——文件压缩

前几天工作中,需要将3个txt文件,打包成*.zip down到本地…… 一开始,我和普通青年一样,想到用PHP内置的 ZipArchive,代码看起来应该是这样的: /*拆分成3个txt文件 分别是wow_1.txt wow_2.txt 和 wow_3.txt*/ $zip=new ZipArchive(); $zipfile='./Exl_file/wow.zip'; if($zip->open($zipfile,ZIPARCHIVE::CREATE)===TRUE){ $zip->add

使用Android平板编程,执行linux命令

android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/apps/details?id=com.aide.ui&hl=en Terminal IDE  介绍http://www.cnblogs.com/simpleyyt/archive/2012/11/24/GNU4Android.html 可以使用平板OTG连接键盘 执行linux命令 http://wen

Java程序执行Linux命令(JSP运行其他程序)

java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个本机进程,并返回 Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成. 检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法可能无法针对某些本机平台上的特定进程很好地工作,比如,本机窗口进程,守护进程,M

php执行linux命令:command not found

目前在SUSE系统上可以不使用命令的全路径.但是在CentOS上必须使用.如何设置环境变量,没辙! 摘自:http://blog.csdn.net/wfdtxz/article/details/7283512 #whereis ifconfig ifco #whereis ifconfig ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz #echo $PATH /usr/local/bin:/usr/bin:/bin:/usr

Java中通过jsch来连接远程服务器执行linux命令

有时候你可能需要通过代码来控制执行linux命令实现某些功能. 针对这类问题可以使用JSCH来实现,具体代码如下: public class CogradientImgFileManager{ private static final Logger log = LoggerFactory.getLogger(CogradientImgFileManager.class); private static ChannelExec channelExec; private static Session