LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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



Reply
 
Search this Thread
Old 03-04-2003, 05:16 AM   #1
justmoon
LQ Newbie
 
Registered: Mar 2003
Location: Germany
Distribution: SuSE 8.1
Posts: 9

Rep: Reputation: 0
Question Shell Color Codes


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....
 
Old 03-04-2003, 07:25 AM   #2
iceman47
Senior Member
 
Registered: Oct 2002
Location: Belgium
Distribution: Debian, Free/OpenBSD
Posts: 1,123

Rep: Reputation: 47
You can search for man pages here: http://man.linuxquestions.org/
 
Old 03-04-2003, 07:40 AM   #3
justmoon
LQ Newbie
 
Registered: Mar 2003
Location: Germany
Distribution: SuSE 8.1
Posts: 9

Original Poster
Rep: Reputation: 0
Hi, there is no documentation about color codes in your manpage archives as far as I see

Maybe I should do another example:
When you type "ls" in most modern Unix shells, you get pretty colored output.

But, when I use my Webinterface (which calls the command and forwards the output to the browser) I just get some cryptic codes.

I thought, I should be possible to convert these cryptic codes to html tags, so that the browser would display the same colors as the shell does.

I just used "man" as an example, because it prints out much bold text and therefore uses many of these codes.

Sorry, it's very difficult for me to speak proper english.
 
Old 03-04-2003, 07:43 AM   #4
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
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.
 
Old 03-04-2003, 07:44 AM   #5
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
Oh. I'm not sure about color codes.
 
Old 03-04-2003, 08:09 AM   #6
justmoon
LQ Newbie
 
Registered: Mar 2003
Location: Germany
Distribution: SuSE 8.1
Posts: 9

Original Poster
Rep: Reputation: 0
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.

Last edited by justmoon; 03-04-2003 at 08:20 AM.
 
Old 03-04-2003, 08:28 AM   #7
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
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.
 
Old 03-04-2003, 08:48 AM   #8
justmoon
LQ Newbie
 
Registered: Mar 2003
Location: Germany
Distribution: SuSE 8.1
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
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:

Code:
index.php
upload
upload_friends
uploader.php

[ means colorcode comes now.
01 means italic/bold/normal (bold in this case)
32 is the color.
m means colorcode done

 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
Greetings, moon

Last edited by justmoon; 03-04-2003 at 08:56 AM.
 
Old 03-04-2003, 02:46 PM   #9
KeithKessler
Member
 
Registered: Dec 2001
Location: Stow, OH
Distribution: Slackware 10
Posts: 73

Rep: Reputation: 15
Wow, what an interesting thread - I'm going to keep my eye on this one. Good work thus far!

--Keith
 
Old 03-04-2003, 02:58 PM   #10
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
[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

PS1='\[\033[0;$PC\](\@)\u@\h:\[\033[1;$PC\]\w\[\033[0;37m\]\$ '
export PS1

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?
 
Old 03-04-2003, 04:29 PM   #11
justmoon
LQ Newbie
 
Registered: Mar 2003
Location: Germany
Distribution: SuSE 8.1
Posts: 9

Original Poster
Rep: Reputation: 0
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)
 
Old 03-04-2003, 04:49 PM   #12
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
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)

Last edited by nxny; 03-04-2003 at 04:54 PM.
 
Old 03-05-2003, 03:00 AM   #13
justmoon
LQ Newbie
 
Registered: Mar 2003
Location: Germany
Distribution: SuSE 8.1
Posts: 9

Original Poster
Rep: Reputation: 0
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?

Last edited by justmoon; 03-05-2003 at 03:01 AM.
 
Old 03-06-2003, 12:59 AM   #14
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
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 !!
 
Old 03-06-2003, 03:48 AM   #15
justmoon
LQ Newbie
 
Registered: Mar 2003
Location: Germany
Distribution: SuSE 8.1
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
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 )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ls color codes jpstunr Linux - Newbie 3 03-20-2012 03:41 PM
Are the hex codes for colors in a jpg the same codes as used in html? abefroman Linux - Security 3 07-31-2005 04:21 PM
How do I change color under shell? babyboss Slackware 5 09-30-2004 02:59 AM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 08:51 PM
Shell Color Tags nocturnal Linux - General 2 05-17-2003 05:13 PM


All times are GMT -5. The time now is 09:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration