python - python程序中的Bash

标签 python linux bash

<分区>

例如,我如何在 python 中使用 bash 命令:

# ifconfig eth0 promisc

代码如下:

import socket
# the public network interface
HOST = socket.gethostbyname(socket.gethostname())
# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))
# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
# receive a package
print s.recvfrom(65565)
# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

而不是 s.ioctl 因为 linux 不支持这个命令?

最佳答案

import subprocess
subprocess.check_call(['ifconfig', 'eth0', 'promisc'])

或者如果您希望它从命令返回文本,请使用 check_output。如果命令失败,两者都会自动引发异常。

关于python - python程序中的Bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26361713/

相关文章:

python - 从终端运行和从 Python 运行时脚本的工作方式不同

linux - 一个或两个点(. 或 ..),好的,但是三个或更多点是什么意思?

python - 拼写错误的 __future__ 导入会导致脚本稍后出错,而不是导入位置出错

在 C Linux 中创建多个守护进程并在它们之间进行通信

python - 使用 matplotlib quiver 改变箭头的大小

linux - 为什么我在执行 1 个 fork 时会得到 2 个进程?

python os.system 阻止向后命令

linux - 当应用程序可以作为系统服务运行时,为什么要使用 nohup?

python - 如何将图片作为值添加到 Tkinter Treeview 中?

python - SQL Bigint 到 int64 到时间戳产生错误的日​​期