如何将终端输出转换为 HTML 文件格式

How to convert terminal output to HTML file format

我在这里使用 "ansi2html.sh" http://www.pixelbeat.org/scripts/ansi2html.sh 来完成我的工作。

ls -lrt /web/htdocs | tail -12 | ./ansi2html.sh --bg=dark >test.html

我在具有 Linux OS 到 运行 的本地主机上使用 ansible,这在多个主机上生成了 html 文件,然后在每个远程主机上我最终会将所有 html 文件从所有远程服务器拉到我的本地 ansible 服务器。

这适用于所有 Linux 系统。

但是,"ansi2html.sh" 具有 gawk 的依赖性,这在一组生产 AiX 6.1 和 7 系统中不存在。

我收到这个错误:

./ansi2html.sh[38]: gawk:  not found

因为我的 AiX 正在托管生产应用程序;不建议安装 gawk

我不知道解决方案是否可以从所有 AiX 主机获取 ls -lrt 的输出,然后将该输出提供给具有 linux 的本地 ansible linux 服务器上的 "ansi2html.sh" =13=]。不确定这是否有效,如果有效如何?注意:我希望输出具有与 putty 终端提示相同的外观和感觉。

我能否获得 AiX 的解决方案,以便我可以使用 ansible 从所有 AiX 主机获取 html 输出为 ls -lrt 的文件?

使用 aha Ansi HTML 适配器 完成此任务,效果很好:

ls -lrt ~ | tail -12 | aha --black --title "Home list"> ls.html

示例使用 html2text

curl  | html2text | aha --black --title "the SO question"> txt.html

安装:

sudo apt install aha
sudo apt install html2text

可以使用 iconv 转换 ANSII 编码: Force encode from US-ASCII to UTF-8 (iconv)


最后,仅 php 也可以实现所有这些功能,它应该适用于任何地方。使用 php:

将 html 转换为文本的快速基础
php -r 'echo htmlspecialchars(trim(strip_tags("<div> <b>Hello</b> world</div>")));'
// Hello world

ANSII 使用正则表达式从 php 转换为文本:

$ansii = " |[0m [34m▓▓▓▓▓[0m";
echo preg_replace("/\x1B\[[0-9;]*[JKmsu]/","",$ansii);

(已写成注释)您可以使用ssh在远程计算机上执行命令并在本地计算机上处​​理输出。示例:

ssh user@aixhost 'ls -lrt /web/htdocs | tail -12' |
./ansi2html.sh --bg=dark >test.html

标准输入也可以重定向,例如:

ssh user1@host1 'cd frompath; tar -czf - sendme/' |
ssh user2@host2 'cd topath; tar -xzf -'

注意:我对 Ansible 一无所知,但我听说有了它你几乎可以做任何没有它能做的事情。