LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Remove "weird" text in cgi after running command. (https://www.linuxquestions.org/questions/programming-9/remove-weird-text-in-cgi-after-running-command-4175679561/)

voncloft 07-28-2020 08:17 PM

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

NevemTeve 07-28-2020 11:03 PM

In shell, it would be
Code:

unset TERM
In Perl
Code:

delete $ENV{TERM};

voncloft 07-29-2020 02:14 PM

Quote:

Originally Posted by NevemTeve (Post 6150477)
In shell, it would be

Code:

delete $ENV{TERM};

That still shows the ANSI color codes

scasey 07-29-2020 04:08 PM

Capture the output of the restart commands and parse as you want before printing.

voncloft 07-29-2020 06:11 PM

Quote:

Originally Posted by NevemTeve (Post 6150477)
In shell, it would be
Code:

unset TERM
In Perl
Code:

delete $ENV{TERM};

Quote:

Originally Posted by scasey (Post 6150735)
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.

NevemTeve 07-30-2020 01:15 PM

Well, the scripts shouldn't send terminal-control sequences it `TERM` variable is not set or `tty -s` returns non-zero.

mina86 08-01-2020 04:39 AM

Quote:

Originally Posted by NevemTeve (Post 6151028)
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 (Post 6150442)
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');’.

NevemTeve 08-01-2020 05:58 AM

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'.)

shruggy 08-01-2020 06:51 AM

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


All times are GMT -5. The time now is 06:53 PM.