Skip to content Skip to sidebar Skip to footer

How To Convert Linux's Shell Output To Html?

is there any way to convert bash output to html ? for example if I had some colorized output in bash ( something like htop ), how can I convert it to html tags ... ( something like

Solution 1:

There's ansifilter plus some tools like highlight will produce colorized html from plain text such as source files.

Both available here.

Solution 2:

Yes, you need to pipe the result through a tool like ansi2html.

Solution 3:

Without any pretty-printing, the simplest thing you can always do is to escape everything that needs escaping, and wrap a basic HTML shell around (the following should be valid minimal HTML5). For example, get a hold of fastesc: http://raa.ruby-lang.org/project/fastesc/, and that wrap it into an HTML shell.

If you want to preserve the ANSI magic, then you need to convert that to HTML, perhaps with http://ansi-sys.rubyforge.org/

And then do something like this, depending on your needs:

require 'ansisys'


def ansi_escape(string)
    terminal = AnsiSys::Terminal.new
    terminal.echo(string)
    terminal.render 
end

def to_html(string)
    %Q{ <!DOCTYPE html><title>Converted to html</title><pre>
        #{ansi_escape(string)}
        </pre>
    } 
end

Solution 4:

1.) check that ansifilter and script programs are installed

2.) script -c 'ansible-playbook -i /tmp/or/some /tmp/other/command.yml' -O /tmp/myTypescriptOutput.file

(script writes the output of a command to a file in typescript notation)

3.) convert to html, e.g. to view with your browser:

ansifilter -i /tmp/myTypescriptOutput.file -H -o output.html

convert to rtf, e.g. to view with libreoffice or word

ansifilter -i /tmp/myTypescriptOutput.file -R -o output.rtf

see man ansifilter for other options (latex, tex, svg, pangot, txt, ...)

(additional, if highlight is installed:

highlight /tmp/myTypescriptOutput.file --force

... this will print the saved-shell output in color, like if it was executed at first time)

Solution 5:

As suggested in @eli's answer, you can use script to capture the colored output to a file.

You can then convert the output to HTML with aha, the "Ansi HTML Adapter", which should be available in your distribution's repositories: apt install aha or yum install aha for Debian-based or Redhat-based distributions.

If you want to capture only a single command, use the -c option:

script -c "grep --color ..."

This will save the output to a file named typescript in your current directory.

To save to a different file, add the file name at the end:

script -c "grep --color ..." my_grep_colored_output

See man script for other options.

To capture several commands from an interactive session, just start script without a command, and enter Ctrl-d to exit script when done.

To just view the file in color, use less -R.

To convert it to HTML with aha :

aha -s -f typescript > output.html

or

aha -s < typescript > output.html

The -s option makes it write a style sheet in the html header instead of using inline styles through the file. That makes it easier to change colors if some background/foreground combinations turn out hard to read in a browser.

Post a Comment for "How To Convert Linux's Shell Output To Html?"