详谈python中subprocess shell=False与shell=True的区别
作者:逍遥自在017 发布时间:2021-08-01 02:00:01
shell=True参数会让subprocess.call接受字符串类型的变量作为命令,并调用shell去执行这个字符串,当shell=False是,subprocess.call只接受数组变量作为命令,并将数组的第一个元素作为命令,剩下的全部作为该命令的参数。
举个例子来说明
from subprocess import call
import shlex
cmd = "cat test.txt; rm test.txt"
call(cmd, shell=True)
上述脚本中,shell=True的设置,最终效果是执行了两个命令
cat test.txt 和 rm test.txt
把shell=True 改为False,
from subprocess import call
import shlex
cmd = "cat test.txt; rm test.txt"
cmd = shlex(cmd)
call(cmd, shell=False)
则调用call的时候,只会执行cat的命令,且把 "test.txt;" "rm" "test.txt" 三个字符串当作cat的参数,所以并不是我们直观看到的好像有两个shell命令了。
也许你会说,shell=True 不是很好吗,执行两个命令就是我期望的呀。但其实,这种做法是不安全的,因为多个命令用分号隔开,万一检查不够仔细,执行了危险的命令比如 rm -rf / 这种那后果会非常严重,而使用shell=False就可以避免这种风险。
总体来说
看实际需要而定,官方的推荐是尽量不要设置shell=True。
补充: python subprocess模块的shell参数问题
昨天调试其他同学的代码时,发现对于subprocess模块所传的args变量,与shell变量存在关联,传值不当会有各种问题。比较有趣,就记录一下。
根据subprocess模块的args定义如下:
args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.
对于args,可传string,也可传list,但当传string时,shell的值必须设为True。
当shell为True时
If shell is True, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user's home directory.
就是调用了系统的 sh 来执行命令(args的string),这样会导致一些猥琐的安全问题,类似于SQL Injection攻击:
from subprocess import call
filename = input("What file would you like to display?\n")
What file would you like to display?
non_existent; rm -rf / #
call("cat " + filename, shell=True) # Uh-oh. This will end badly...
所以,安心用shell=False吧,记得args传list。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/xiaoyaozizai017/article/details/72794469


猜你喜欢
- df.sort_index()实现按索引排序,默认以从小到大的升序方式排列,如希望按降序排列,传入ascending = Falseimpo
- 1、Linux主机重定向 Godaddy的Liunx主机,Godaddy本身已经支持Apache,所以直接创建一个.htaccess文件就可
- 想必大家都知道MSSQL中SA权限是什么,可以说是至高无上。今天我就它的危害再谈点儿,我所讲的是配合NBSI上传功能得到WebShell。在
- 一,对应点相乘,x.mul(y) ,即点乘操作,点乘不求和操作,又可以叫作Hadamard product;点乘再求和,即为卷积data =
- 继上一篇文章使用xlrd来读Excel之后,这一篇文章就来介绍下,如何来写Excel,写Excel我们需要使用第三方库xlwt,和xlrd一
- 有关JS中字符串的相关文章,现在网上大概不计其数了。这里我不想再就这个问题做过多的论述,只是对几种方式的实现在各种浏览器中的执行效率进行对比
- 简单的显示记录已经掌握,现在需要的就是通过ASP将信息内容插入到数据库中。一、拥有数据库cnbruce.mdb本数据库的作用就是用来 * 入数
- 背景在吉日嘎拉的软件编程走火入魔之:数据库事务处理入门(适合初学者阅读)文章中关于MS SQL Server和Oracle对数据库事务处理的
- #!/usr/bin/python#coding=gbkclass User: def __init__
- 时间差函数TIMESTAMPDIFF、DATEDIFF的用法我们在写sql语句,尤其是存储过程中,会频繁用到对于日期、时间的比较和判断,那么
- 在很多应用程序开发中,需要记录某些数据表的历史记录或修改痕迹,以便日后出现数据错误时进行数据排查。这种业务需求,我们可以通过数据库的触发器来
- 见下面的代码:<html><head><title>精彩春风之月份查询</title><
- 实例如下:#coding=utf-8import subprocessfrom time import *import win32apiim
- 这篇文章主要介绍了用Python画一个LinkinPark的logo代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 在有些情况下,利用try…except来捕捉异常可以起到代替if…else的作用。比如在判断一个链表是否存在环的leetcode题目中,初始
- 什么是 MyBatis?MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 J
- 1.概述mysql-monitor MYSQL 监控工具,优化工具,各种工具为一体的java spring boot 项目git地址:htt
- 几天前,想把上个月校园招聘的餐旅费报销一下。结果在公司内网的报销系统折腾了三个半小时才搞定。看看自己报销的金额:802块。觉得挺无奈,花了三
- 在ASP中加密方法有对应的解密方法好象不多,现在根据前辈资料整理出在asp中加密与解密函数,根据RSA 算法实现的。什么是RSA?
- 如下所示:(x,y)为要转的点,(pointx,pointy)为中心点,如果顺时针角度为anglesrx = (x-pointx)*cos(