Python 运行 Linux 命令

标签 python linux command

我想使用 python 执行一些 linux 命令 这些是我的命令。

modprobe ipv6
ip tunnel add he-ipv6 mode sit remote 216.218.221.6 local 117.211.75.3 ttl 255
ip link set he-ipv6 up
ip addr add 2001:470:18:f3::2/64 dev he-ipv6
ip route add ::/0 dev he-ipv6
ip -f inet6 addr

216.218.221.6
117.211.75.3
2001:470:18:f3::2/64

这些 ip 是来自用户的输入。命令也需要root权限。 到目前为止我的代码。

import os

print("Enter Server Ipv4 Address")
serverip4=input()

print("Enter Local Ipv4 Address")
localip4=input()

print("Enter Client Ipv6 Address")
clientip4=input()

最佳答案

像这样:

import sys
import os
os.system("ip tunnel add he-ipv6 mode sit remote %s local %s ttl 255" % (whicheveripvariableisfirst), (whicheveripvariableisnext)))

如果您需要它以 sudo 级别运行,则将 sudo 放在命令部分或确保以 sudo 运行 python 脚本。

关于Python 运行 Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29339341/

相关文章:

python - 捕获变量中的结果

python - 哪一种效率更高?

python - 如何撤消 pandas 数据帧上的列聚合

Python多处理内存使用

node.js - Node - 检查路径中是否存在命令

regex - 如何使用 vi 删除文本文件中每行的前 5 个字符?

python - 使用 BeautifulSoup 解析 HTML,不使用类(仅段落)

linux - 模拟鼠标事件linux

linux - "systemctl restart"和 "systemctl start"有什么区别?

linux - 如何从命令提示符发送电子邮件?