avoid password prompt - php

I want execute command sudo ls -l without password prompt.
$streams = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$process = proc_open('sudo ls -l', $streams, $pipes);
if (is_resource($process)) {
list($stdin, $stdout) = $pipes;
fwrite($stdin, "password\n"); //must write password, but not
fclose($stdin);
var_dump(stream_get_contents($stdout));
fclose($stdout);
$returnCode = proc_close($process);
echo "Return code $returnCode\n";
}
After execution password prompt still exists, how to avoid it ?

Have you tried to add the command ls to /etc/sudoers file ?
you can autorize to a user to execute some command without password.
user ALL= NOPASSWD: /bin/ls

Related

php exec grep -axv don't return anything

I'am stuck since days while requesting grep on PHP, it work in cli but don't return anything via http.
it search for files that contain non UTF-8 carachters
in CLI it retrun ퟿������ but nothing (array is null) from the web
<?php
exec("/sbin/grep -axv '.*' /srv/http/test 2>&1", $datareturn);
print_r($datareturn);
?>
disable_functions = is empty in php.ini
Also tried with proc_open :
<?php
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$process = proc_open(
"/sbin/grep -axv '.*' /srv/http/test",
$descriptorspec,
$pipes
);
if (is_resource($process)) {
// Closing $pipes[0] because we don't need it
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
//avoid a deadlock
$return_value = proc_close($process);
echo "command returned $return_value\n";
}
?>
In CLI it return :
"퟿������
command returned 0
From http, "command returned 1", error-output.txt is empty in 2 cases

How to read binary from pipe in PHP

I'm trying to create a PNG from an SVG using Inkscape. I'm using Linux. the command,
cat in.svg | inkscape -z /dev/stdin -w 800 -h 475 -e /dev/stderr 2> out.png
works fine, but I would rather not write the output files on the server.
My code is,
<?php
$svg_data = file_get_contents('in.svg');
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$pipes = array();
$process = proc_open("inkscape -z /dev/stdin -w 800 -h 475 -e /dev/stderr", $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $svg_data);
fclose($pipes[0]);
$fil_data = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
file_put_contents("out.png", $fil_data);
}
?>
If I change the line,
$process = proc_open("inkscape -z /dev/stdin -w 800 -h 475 -e /dev/stderr", $descriptorspec, $pipes);
to
$process = proc_open("inkscape -z /dev/stdin -w 800 -h 475 -e out.png", $descriptorspec, $pipes);
The correct "out.png" is printed.
The current code writes a file, but the file is corrupted. It seems that it is corrupted from beginning to end, but it is about the right size.
I want the data (out.png) in $fil_data, but I don't want to read it from the disk drive.
Why is out.png corrupted, and how can I make the correct conversion without resorting to writing the disk.
Inkscape was putting some messages to stderr before writing the file. The same thing happened on stdout. The problem was fixed by skipping past the messages. So the line,
$fil_data = stream_get_contents($pipes[2]);
was changed to,
$fil_data = stream_get_contents($pipes[2], -1, 150);
If there is a better way, I would like to see it.

Mysql not connecting when callng from proc_open in php

There is a Linux shell script which includes mysql connection and queries.
filename is backup.sh.
#!/bin/bash
set -x
user="root"
pass="dbpass"
output=`/usr/local/mariadb/bin/mysql -u$user -p$pass -h127.0.0.1 -e 'select now()' `
echo "data output=$output"
It will work if i run thorugh below linux command line.
sh backup.sh
But if i call from php code it will not work. It throughs permission denied error.
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "r")
);
$process = proc_open("sh /tmp/backup.sh", $descriptorspec, $pipes, null, null); if (is_resource($process))
{
//echo ("<br>Start process:<br>");
stream_set_blocking($pipes[2], 0);
while (!feof($pipes[1])) {
$out = fgets($pipes[1], 1024);
echo ''.$out.'';
}
}
Anybody help me pls.

Generate ZPL code with PHP and print to zebra chrome addon for testing?

Ok I downloaded the Chrome ZPL addon for testing with zebra printers. But now I like to print the following:
$qrcode = "012039444";
$name = "Matthew Pitt";
$jobtitle = "CTO funny man";
$company = "Google light company";
$labelcode =<<<AAA
^XA
^FX Left section with QR code.this part doesn't seem to work in simulator
^FO100,100
^BQN,2,10
^FD$qrcode^FS
^FX Right section with name and job title.
^CF0,35,35^FO330,50
^FB300,7,,
^FD$name\&\&$jobtitle^FS
^FO50,500^GB700,1,3^FS
^FX Bottom section only company name
^CF0,35,35^FO30,350
^FB400,2,,
^FD$company^FS
^XZ
AAA;
print_example($labelcode);
//print_example('Hello world');
function print_example($data) {
// You'll need to change these according to your local names and options.
$server = '127.0.0.1:9100';
$printer_name = 'zpl'; // That's effectively the same thing as your GK420d
$options_flag = '-o position=top-left,ppi=203,landscape';
$process_name = 'LC_ALL=en_US.UTF-8 /usr/bin/lp -h %s -d %s %s';
$command = sprintf($process_name, $server, $printer_name, (string)$options_flag);
$pipes = array();
$descriptors = array(
0 => array("pipe", "r"), // STDIN
1 => array("pipe", "w"), // STDOUT
2 => array("pipe", "w") // STDERR
);
$process = proc_open($command, $descriptors, $pipes);
// Couldn't open the pipe -- shouldn't happen
if (!is_resource($process))
trigger_error('Printing failed, proc_open() did not return a valid resource handle', E_USER_FATAL);
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// As we've been given data to write directly, let's kinda like do that.
fwrite($pipes[0], $data);
fclose($pipes[0]);
// 1 => readable handle connected to child stdout
$stdout = fgets($pipes[1]);
fclose($pipes[1]);
// 2 => readable handle connected to child stderr
$stderr = fgets($pipes[2]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
// We've asked lp not to be quiet about submitting jobs so we can make
// sure that the print job was submitted.
$request_match = array();
if (!preg_match('/request id is\b(.+)/', $stdout, $request_match)) {
echo ("Print to '$printer_name' failed. Please check the printer status.");
return false;
}
echo ("Print to '$printer_name' succeeded. Job $request_match[1].");
return true;
}
[EDIT]
is this the right command to call the printer
LC_ALL=en_US.UTF-8 /usr/bin/lp -h zpl -d 127.0.0.1:9100 -o position=top-left,ppi=203,landscape
[EDIT 2]
I am trying with this php
$command = 'lp -h 127.0.0.1:9100 -d zpl ' . $labelcode . ' -o position=top-left,ppi=203,landscape';
$pipes = array();
$descriptors = array(
0 => array("pipe", "r"), // STDIN
1 => array("pipe", "w"), // STDOUT
2 => array("pipe", "w") // STDERR
);
$process = proc_open($command, $descriptors, $pipes);
echo 'test: ' . $process;

Is it possible to get a file stream using exec with another command?

I need the file stream.
For example
private function faviconFoundExec($url)
{
exec('wget ' . $url . ' -O ../favicons/test.jpg 2>&1', $output);
}
will save the actual file, but I need the file stream, the same thing that file_get_contents would return as below.
private function faviconFoundGet($url)
{
return #file_get_contents( $url );
}
I'm looking at passthru but the documentation is a bit unclear.
You can get a stream from a command with proc_open
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
$cmd = 'wget -qO- ' . $url;
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
$contents = stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
proc_close($process);

Categories