3

我知道ufw是用python写的。是否可以通过 python 将 ufw 用作 api?

4

2 回答 2

4

当然,ufw可执行文件只是对同名 python 包的一个薄包装。我不知道有任何文档,但您可以浏览源代码并查看一切是如何工作的。通过启动 python 解释器、导入模块并询问它的导入路径来查找ufw脚本位置和包位置:which ufwufw

simon@mymachine:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ufw
>>> ufw.__file__
'/usr/lib/python2.6/dist-packages/ufw/__init__.pyc'
>>> 
于 2012-06-23T16:26:45.100 回答
3

我创建了一个包装器 easyufw,以支持将 ufw 用作 api。

博客文章在这里

存储库在这里

设置

你可以像这样从 GitLab 中提取 easyufw:

git clone https://gitlab.com/dhj/easyufw.git

您还需要 ufw 并且需要以 root 权限运行 easyufw,因为 ufw 需要它:

sudo pip install ufw
sudo python

尝试一下

设置完成后,您可以在 python 终端中键入这些命令,以感受它是多么容易。

请注意,界面是实时的——没有空运行,所以它实际上会运行你给它的命令。当心。

import easyufw.easyufw as ufw

print ufw.status()
ufw.enable() # enable the firewall
ufw.deny('22/tcp') # disable ssh -- could disable active sessions!
ufw.allow('22/tcp') # enable ssh -- '22' alone or as int includes tcp and udp
ufw.delete(22) # delete all rules for port 22 -- int required
ufw.disable() # this will leave your firewall disabled! (default in ubuntu, but IMO, not smart)

ufw.run您还可以使用like ufw.run('allow from 207.46.232.182')(after import easyufw.easyufw as ufw)运行任意命令

就是这样。希望它是有用的。

于 2016-09-15T13:04:07.950 回答