LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2007, 10:35 PM   #1
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
[Perl] Transforming non-printable characters.


Hello all.

I have spent some time reading perl books and writing my first perl scripts and now I have a script that reads data from iptables ipqueue (tcp port 80) and it prints all that tcp data to stdout.

From there I can pipe it to grep or sed and print only parts of that data that I'm interested of, for example like this:
Code:
bt ~ # /root/Desktop/tcp-payload.pl |sed -n '/<iframe/,/\/iframe>/p'
That would print out only http traffic that is between <iframe> tags.

But there is one thing that really bugs me, it is that when there is pictures like .jpg .png it really messes up things. You can test how it messes things up by catting .jpg or .png images.
Code:
cat /path/to/some.jpg
Is there some non printable characters or something similar (binary data?) messing up things ?
I believe what I need is somekind of filter that transforms that data/characters to printable characters like "*" .
If someone could point me to right direction (or give links to pages that I could read) it would be great.


tia

/////

Last edited by //////; 04-08-2007 at 05:28 AM.
 
Old 04-04-2007, 12:25 AM   #2
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824

Original Poster
Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
Ok, looks like I got it to work.
Code:
s/[^[:print:]]+//g
That removes all "badly behaving" characters from that traffic.
 
Old 04-04-2007, 03:59 PM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Quote:
Originally Posted by //////
there is one thing that really bugs me, it is that when there is pictures like .jpg .png it really messes up things. You can test how it messes things up by catting .jpg or .png images.
Code:
cat /path/to/some.jpg
Is there some non printable characters or something similar (binary data?) messing up things
What "things" it messes up depends on your terminal. To stay with your example, see how the following doesn't mess things up:
Code:
cat -v /path/to/some.jpg
I see you found a decent solution. Occasionally it may produce ambiguous output; e.g. "bravest" may have been "bra vest" (with any non-printable in between).
 
Old 04-04-2007, 04:51 PM   #4
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824

Original Poster
Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
Thanks for hints, I think I now understand that my problem was/is terminal control characters, am I right ?

EDIT: Ok, here is finished product. It preserves linefeeds and indentation too
Code:
s/[\x00-\x08\x0B-\x1F\x7F-\xFF]//g

Last edited by //////; 04-04-2007 at 07:11 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
display in hex + perl + non ASCII characters kshkid Programming 4 02-06-2007 04:48 PM
using perl to insert special characters into mysql database ihopeto Programming 5 12-25-2006 04:08 PM
Convert characters to hex values in perl pjz Programming 3 12-08-2005 08:35 AM
printing Spanish accent characters in Perl/tk entry widget puedo Programming 1 02-19-2005 01:16 PM
extracting first n characters of a string in perl ananthbv Programming 4 09-14-2004 07:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 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
Open Source Consulting | Domain Registration