I am trying to develop a web console using php and xterm.js,
I managed to get the pseudo tty allocated and attach it to xterm.js via websocket but I am not able to tell the process what is the size of terminal to make it work correctly with the size, and I couldn't find any documentation for this.
// using react/child-process
$process = new Process('/usr/bin/env bash -l', null, null, [
0 => ['pty', 'r'],
1 => ['pty', 'w'],
2 => ['pty', 'w'],
]);
$process->start($this->loop);
I found out run stty on the allocated devpts will do the tricks
if (isset($this->processes[spl_object_id($conn)])) {
$process = $this->processes[spl_object_id($conn)];
$data = unpack('i3', $raw);
$col = $data[2];
$row = $data[3];
$getstream = (function () {
return $this->stream;
});
$stty = new Process("stty cols $col rows $row", null, null, [
$getstream->call($process->stdin), // ex: /dev/pts/0
$getstream->call($process->stdout),
$getstream->call($process->stderr),
]);
$stty->start($this->loop);
}
alternatively can do this
use React\ChildProcess\Process;
$gettty = escapeshellcmd(PHP_BINARY).' -r "echo posix_ttyname(STDIN).PHP_EOL;"';
$bash = "setsid bash -l"; // setsid is important here
$process = new Process(
"$gettty && $bash",
null, null,
[
['pty', 'r'],
['pty', 'w'],
['pty', 'w'],
]
);
// TODO: read the first line and get the tty path so can run stty on it later.
Here is a simple example:
https://gist.github.com/eslym/d3bd7809681aa9c1eb34913043df9bb6
Related
I'm trying to execute ffmpeg commands using Symfony Process Component but command is not being processed. What am I doing wrong?
I get the error
The command "'ffmpeg -i [...........]' failed. Exit Code: 127(Command
not found)"
<?php
$info = pathinfo($file);
$dir = "{$info['dirname']}/{$info['filename']}";
File::makeDirectory($dir, 0755, true)
$process = new Process(["ffmpeg -i {$info['basename']} -codec copy -map 0 -f segment -segment_list {$dir}/playlist.m3u8 -segment_list_flags +live -segment_time 10 {$dir}/{$info['filename']}_%02d.ts"]);
$process->setWorkingDirectory($info['dirname']);
$process->start();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
?>
You have to put each argument in a separate element of the array, for example:
$process = new Process([
"ffmpeg",
"-i",
"{$info['basename']}",
"-codec",
"copy",
"-map",
"0",
"-f",
"segment",
"-segment_list",
"{$dir}/playlist.m3u8",
"-segment_list_flags",
"+live",
"-segment_time",
"10",
"{$dir}/{$info['filename']}_%02d.ts",
]);
And I think you should either:
use $process->run() instead of $process->start()
or read in more detail how to run a process asynchronously with $process->start()
I'm trying to convert this piece of PHP code to Python. I've figured out how to do the crc32b hash but I'm stuck on proc_open(). Could you help me out?
What I want is to get the value of $pipeOutput in Python code.
PHP
$hashedData = strrev(hash("crc32b", $data, true)) . $data;
$xzBinary = trim(`which xz`);
$xzProcess = proc_open("$xzBinary '--format=raw' '--lzma1=lc=3,lp=0,pb=2,dict=128KiB' '-c' '-'", [
0 => [
"pipe",
"r"
],
1 => [
"pipe",
"w"
]
], $xzProcessPipes);
fwrite($xzProcessPipes[0], $hashedData);
fclose($xzProcessPipes[0]);
$pipeOutput = stream_get_contents($xzProcessPipes[1]);
fclose($xzProcessPipes[1]);
proc_close($xzProcess);
Python
import zlib
from subprocess import Popen
def crc32b(x):
h = zlib.crc32(x)
x='%08X' % (h & 0xffffffff,)
return x.lower()
data = 'some_data'
hashed_data = crc32b(data)[::-1]+data #strrev(hash("crc32b", $data, true)) . $data;
xz_binary = '//usr/bin/xz'
r = Popen([xz_binary, "--format=raw",'--lzma1=lc=3,lp=0,pb=2,dict=128KiB','-c','-'])
EDIT
hashed_data = crc32b(data)[::-1]+data
xz_binary = '//usr/bin/xz'
p = Popen([xz_binary, "--format=raw",'--lzma1=lc=3,lp=0,pb=2,dict=128KiB','-c','-'],stdin=PIPE, stdout=PIPE,stderr=PIPE)
p.communicate()
stdout_file = p.stdout
stdin_file = p.stdin
stdin_file.write(hashed_data)
says that stdin_file is closed but I'm not sure if this is what I should do
ValueError: I/O operation on closed file
I am trying to build a ssh script to run multiple commands on a host. My goal is get the output of each command. My target host is a cisco router and for the following script to execute more that one command i need to run it for each command i want to execute which is not a very elegant solution.
$cmd = array ('sh run int te 1/1', 'sh run int te 1/2');
for ($i = 0; $i <= 1; $i++) {
$connection = ssh2_connect('10.1.1.1', 22);
ssh2_auth_password($connection, 'user', 'pass');
$stream = ssh2_exec($connection, $cmd[$i]);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out); }
I have created a loop because i was unable to get the output for each command in the same stream. Since i guess that the php is terminating the ssh connections at the end of each stream.
What i would like to achieve is to execute several commands and get the output in the same stream (if posible).
I am editing the post to answer the sugestions made by #Melvin Koopmans and #LSerni.
if i change the code as sugested (this was someting i also tried before) the second command returns an error. here is the cli output:
the script changed:
$cmds = array ('sh run int te 1/1', 'sh run int te 1/2');
$connection = ssh2_connect('10.1.1.1', 22);
ssh2_auth_password($connection, 'user', 'pass');
foreach ($cmds as $cmd) {
$stream = ssh2_exec( $connection, $cmd );
stream_set_blocking( $stream, true );
$stream_out = ssh2_fetch_stream( $stream, SSH2_STREAM_STDIO );
echo stream_get_contents($stream_out);}
the output from cli
interface TenGigabitEthernet1/1
description trunk
switchport trunk allowed vlan 1,2,3,4,5,6,10
switchport mode trunk
auto qos trust
storm-control broadcast include multicast
storm-control broadcast level 1.00
spanning-tree guard loop
service-policy input AutoQos-4.0-Input-Policy
service-policy output AutoQos-4.0-Output-Policy
ip dhcp snooping trust
end
PHP Warning: ssh2_exec(): Unable to request a channel from remote host in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 13
PHP Warning: stream_set_blocking() expects parameter 1 to be resource, boolean given in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 14
PHP Warning: ssh2_fetch_stream() expects parameter 1 to be resource, boolean given in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 15
PHP Warning: stream_get_contents() expects parameter 1 to be resource, null given in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 16
I am only getting the output from the first command "sh run int te 1/1".
You are repeating the connection stage. Try this instead:
$cmds = array ('sh run int te 1/1', 'sh run int te 1/2');
$connection = ssh2_connect('10.1.1.1', 22);
ssh2_auth_password($connection, 'user', 'pass');
foreach ($cmds as $cmd) {
$stream = ssh2_exec($connection, $cmd);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);
}
I tested now on a Ubuntu machine (14.04-LTS), and it works:
I say 'who', answer was: lserni :0 Jan 21 11:25 (console)
I say 'date', answer was: Thu Feb 16 09:55:52 CET 2017
...well, apart from the fact that I forgot an open console login on that machine :-(
I wouldn't recommend initiating a new connection on every single loop, instead set the connection first then loop over an array of commands and push the output to an array. Like so:
$cmds = [ 'ls', 'ps ux' ];
$connection = ssh2_connect( '127.0.0.1', 22 );
ssh2_auth_password( $connection, 'username', 'password' );
$output = [];
foreach ($cmds as $cmd) {
$stream = ssh2_exec( $connection, $cmd );
stream_set_blocking( $stream, true );
$stream_out = ssh2_fetch_stream( $stream, SSH2_STREAM_STDIO );
$output[] = stream_get_contents($stream_out);
}
This will push all of the output to the array $output.
Now you can loop over $output, or you can choose to give output the key of yoru command so you can access it:
$output[$cmd] = stream_get_contents($stream_out);
And then, for example, call: $output['ls']
I have installed youtube-dl on my CentOS 6 / Plesk 10 Dedicated server, and via SSH, everything works like a charm !
The thing is I'm trying to write a php script which takes a POST parameter in containing the URL of the video I want to copy to the server, copy it and then process it with ffmpeg.
I thought I would use the exec() function.
I can get an output if I echo the results of youtube-dl --help, but everytime I ask php to execute a command which actually does something with the video, it returns a status of '1', and outputs nothing.
Any ideas on what I'm doing wrong ?
Here is my php code:
<?php
$result = array();
$status;
$url = $_POST['src'];
$string = 'youtube-dl "'.$url.'" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s"';
$string2 = 'youtube-dl --help';
exec($string, $result, $status);
echo json_encode(array('status' => $status, 'url_orginal'=>$url, 'url' => $result));
?>
When I execute $string2, I get status: "0" and "url": [youtube-dl help text lines]
But when I execute $string, nothing happens, I get "status": "1" and nothing else, no video is downloaded. I've tried also a simulation with the "-g" parameter, and variants but as soon as youtube-dl has to fetch the video, it breaks.
Thank you in advance !
EDIT
I edited my code so it looks like this :
<?php
$result = array();
$status;
$url = $_POST['src'];
$string = 'youtube-dl "'.$url.'" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s"';
$string2 = 'youtube-dl --help';
exec($string, $result, $status);
echo json_encode(array('status' => $status, 'url_orginal'=>$url, 'url' => $result, 'command' => $string));
?>
and the result, which I didn't get yesterday now is :
command: "youtube-dl "http://www.youtube.com/watch?v=coq9klG41R8" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s""
status: 1
url:
0: "[youtube] Setting language"
1: "[youtube] coq9klG41R8: Downloading video info webpage"
2: "[youtube] coq9klG41R8: Extracting video information"
url_orginal: "http://www.youtube.com/watch?v=coq9klG41R8"
Which is weird, considering a) that yesterday I got an empty url[], and that b) even if now I get what apparently looks like a normal youtube-dl return, it only contains the 3 first lines, and I cannot see any video file in the specified path... Any ideas ?
exec reads only stdout, so you're missing the error message on stderr, whatever that may be.
Use the following code to get stderr as well:
$url = 'http://www.youtube.com/watch?v=coq9klG41R8';
$template = '/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/' .
'downloads/%(id)s.%(ext)s';
$string = ('youtube-dl ' . escapeshellarg($url) . ' -f 18 -o ' .
escapeshellarg($template));
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
$process = proc_open($string, $descriptorspec, $pipes);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$ret = proc_close($process);
echo json_encode(array('status' => $ret, 'errors' => $stderr,
'url_orginal'=>$url, 'output' => $stdout,
'command' => $string));
Most likely, you don't have permission to write to that directory. To test that theory, check whether
touch /var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/test
works. You can use chmod and chown to change permissions.
I'm trying to write a helper script for doing various admin tasks on a server which can only be done from the command line and trying to use the 'dialog' command to display message boxes, inputs, password prompts etc, however, the needs of this task call for me to process the data in PHP.
I'm having problems getting the dialog command to work in this way and can't figure out what i'm doing wrong.
There's an example here
Unfortunately it doesn't work.
When you run PHP and exec/backtick/system to an external application, the IO doesn't appear to work how you'd expect.
The nearest I can get is using the passthru() command:
<?php
$CMD = "dialog --menu \"Please select\" 10 40 3 backup \"Backup Files\" restore \"Restore Files\"";
passthru($CMD);
?>
This is the only way that PHP will let dialog use the STDOUT properly, anything else results in no display but you can press return to select an option.
I've tried backticks, exec() and system() but nothing seems to work.
What I wondered was how to read STDERR properly from within PHP to get the return value into a variable called $result.
I'm sure some other sysadmins have had to do this before.
My reasons for not using bash for this are that one command I have to execute as a result of a selection produces XML output only and I can't parse that effectively in bash.
Just in case someone else is searching for this:
function dialog ($args) {
$pipes = array (NULL, NULL, NULL);
// Allow user to interact with dialog
$in = fopen ('php://stdin', 'r');
$out = fopen ('php://stdout', 'w');
// But tell PHP to redirect stderr so we can read it
$p = proc_open ('dialog '.$args, array (
0 => $in,
1 => $out,
2 => array ('pipe', 'w')
), $pipes);
// Wait for and read result
$result = stream_get_contents ($pipes[2]);
// Close all handles
fclose ($pipes[2]);
fclose ($out);
fclose ($in);
proc_close ($p);
// Return result
return $result;
}
It requires dialog (apt-get install dialog) and proc_xxx (PHP 4.3.0, PHP 5)
It works, at least for me. :)
You can use proc_open() but not as show above... All dialog boxes do not work in same way. I provides a concrete sample below:
#!/usr/bin/env php
<?php
$pipes = array();
$process = null;
$output = '';
$ret = -1;
/**
* Start process
*
* #param string $cmd Command to execute
* #param bool $wantinputfd Whether or not input fd (pipe) is required
* #retun void
*/
function processStart($cmd, $wantinputfd = false)
{
global $process, $pipes;
$process = proc_open(
$cmd,
array(
0 => ($wantinputfd) ? array('pipe', 'r') : STDIN, // pipe/fd from which child will read
1 => STDOUT,
2 => array('pipe', 'w'), // pipe to which child will write any errors
3 => array('pipe', 'w') // pipe to which child will write any output
),
$pipes
);
}
/**
* Stop process
*
* #return void
*/
function processStop()
{
global $output, $pipes, $process, $ret;
if (isset($pipes[0]) {
fclose($pipes[0]);
usleep(2000);
}
$output = '';
while ($_ = fgets($pipes[3])) {
$output .= $_;
}
$errors = '';
while ($_ = fgets($pipes[2])) {
fwrite(STDERR, $_);
$errors++;
}
if ($errors) {
fwrite(STDERR, "dialog output the above errors, giving up!\n");
exit(1);
}
fclose($pipes[2]);
fclose($pipes[3]);
do {
usleep(2000);
$status = proc_get_status($process);
} while ($status['running']);
proc_close($process);
$ret = $status['exitcode'];
}
// Test for yesno dialog box
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --yesno 'yesno dialog box' 0 70");
processStop();
echo "Exit code is $ret\n";
// Test for gauge dialog box
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --gauge 'Gauge dialog box' 0 70 0", true);
sleep(1);
fwrite($pipes[0], "XXX\n0\nFirst step\nXXX\n20\n");
sleep(1);
fwrite($pipes[0], "XXX\n20\nSecond step\nXXX\n50\n");
sleep(1);
fwrite($pipes[0], "XXX\n50\nThird step\nXXX\n80\n");
sleep(1);
fwrite($pipes[0], "XXX\n80\nFourth step\nXXX\n100\n");
sleep(1);
processStop();
echo "Exit code is $ret\n";
// Test for input dialog box
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --inputbox 'input dialog box' 0 70");
processStop();
echo "Output is $output\n";
echo "Exit code is $ret\n";
// Test for errors output
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --dummy 'my input box' 0 70");
processStop();
exit(0);
You can use proc_open() to run a command and interact on all pipes, STDIN, STDOUT and STDERR:
$pipes = array(NULL, NULL, NULL);
$proc = proc_open(
"dialog --gauge ..",
array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w'),
),
$pipes
);
print fgets($pipes[2]);
See the manual for more examples.
PHP-GTK looks like a solution for this problem
http://gtk.php.net/
I think you can't run ncurses application through PHP like this - maybe you should take a look at this: http://php.net/manual/de/book.ncurses.php