Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm quite new to linux, so excuse me if this is a stupid question.
I wanted to control my linux server from school, but they don't allow NAT, neither installing own programs, so the only way out is a HTTP Proxy. Therefore I wrote a little php script allowing you to upload files and send commands to the shell.
Everything is running fine, except the "man" command. The manpages have some kind of color codes in them, and these aren't understood by the web browser. So I'd like to convert them to HTML-Tags. I didn't find any documentation of these codes, where can I find a doc?
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771
Rep:
Re: Shell Color Codes
Quote:
Originally posted by justmoon Hi guys,
I'm quite new to linux, so excuse me if this is a stupid question.
I wanted to control my linux server from school, but they don't allow NAT, neither installing own programs, so the only way out is a HTTP Proxy. Therefore I wrote a little php script allowing you to upload files and send commands to the shell.
Everything is running fine, except the "man" command. The manpages have some kind of color codes in them, and these aren't understood by the web browser. So I'd like to convert them to HTML-Tags. I didn't find any documentation of these codes, where can I find a doc?
Thanks for your time, jm
ps: I'm german....
Very interesting. I'd like to see your php script once you're done with it. Regarding your question, you can run 'man2html' to convert unformatted man pages to HTML formatted pages.
for example, you can get yourself an HTML formatted mount man page by using
man2html /usr/share/man/man8/mount.8
or wherever your man pages are depending on your distro. Try running man -w <command> to find it out. Please update your profile with your distro.
Watch out for gzipped man pages though. Like /usr/share/man/man8/init.8.gz
I would run gzcat and pipe the output in this case. The best way would be to write a neat little shell script that outputs HTML in either case and call it from the PHP page.
thx, by searching for man2html I found out that the codes "man" uses are called "nroff".
I already downloaded the sourcecode of a simple c program similar to man2html/nroff2html. I will now port it to php and integrate it into the system.
But, I'm stil not able to interpret the codes ls etc. uses, because nroff is only for bold and italic text not for colors.
The search continues....
Edit1://I now know that ls first needs to be configured to output color codes also if not outputting to a tty.
@nxny: You think a php-based remote control system would be of interest? If so, I would love to make it public.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771
Rep:
My man pages dont seem to be in color in RH 8.0, again.. what distro are you using?
Have you considered using a stylesheet? I would examine the man2html output, mark the tags I'm interested in and make a simple stylesheet that gets 'link rel'ed from a PHP page that actually displays the html output.
I bet your distro is already using the shell-nroff equivalent of the HTML stylesheet.
OH HELL YEAH. A php based remote control would be fabulous. I'll even contribute. I'm sure a few other people will too.
Originally posted by nxny My man pages dont seem to be in color in RH 8.0, again.. what distro are you using?
No no, man pages are not colored! nrof is just for bold and italic text. That's why it's different from ls. I'm using SuSE 8.1 at the moment, but I want to change to Debian, when I have the time to do so.
Quote:
Have you considered using a stylesheet? I would examine the man2html output, mark the tags I'm interested in and make a simple stylesheet that gets 'link rel'ed from a PHP page that actually displays the html output.
nroff is based on control chars that are saying "next char is bold".
html is totally different, it says "bold from here until here". So a converter is needed, that detects multiple bold characters and combines them within a single <b></b>. (Am I right?)
Quote:
I bet your distro is already using the shell-nroff equivalent of the HTML stylesheet.
OH HELL YEAH. A php based remote control would be fabulous. I'll even contribute. I'm sure a few other people will too.
As soon as I solved that color/bold/italic stuff, I'll change the system into a more flexible, module based one.
I now know about the ls colors, too:
You can force the display of colors with "--color=always". Then there are codes like this:
[ means colorcode comes now. 01 means italic/bold/normal (bold in this case) 32 is the color. m means colorcode done
[m means all colors back to default.
But I still need to find out more about the color numbers.
EDIT://Here are the number definitions:
Code:
# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771
Rep:
[QUOTE]Originally posted by justmoon
>nroff is based on control chars that are saying "next char is bold".
html is totally different, it says "bold from here until here". So a converter is needed, that detects multiple bold characters and combines them within a single <b></b>. (Am I right?)
I haven't messed with nroff, but sounds quite reasonable
>I now know about the ls colors, too:
You can force the display of colors with "--color=always". Then there are codes like this:
I could've saved you some time there. Here's a snippet from /etc/profile that sets a different promptcolor for root. I got curious about it a few months ago too , hehe.
#set promptcolor
if [ `id -g` -eq 0 ]; then
PC=32m; # Green
else
PC=31m; # Red
fi
Here are my ls aliases as defined in the same file.
alias ls='ls -F --color=tty'
alias la='ls -AlFh --color=tty'
alias ll='ls -lF --color=tty'
>But I still need to find out more about the color numbers.
I wonder why. If you need to display the colors in a browser, and your input is 'uncolored' HTML, the only thing I can think of is CSS ( Cascading Style Sheets) as this point. Would you clarify?
First: Thanks! Your snippets helped me solving some of the last bugs in my converter routine.
Second: What I meant with "But I still need to find out more about the color numbers" was that I still needed to know which color code (i.e. 31) corresponds with which color (i.e. red)
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771
Rep:
First: Glad I could be of assistance.
Second: You posted those yourself ( the color codes chart ) here
I'm definitely missing something here.
Third: Are you curious about the shell colors in a way that is totally unrelated to your PHP based project? If you're _not_ seeing you man pages in color already, ( dont mean to be rude here ) why are you still worried about the color codes? Even if you were seeing man pages in color, do you realize those codes mean nothing when outside of a BASH shell environment? A browser will print them out as literal characters, wont it?
Fourth: What are you doing currently from a security standpoint ( who can run it? Are you using htpasswd based authentication or something to control access to the page )? Aren't all your system commands executed in the security context of the PHP user ( not depending on who's using the PHP page - without privilege separation in other words)
That First, Second etc. seems to be quite helpful, so I'm keeping that for now:
Second: I posted them as a edit, when I found a doc about them.
I just want to keep questions in (even if I ansered them by myself), because people searching for that question will come here and find the answer.
Third: I started this thread, because I thought there was some standard, how to code stuff under linux, so that it will be displayed colored/bold/whatever in the shell. During our research, we found out, that the man pages are coded in nroff, which supports only bold/italic text. Now I was curious about how ls (and some other commands) do their coloring. And the last step was to teach my script to detect both types of codes.
Now, if you type in the man command, all output is automatically displayed correctly, with all the bold and italic text. And if you type in ls /etc/init.d/, you see a directory listing... with beautiful colors.
Don't know how I could say it any better, let's just wait until I make it public, then you can have a look at the source.
Fourth: For the Client, it looks like htpasswd authentication, but it isn't. The Script reads out the "HTTP_AUTH_USER" and "HTTP_AUTH_PASS" Variables and then compares them with two hardcoded values. VERY secure, believe me, but absolutely unflexible and unacceptable for an opensource script. So there are two ways I could go:
1: Store all User/Pass Combinations and Privileges in a database, mysql i.e.
+ Absolutely safe
+ Can be integrated easily into systems like Postnuke, xoops
2: Store all User/Pass Combinations and Privileges in a file, i.e. xml format
+ Much faster (not having to connect to a mysql server, which is slow even for local servers)
+ Does not require the user to have mysql installed
- I'm not sure, how secure a file containing all user/pass combinations (MD5 coded of course) is
I think I will first code way two, then I will code way one, which is much more complicated to code and then I'll make an option and let the user decide. What do you think?
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771
Rep:
Second: Good!!! I didn't realize you added it later.
Third: Aah. I get it. You are out to replicate the exact behavior that one would expect from a shell, man page or directory listing. Crystal Clear. Thanks for the explanation.
Fourth: HTTP_AUTH_USER and HTTP_AUTH_PASSWD, hmmm I would keep it that way for the time being, but that would make it easy for a sniffer to find out the credentials en route to your server, wouldn't it? I mean, sometime down the line I would see myself MD5 hashing the password combined with a-random-number-that-the-server-sends as described in great lengths here ( in such a way that it keeps cross-site-scripting risks to a minimum). The javascript implementation of MD5 can be downloaded from the same site. This mechanism is reasonably immune to eavesdroppers, but still vulnerable when it comes to man-in-the-middle attacks. We'll think about those sometime down the line.
Regarding credential storage on the server side, I would opt for method #2 at this point although a decent indexed table on a server side SQL database should offer decent performance on the server side, if you decide to implement it later on. If not, we can stretch it by storing this database on /dev/ram0 !!
Originally posted by nxny Second: Good!!! I didn't realize you added it later.
Third: Aah. I get it. You are out to replicate the exact behavior that one would expect from a shell, man page or directory listing. Crystal Clear. Thanks for the explanation.
OK for that...
Now lets get to business:
Quote:
Fourth: HTTP_AUTH_USER and HTTP_AUTH_PASSWD, hmmm I would keep it that way for the time being, but that would make it easy for a sniffer to find out the credentials en route to your server, wouldn't it?
You're right definitly! Still it would be quite safe, because really few people know how to break into the routers that are on the way. (I don't know it, at least ) As a Coder, it's my duty to find the best solution possible... so let's see the altarnatives:
Quote:
I mean, sometime down the line I would see myself MD5 hashing the password combined with a-random-number-that-the-server-sends as described in great lengths here ( in such a way that it keeps cross-site-scripting risks to a minimum). The javascript implementation of MD5 can be downloaded from the same site. This mechanism is reasonably immune to eavesdroppers, but still vulnerable when it comes to man-in-the-middle attacks. We'll think about those sometime down the line.
Not meaning, that I would have kept with the HTTP_AUTH_ thing, this is even more, than I had planned. But I think, as this is a remote administration tool, we will use this technique. Can't be too secure
Quote:
Regarding credential storage on the server side, I would opt for method #2 at this point although a decent indexed table on a server side SQL database should offer decent performance on the server side, if you decide to implement it later on. If not, we can stretch it by storing this database on /dev/ram0 !!
All right!
Ah, what I wanted to ask you:
Where are you from? (I'm German in case you didn't notice )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.