python之执行shell命令

[[email protected] ~]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os          #加载模块
>>> r=os.system
>>> r("ls /root")      #执行命令
anaconda-ks.cfg			  Music
ceph-release-1-1.el7.noarch.rpm   Pictures
Desktop				  Public
Documents			  Templates
Downloads			  Videos
epel-release-latest-7.noarch.rpm  win_2008.qcow2
initial-setup-ks.cfg		  zabbix-agent-3.0.5-1.el7.x86_64.rpm
0
时间: 2024-07-28 19:15:34

python之执行shell命令的相关文章

C++/Php/Python 语言执行shell命令

编程中经常需要在程序中使用shell命令来简化程序,这里记录一下. 1. C++ 执行shell命令 1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 5 int exec_cmd(std::string cmd, std::string &res){ 6 if (cmd.size() == 0){ //cmd is empty 7 return -1; 8 } 9 10 char

python中执行shell命令

sh是一个比subprocess好的库,能够执行shell命令 1.查看ip: [[email protected] myblog]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.31.253.193 netmask 255.255.240.0 broadcast 172.31.255.255 ether 00:16:3e:01:72:ac txqueuelen 1000 (Etherne

python如何执行shell命令

Jmeter是公司做接口测试的一个开源项目,今天在研究如何用python写个脚本去执行这个jmeter脚本,Jmeter有命令行模式和界面模式,设置好了环境之后,我用了最简单的一条命令做了测试: jmeter -n -t <testplan filename> -l <listener filename> 比如:jmeter -n -t  ..../文件.jmx  -l  result.txt  这里忽略jmx文件的编写,这些是测试写好的,我只要调用命令去执行就好了,后期写个定时任

python-执行shell命令的模块使用

最近,学习别人的blog,总结了下几个知识点,以备不时之需,面试中偶尔被问到:一种结果多种方法.以下为: 获取系统执行命令的返回值和输出结果: 1.os.system: 调用系统命令,完成后退出,显示输出.返回值(一般执行成功就为0). >>> import os >>> os.system('pwd')     /home/ubuntu     0 2.os.popen: 返回的是 file read 的对象,对其再进行read() 的操作可以看到执行的输出 >

使用sh库执行shell命令

python中执行shell命令 之前执行shell命令多是通过os.system(shell命令)的方式来执行,比较麻烦. 了解到sh是一个比subprocess好的库,能够执行shell命令 1.查看ip: [[email protected] myblog]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.31.253.193 netmask 255.255.240.0 broadcas

python中执行shell的两种方法总结

这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包均是Python现有的内置模块.需要的朋友可以参考借鉴,下面来一起看看吧. 一.使用python内置commands模块执行shell commands对Python的os.popen()进行了封装,使用SHELL命令字符串作为其参数,返回命令的结果数据以及命令执行的状态: 该命令目前已经废弃,被s

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 执行shell 命令,自动化添加攻击IP地址到iptables

通过python执行shell命令的方法有4种,在这里介绍一种常用的. os.system. os.popen. commands. subprocess 接下来介绍subprocess的使用 通过python 日志分析,获取到攻击源IP地址,收集写入到mysql数据库中 mysql如下: iptables.py 脚本 #!/usr/bin/env python # -*- coding:utf-8 -*- import os import MySQLdb import subprocess t

Python之路35-subprocess模块和Python3中常用执行shell命令方法

import subprocess #执行命令,返回命令执行状态 , 0 or 非0 retcode = subprocess.call(["free","-m"])    #返回值可判断执行是否正确,命令执行结果直接返回到屏幕 #执行命令,如果命令结果为0,就正常返回,否则抛异常 result = subprocess.check_call(["ls","-l"]) #执行命令,并返回结果,注意是返回结果,不是打印,下例结果