如何在執行一個命令或程序之前就了解它會做什么
有沒有想過在執行一個 Unix 命令前就知道它干些什么呢?并不是每個人都會知道一個特定的命令或者程序將會做什么。當然,你可以用 Explainshell 來查看它。你可以在 Explainshell 網站中粘貼你的命令,然后它可以讓你了解命令的每個部分做了什么。但是,這是沒有必要的。現在,我們從終端就可以輕易地在執行一個命令或者程序前就知道它會做什么。 maybe
,一個簡單的工具,它允許你運行一條命令并可以查看此命令對你的文件做了什么,而實際上這條命令卻并未執行!在查看 maybe
的輸出列表后,你可以決定是否真的想要運行這條命令。
maybe
是如何工作的
根據開發者的介紹:
maybe
利用python-ptrace
庫在ptrace
控制下運行了一個進程。當它截取到一個即將更改文件系統的系統調用時,它會記錄該調用,然后修改 CPU 寄存器,將這個調用重定向到一個無效的系統調用 ID(效果上將其變成一個無效操作(no-op)),并將這個無效操作(no-op)的返回值設置為有效操作的返回值。結果,這個進程認為,它所做的一切都發生了,實際上什么都沒有改變。
警告:在生產環境或者任何你所關心的系統里面使用這個工具時都應該小心。它仍然可能造成嚴重的損失,因為它只能阻止少數系統調用。
安裝 maybe
確保你已經在你的 Linux 系統中已經安裝了 pip
。如果沒有,可以根據您使用的發行版,按照如下指示進行安裝。
在 Arch Linux 及其衍生產品(如 Antergos、Manjaro Linux)上,使用以下命令安裝 pip
:
sudo pacman -S python-pip
在 RHEL,CentOS 上:
sudo yum install epel-release
sudo yum install python-pip
在 Fedora 上:
sudo dnf install epel-release
sudo dnf install python-pip
在 Debian,Ubuntu,Linux Mint 上:
sudo apt-get install python-pip
在 SUSE、 openSUSE 上:
sudo zypper install python-pip
安裝 pip
后,運行以下命令安裝 maybe
:
sudo pip install maybe
了解一個命令或程序在執行前會做什么
用法是非常簡單的!只要在要執行的命令前加上 maybe
即可。
讓我給你看一個例子:
$ maybe rm -r ostechnix/
如你所看到的,我從我的系統中刪除一個名為 ostechnix
的文件夾。下面是示例輸出:
maybe has prevented rm -r ostechnix/ from performing 5 file system operations:
delete /home/sk/inboxer-0.4.0-x86_64.AppImage
delete /home/sk/Docker.pdf
delete /home/sk/Idhayathai Oru Nodi.mp3
delete /home/sk/dThmLbB334_1398236878432.jpg
delete /home/sk/ostechnix
Do you want to rerun rm -r ostechnix/ and permit these operations? [y/N] y
maybe
執行了 5 個文件系統操作,并向我顯示該命令(rm -r ostechnix/
)究竟會做什么?,F在我可以決定是否應該執行這個操作。是不是很酷呢?確實很酷!
這是另一個例子。我要為 Gmail 安裝 Inboxer 桌面客戶端。這是我得到的輸出:
$ maybe ./inboxer-0.4.0-x86_64.AppImage
fuse: bad mount point `/tmp/.mount_inboxemDzuGV': No such file or directory
squashfuse 0.1.100 (c) 2012 Dave Vasilevsky
Usage: /home/sk/Downloads/inboxer-0.4.0-x86_64.AppImage [options] ARCHIVE MOUNTPOINT
FUSE options:
-d -o debug enable debug output (implies -f)
-f foreground operation
-s disable multi-threaded operation
open dir error: No such file or directory
maybe has prevented ./inboxer-0.4.0-x86_64.AppImage from performing 1 file system operations:
create directory /tmp/.mount_inboxemDzuGV
Do you want to rerun ./inboxer-0.4.0-x86_64.AppImage and permit these operations? [y/N]
如果它沒有檢測到任何文件系統操作,那么它會只顯示如下所示的結果。
例如,我運行下面這條命令來更新我的 Arch Linux。
$ maybe sudo pacman -Syu
sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid' option set or an NFS file system without root privileges?
maybe has not detected any file system operations from sudo pacman -Syu.
看到沒?它沒有檢測到任何文件系統操作,所以沒有任何警告。這非常棒,而且正是我所預料到的結果。從現在開始,我甚至可以在執行之前知道一個命令或一個程序將執行什么操作。我希望這對你也會有幫助。
Cheers!
資源: