LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-28-2020, 08:17 PM   #1
voncloft
Member
 
Registered: Mar 2019
Posts: 42

Rep: Reputation: Disabled
Question Remove "weird" text in cgi after running command.


I wrote a cgi script and it is working - however is there a way to get rid of some weird text during the run?

the code:
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
use 5.010;

use Data::Dumper;
use CGI;
my $q = CGI->new;

my %data;
$data{url} = $q->param('url');
$data{ip} = $q->param('ip');

print $q->header;

print "URL: $data{url} <br>";
print "IP: $data{ip}";
my $filename = '/srv/www/ip/text/hosts-file';
open my $fh, ">>", "$filename" or die "nope" ;
print $fh "$data{ip} $data{url} \n";

my $filename = '/srv/www/ip/text/hosts-ip';
open my $fh, ">>", "$filename" or die "nope" ;
print $fh "$data{ip} 0; \n";


system('sudo /etc/init.d/dhcpd restart');
system('sudo /etc/init.d/psad restart');
system('sudo /etc/init.d/hostapd restart');
After it runs it has the following output:
Code:
URL: woot
IP: 192.168.1.2 Stopping dnsmasq [0G[1;32m * [0;39m[-8G[1;34m[[1;32m OK [1;34m][0;39m Starting dnsmasq [0G[1;32m * [0;39m[-8G[1;34m[[1;32m OK [1;34m][0;39m
I'd like it to be:
Code:
URL: woot
IP: 192.168.1.2 
Stopping dnsmasq OK
Starting dnsmasq OK
Is this possible? webmin seems to have nailed this down, but frankly I am brand new to CGI - first day, and yes I know running root is a risk to my system....(I don't want to hear the ethics behind it, its my system, all this is behind a closed port only available to my lan - the outside world can't get in)

Me and google are having a translation problem, I could really use some human help on the final piece of the puzzle.

Thanks in advance

Last edited by voncloft; 07-28-2020 at 08:22 PM.
 
Old 07-28-2020, 11:03 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,062

Rep: Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470
In shell, it would be
Code:
unset TERM
In Perl
Code:
delete $ENV{TERM};

Last edited by NevemTeve; 07-28-2020 at 11:12 PM.
 
Old 07-29-2020, 02:14 PM   #3
voncloft
Member
 
Registered: Mar 2019
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
In shell, it would be

Code:
delete $ENV{TERM};
That still shows the ANSI color codes
 
Old 07-29-2020, 04:08 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 4,953

Rep: Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830
Capture the output of the restart commands and parse as you want before printing.
 
Old 07-29-2020, 06:11 PM   #5
voncloft
Member
 
Registered: Mar 2019
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
In shell, it would be
Code:
unset TERM
In Perl
Code:
delete $ENV{TERM};
Quote:
Originally Posted by scasey View Post
Capture the output of the restart commands and parse as you want before printing.
I worked it out instead of using Sysvinit scripts I just ran the commands from the sysvinit in the cgi code in a bash script.
 
Old 07-30-2020, 01:15 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,062

Rep: Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470
Well, the scripts shouldn't send terminal-control sequences it `TERM` variable is not set or `tty -s` returns non-zero.
 
Old 08-01-2020, 04:39 AM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 514

Rep: Reputation: 226Reputation: 226Reputation: 226
Quote:
Originally Posted by NevemTeve View Post
Well, the scripts shouldn't send terminal-control sequences it `TERM` variable is not set or `tty -s` returns non-zero.
Those are init script so they expect TERM not to be set and hard-code the control sequences since they know exactly what the terminal is – the Linux console.

Quote:
Originally Posted by voncloft View Post
Is this possible? webmin seems to have nailed this down, but frankly I am brand new to CGI - first day, and yes I know running root is a risk to my system....
Other ‘webmin’ interfaces don’t rely on init scripts to do their thing. Whatever needs to be done is programmed to them directly which might also allow for better error reporting.

In your case, ‘sed’ might get you an acceptable result:
Code:
system("sudo /etc/init.d/dhcpd restart | sed 's/\e\\[[^a-zA-Z]*[a-zA-Z]//g'");
Or it might be easier to capture the output and process it in Perl. Something like:
Code:
my $out = `sudo /etc/init.d/dhcpd restart`;
$out =~ s/\e\[a-zA-Z]*[a-zA-Z]//g;
print $out;
This will leave the asterisks behind.

Be the way, you should probably set Content-Type of the script to ‘text/plain’, i.e. ‘print $q->header('text/plain');’.
 
1 members found this post helpful.
Old 08-01-2020, 05:58 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,062

Rep: Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470Reputation: 1470
Yes, these scripts _usually_ run at system startup/shutdown, so the well-meaning coders thought it was okay to handle only that case and ignore other possible usages. (This is one of the things that make the difference between 'coder' and 'programmer'.)
 
Old 08-01-2020, 06:51 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 1,135

Rep: Reputation: Disabled
There are ways to switch off ANSI colors in init scripts.

On Debian-based systems:
Code:
export FANCYTTY=0
On Fedora-based systems:
Code:
export BOOTUP=other
 
2 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird strings in a text file: how to avoid/remove them? Sudharshana Linux - Newbie 12 02-19-2013 03:58 AM
[SOLVED] How can remove this weird chars from my text file? sopier Programming 4 12-21-2011 06:15 AM
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
Help With Java Problem Please"""""""""""" suemcholan Linux - Newbie 1 04-02-2008 06:02 PM
HP Photosmart weird weird weird.... Vlad_M Linux - General 5 02-20-2005 05:41 AM

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

All times are GMT -5. The time now is 01:35 PM.

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