命令行樂趣:嘲諷輸錯(cuò)Bash命令的用戶
你可以通過配置 sudo 命令去嘲諷輸入錯(cuò)誤密碼的用戶。但是現(xiàn)在,當(dāng)用戶在 shell 輸錯(cuò)命令時(shí),就能嘲諷他了(濫用?)。
你好 bash-insulter
來自 Github 頁面:
當(dāng)用戶鍵入錯(cuò)誤命令,隨機(jī)嘲諷。它使用了一個(gè) bash4.x. 版本的全新內(nèi)置錯(cuò)誤處理函數(shù),叫
command_not_found_handle。
安裝
鍵入下列 git 命令克隆一個(gè)倉庫:
git clone https://github.com/hkbakke/bash-insulter.git bash-insulter
示例輸出:
Cloning into 'bash-insulter'...remote: Counting objects: 52, done.remote: Compressing objects: 100% (49/49), done.remote: Total 52 (delta 12), reused 12 (delta 2), pack-reused 0Unpacking objects: 100% (52/52), done.
用文本編輯器,比如說使用 vi,編輯你的 ~/.bashrc 或者 /etc/bash.bashrc 文件:
$ vi ~/.bashrc
在其后追加這一行(具體了解請(qǐng)查看 if..else..fi 聲明 和 source 命令):
if [ -f $HOME/bash-insulter/src/bash.command-not-found ]; thensource $HOME/bash-insulter/src/bash.command-not-foundfi
保存并關(guān)閉文件。重新登錄,如果不想退出賬號(hào)也可以手動(dòng)運(yùn)行它:
$ . $HOME/bash-insulter/src/bash.command-not-found
如何使用它?
嘗試鍵入一些無效命令:
$ ifconfigs$ dates
示例輸出:

一個(gè)有趣的 bash 鉤子功能,嘲諷輸入了錯(cuò)誤命令的你。
自定義
你需要編輯 $HOME/bash-insulter/src/bash.command-not-found:
$ vi $HOME/bash-insulter/src/bash.command-not-found
示例代碼:
command_not_found_handle () {local INSULTS=("Boooo!""Don't you know anything?""RTFM!""Hahaha, n00b!""Wow! That was impressively wrong!""What are you doing??""Pathetic""...and this is the best you can do??""The worst one today!""n00b alert!""Your application for reduced salary has been sent!""lol""u suk""lol... plz""plz uninstall""And the Darwin Award goes to.... ${USER}!""ERROR_INCOMPETENT_USER""Incompetence is also competence""Bad.""Fake it till you make it!""What is this...? Amateur hour!?""Come on! You can do it!""Nice try.""What if... you type an actual command the next time!""What if I told you... it is possible to type valid commands.""Y u no speak computer???""This is not Windows""Perhaps you should leave the command line alone...""Please step away from the keyboard!""error code: 1D10T""ACHTUNG! ALLES TURISTEN UND NONTEKNISCHEN LOOKENPEEPERS! DAS KOMPUTERMASCHINE IST NICHT FÜR DER GEFINGERPOKEN UND MITTENGRABEN! ODERWISE IST EASY TO SCHNAPPEN DER SPRINGENWERK, BLOWENFUSEN UND POPPENCORKEN MIT SPITZENSPARKEN. IST NICHT FÜR GEWERKEN BEI DUMMKOPFEN. DER RUBBERNECKEN SIGHTSEEREN KEEPEN DAS COTTONPICKEN HÄNDER IN DAS POCKETS MUSS. ZO RELAXEN UND WATSCHEN DER BLINKENLICHTEN.""Pro tip: type a valid command!")# 設(shè)置“隨機(jī)”種子發(fā)生器RANDOM=$(date +%s%N)VALUE=$((${RANDOM}%2))if [[ ${VALUE} -lt 1 ]]; thenprintf "\n $(tput bold)$(tput setaf 1)$(shuf -n 1 -e "${INSULTS[@]}")$(tput sgr0)\n\n"fiecho "-bash: $1: command not found"# 無效命令,常規(guī)返回已存在的代碼return 127}
贈(zèng)品:sudo 嘲諷
編輯 sudoers 文件:
$ sudo visudo
追加下面這一行:
Defaults insults
或者像下面尾行增加一句嘲諷語:
Defaults !lecture,tty_tickets,!fqdn,insults
這是我的文件:
Defaults env_resetDefaults mail_badpassDefaults secure_path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"## If set, sudo will insult users when they enter an incorrect password. ##Defaults insults# Host alias specification# User alias specification# Cmnd alias specification# User privilege specificationroot ALL = (ALL:ALL) ALL# Members of the admin group may gain root privileges% admin ALL = (ALL) ALL# Allow members of group sudo to execute any command% sudo ALL = (ALL:ALL) ALL# See sudoers(5) for more information on "#include" directives:#includedir /etc/sudoers.d
試一試:
$ sudo -k # 清除緩存,從頭開始$ sudo ls /root/$ sudo -i
樣例對(duì)話:

當(dāng)輸入錯(cuò)誤密碼時(shí),你會(huì)被一個(gè)有趣的的 sudo 嘲諷語戲弄。
贈(zèng)品:你好 sl
sl 或是 UNIX 經(jīng)典搗蛋軟件 游戲。當(dāng)你錯(cuò)誤的把 ls 輸入成 sl,將會(huì)有一輛蒸汽機(jī)車穿過你的屏幕。
$ sl

Linux / UNIX 桌面樂趣: 蒸汽機(jī)車
























