用 inxi 列出系統資訊,在回報問題時提供環境

在「GPU Survival Toolkit for the AI age: The bare minimum every developer must know」這篇看到的工具,雖然裡面是講 GPU 相關的事情,但看到這段:

inxi -G

This command provides information about the graphics subsystem, including details about the GPU and the display.

inxi 這個工具是用 Perl 寫的,在 manpage 的說明是:

inxi - Command line system information script for console and IRC

提供一些資訊給使用者,方便使用者在問問題時順便複製貼上到 IRC 或是論壇。

看了一下系統的 dependency,我的桌機是因為 xubuntu-desktop 這個套件相依裝進去的,這是 XFCE 桌面環境的套件。

Ubuntu 的預設環境沒有包這個套件進去,會需要額外安裝。

處理 rTorrent 會卡頓的問題

rTorrent 算是很久了,在 CLI 下的 BitTorrent client。

但 rTorrent 一直有卡頓的問題,這個週末決定還是找一下怎麼解,而在 Reddit 上面可以看到討論:「Rtorrent stalling / freezing caused by bad tracker? I fixed my problem but I'm a bit puzzled.」,其中就有提到是 libtorrent 因為 DNS 查詢 blocking 的問題。

不過這邊有些人已經提出了解法,其中一個是使用 libudns 達成 async DNS query 來解決這個問題,這個 branch 有進到官方的 git repository 裡面 (但沒有被 merge 到 master 上):「rakshasa/libtorrent at slingamn-udns.10」。

看了一下雖然最後 commit 是 2019 年,但其實不算太舊,因為 master 從 2019 年到現在 2023 年也沒幾個 commit,不過直接用 slingamn-udns.10 的 libtorrent 搭最新版的 rtorrent 是編不起來的,主要是因為 IPv6 的支援改變了一些程式的 API 介面。

最後一個可以搭 rtorrent 的版本是 2021 年六月的 582e4e40256b43d3e5322168f1e1ed71ca70ab64,也已經比目前最近的正式 release 0.9.8 (2019/07/19) 還新。

把這些組合弄起來後跑了幾個小時,看起來順不少,暫時沒有遇到卡住的問題了...

jless:檢視 JSON 的工具

前幾天在「Show HN: Jless, a command-line JSON viewer (pauljuliusmartinez.github.io)」這邊看到用 Rust 寫的 jless 這個工具,官網有個動圖可以參考:

這樣方便不少,就不需要自己在對半天...

另外也剛好拿來練手,把 Rust 寫的套件包成 Ubuntu PPA:「PPA for jless」。

主要是 cargo vendor 這個指令可以把相依套件都抓下來放到 vendor/ 下面,然後設定 .cargo/config.toml 後就可以在本地端處理了,這對於 build farm 限制 internet 連線的情況會好用很多...

curl 的 command line 工具也要支援 JSON 格式了

Daniel Stenbergcurl 的 mailing list 上宣佈要支援 JSON 格式:「JSON support」,用法在「JSON awareness in the curl tool」在這邊可以看到,另外在 Hacker News 上的討論「The time might come when we add some JSON specific command line options (curl.se)」也可以翻翻。

討論裡面馬上有提到 HTTPie,這個套件基本上也算是標配了 (而且在各大 distribution 的 package 都有內建,直接裝就可以了),可以看到主要是處理 POST 輸入時的 JSON 部份 (在 Content-Type: application/json 的情境下),HTTP response 輸出的部份一般都還是用 jq 處理。

不過 curl 自己又定義了一套指定 JSON 內容的方式...

與 jq 互相配合的 jc

Simon Willison 的 blog 上看到的工具:「jc」,專案的網站:「JSON CLI output utility」。

可以把許多種輸出結果轉成 JSON 格式:

CLI tool and python library that converts the output of popular command-line tools and file-types to JSON or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.

所以就可以這樣用:

dig example.com | jc --dig

然後再丟給 jq

dig example.com | jc --dig | jq -r '.[].answer[].data'

支援的格式不少,在「Parsers」這段可以看到。

Ubuntu 22.04 後可以直接透過系統的 apt 安裝:「Ubuntu – Details of package jc in jammy」,在那之前也可以透過 pip 裝起來用...

等 Ubuntu 22.04 出了以後應該會變成標配安裝...

在 command line 下生出全灰的圖片

因為需要一張 1920x1080 全灰色的圖片,翻了一下 DuckDuckGo 找到這篇:「Linux create Image pixel by pixel in command line」。

裡面抓重點,主要的想法是 PPM 格式 (可以參考「Netpbm」這個條目),然後找到「PPM Format Specification」這份文件。

主要就是用 P3 模式下去產生檔案:

repeat 2073600 echo '127 127 127' >> grey.ppm

然後開頭的地方修一下,加上 P3、長寬資訊以及最大的值:

P3
1920 1080
255

接著就用 ImageMagick 轉檔:

convert grey.ppm grey.png

然後用 viewer 看一下,確認沒問題就收工了...

很多 ls 開頭的指令...

Twitter 上看到 ls* 指令:

文章是「ls* Commands Are Even More Useful Than You May Have Thought」這篇,本來以為是要裝軟體的,結果發現在講的都是已經內建裝好的指令...

包括了很多跟底層硬體界面相關的指令:

  • lsblk (儲存裝置相關)
  • lshw (硬體)
  • lscpu (CPU)
  • lspci (PCI)
  • lsusb (USB)

可以用一次有個感覺就好,這個年頭遇到問題時還是會靠搜尋引擎找答案 XDDD

Mutt 跳版推出 2.0

看到「Mutt 2.0 released」這篇,Mutt 推出 2.0 版,官方的 release notes 則是在這:「Mutt 2.0 Release Notes」。

這次版本跳到 2.0 主要是因為有 incompatible changes,實質上的變更其實沒有太多:

This release was bumped to 2.0, not because of the magnitude of features (which is actually smaller than past releases), but because of a few changes that are backward incompatible.

我自己還是保留一個信箱用 Mutt 在看信,主要是有一個信箱是完全控制在自己手上,所以自己架設 mail server 並且在上面看信...

上面主要是透過 procmail 拿來收各種信件 (尤其是 mailing list),對於純文字的閱讀還是頗方便...

第五堂:「Command-line Environment」

這個系列是從『MIT 的「The Missing Semester of Your CS Education」』這邊延伸出來的,這邊講的是「Command-line Environment」這篇。

開始聊之前先看一下其他的東西,我注意到這系列文章有簡體中文 (這裡,已經翻完) 與繁體中文 (這裡,還有幾篇還沒翻完) 的版本了,如果讀英文會比較累的人可以參考看看。

這堂講 command line 下的各種事情,感覺起來比起前面硬不少。

首先就先開始講 signal,各家系統的 signal 應該還是有些微差異,可以參考各家的 signal(7) (在 command line 下用 man 7 signal 可以列出來)。

這邊講的 signal 都比較通用,像是 SIGKILL 的殺傷力,SIGTERMSIGQUIT,然後有提到 SIGHUP,但 SIGHUP 的設計慣例是拿來重讀設定檔好像就沒提到了...

接下來就帶過 tmux,只大概講了一下概念 (sessions、windows、panes) 與用法。

然後講 shell 的 alias 對於打造環境的幫助,以及 dotfiles 的設計。

後面講 SSH 的應用,然後還提到了兩個不同方向的 port forwarding。

這些對於初學者來說應該蠻有幫助的,第一次打造自己的 shell environment 的過程應該還蠻有趣的?(已經是很久前了...)

讓 Python 輸出變豐富的 Rich

Hacker News 上看到的 Python 專案,讓 terminal 輸出變得更好看:「Rich is a Python library for rich text and beautiful formatting in the terminal.」。

看到當下吸引我的地方在於表格:

from rich.console import Console
from rich.table import Column, Table

console = Console()

table = Table(show_header=True, header_style="bold magenta")
table.add_column("Date", style="dim", width=12)
table.add_column("Title")
table.add_column("Production Budget", justify="right")
table.add_column("Box Office", justify="right")
table.add_row(
    "Dev 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118"
)
table.add_row(
    "May 25, 2018",
    "[red]Solo[/red]: A Star Wars Story",
    "$275,000,000",
    "$393,151,347",
)
table.add_row(
    "Dec 15, 2017",
    "Star Wars Ep. VIII: The Last Jedi",
    "$262,000,000",
    "[bold]$1,332,539,889[/bold]",
)

console.print(table)

輸出長這樣:

另外還有不少功能也不錯,會讓畫面豐富不少。