I'm trying to echo a variable which is defined in Linux (Centos 6.3).
For accessing the server I use phpseclib 2.0.
When accessing using PuTTY (or similar), with vfrepc86 user, i'm getting the following output:
vfrepc86#illin935!:vfrepc86> echo $USER
vfrepc86
vfrepc86#illin935!:vfrepc86> pwd
/vfuser1/vfr/abp/vfrepc86
vfrepc86#illin935!:vfrepc86> echo $WL_HOME
/opt/weblogic1211_new/wlserver_12.1
When I try running the same with PHP (connecting with the same user vfrepc86), using the following code:
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('phpseclib/Net/SSH2.php');
$ssh = new Net_SSH2('illin935');
if (!$ssh->login('vfrepc86', '******')) {
exit('Login Failed');
}
echo "---User:---";
echo "<br>";
echo $ssh->exec('echo $USER');
echo "<br><br>";
echo "---Location of run:---";
echo "<br>";
echo $ssh->exec('pwd');
echo "<br><br>";
echo "---Default:---";
echo "<br>";
echo $ssh->exec('echo $WL_HOME'); //my first try, returns nothing
echo "<br><br>";
echo "---Using su:---";
echo "<br>";
echo $ssh->exec('su - vfrepc86 -c \'echo $PATH\''); //tried with su
echo "<br><br>";
echo "---Writing to file:---";
echo "<br>";
echo $ssh->exec('echo $WL_HOME >> temp.txt'); //tried writing to file
?>
I get the following:
---User:---
vfrepc86
---Location of run:---
/vfuser1/vfr/abp/vfrepc86
---Default:---
---Using su:---
standard in must be a tty
---Writing to file:---
You can see I'm not able to get the $WL_HOME. Though the script is running from the same path. As seen above, I've also tried using su but that returns standard in must be a tty. Printing the command to a file doesnt help, as the file gets empty.
NET_SSH2 does not request an interactive shell, so your .profile doesn't get executed. That's why $WM_HOME is not defined.
Instead of phpseclib, use the PECL ssh2 extension, it has a function ssh2_shell that requests an interactive shell.
Two solutions.
Enable a PTY ($ssh->enablePTY()) and then do $ssh->exec('command'). More info: http://phpseclib.sourceforge.net/ssh/pty.html
Use an interactive shell. eg. $ssh->read('[prompt]'); $ssh->write("command\n"); echo $ssh->read('[prompt]');. More info: http://phpseclib.sourceforge.net/ssh/examples.html#interactive
Related
I need to be able to create an archive(zip) file with password protection using PHP. I am using Laravel 5.4 and PHP 7.1 version. I looked at this link here for ZipArchive class documentation in PHP. I also looked at here for setPassword function. But appears that creation of password protected archives is not supported. It will be a massive surprise for me if it is not possible to create password protected archive in a mature programming language such as PHP 7.1.
So I guess I must be missing something. Can someone point me to the right direction? E.g. a sample example or open source third party library or extension to achieve this will be greatly appreciated.
Easy peasy lemon squeezy (no).
Yes, creation of password protected archives is not supported (they will be created simply as non-protected archives, as you just described).
But, still it can be used to extract password protected archives.
Returning to the problem.
You always can just
<?php echo system('zip -P pass file.zip file.txt'); ?>
(this will work both on Windows and our beloved Linux)
But, if it not fits into your requirements, let's continue.
I would suggest you to use DotNetZip (Windows only), you will exactly dynamically generate AES-encrypted zip archives from PHP.
<?php
// origin: https://stackoverflow.com/a/670804/3684575
try
{
$fname = "zip-generated-from-php-" . date('Y-m-d-His') . ".zip";
$zipOutput = "c:\\temp\\" . $fname;
$zipfact = new COM("Ionic.Zip.ZipFile");
$zip->Name = $zipOutput;
$dirToZip= "c:\\temp\\psh";
# Encryption: 3 => 256-bit AES.
# 2 => 128-bit AES.
# 1 => PKZIP (Weak).
# 0 => None
$zip->Encryption = 3;
$zip->Password = "AES-Encryption-Is-Secure";
$zip->AddDirectory($dirToZip);
$zip->Save();
$zip->Dispose();
if (file_exists($zipOutput))
{
header('Cache-Control: no-cache, must-revalidate');
header('Content-Type: application/x-zip');
header('Content-Disposition: attachment; filename=' . $fname);
header('Content-Length: ' . filesize($zipOutput));
readfile($zipOutput);
unlink($zipOutput);
}
else
{
echo '<html>';
echo ' <head>';
echo ' <title>Calling DotNetZip from PHP through COM</title>';
echo ' <link rel="stylesheet" href="basic.css"/>';
echo ' </head>';
echo '<body>';
echo '<h2>Whoops!</h2>' . "<br/>\n";
echo '<p>The file was not successfully generated.</p>';
echo '</body>';
echo '</html>';
}
}
catch (Exception $e)
{
echo '<html>';
echo ' <head>';
echo ' <title>Calling DotNetZip from PHP through COM</title>';
echo ' <link rel="stylesheet" href="basic.css"/>';
echo ' </head>';
echo '<body>';
echo '<h2>Whoops!</h2>' . "<br/>\n";
echo '<p>The file was not successfully generated.</p>';
echo '<p>Caught exception: ', $e->getMessage(), '</p>', "\n";
echo '<pre>';
echo $e->getTraceAsString(), "\n";
echo '</pre>';
echo '</body>';
echo '</html>';
}
?>
But still, this is very dirty solution and more of that, not works on Linux.
So, although PHP is a mature language, there is no adequate method (excluding custom extension or something like that) to achieve such a simple task with pure PHP.
What you also can do, is to wait until PHP 7.2 will be available for production (cuz ZipArchive::setEncryptionName is implemented (thanks to Pierre and Remi)).
But, until then you also can try to port php_zip >= 1.14.0 to PHP < 7.2, but there is currently no compiled binaries available, so you have to compile it yourself and try if it is possible at all (I believe it is).
p.s. I would try it, but have no VS2015+ on my PC right now.
I have python file which is newtry.py and this is my code:
print ("hello world")
I also have php file which is importKeyword.php and this is my code:
<?php
$python = `python newtry.py`;
echo $python;
echo "yes";
?>
I want to print "hello world" from python in the browser but it only print "yes" which is from php file. I have look at this solution which is using backquote operator ( enter link description here ) and wondering why I can't make it.
You can use exec function
exec('python newtry.py', $output);
var_dump($output);
use 2>&1 to redirect the output
<?php
exec("python newtry.py 2>&1", $python);
print_r($python);
echo "yes";
?>
i need to call a variable ($id) within a php system command. I'm automating a solr curl delete command, and it works with a static id, but i need to echo the $id so it deletes the correct document.
code is:
<?php
echo '<pre>';
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:<?php echo $id; ?></query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
The code shows no error but the document is not deleted so it is not grabbing the $id variable. Its an echo within a php system command. How to make it work?
You can't use <?php echo $id; ?> inside a string. That only works when you're out of PHP execution mode. Use string concatenation.
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:' . $id . '</query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);
Remove php tags and enclose a variable in single quotes in your system call. Now your variable along with php tags is passed as a string.
so update this line:
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:<?php echo $id; ?></query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);
to:
$last_line = system('curl http://localhost:8983/solr/imagedb/update?commit=true --data \'<delete><query>id\:'.$id.'</query></delete>\' -H \'Content-type:text/xml; charset=utf-8\'', $retval);
I'm using Slim framework for my project. I've copied the Slim folder to my project directory.
No following is the code I'm having issue with :
PHP Code(requestdemo.php):
<?php
require 'Slim/Slim.php';
/* Invoke the static "registerAutoloader()" function defined within Slim class.
* Register the autoloader is very important.
* Without doing it nothing will work.
*/
\Slim\Slim::registerAutoloader();
//Instantiate Slim class in order to get a reference for the object.
$application = new \Slim\Slim();
$application->get(
'/request',
function()
{
GlOBAL $application;
echo " <br/><b>request methods</b>";
echo "<br/>application->request->getMethod()=".$application->request->getMethod();
echo "<br/>application->request->isGet()=".$application->request->isGet();
echo "<br/>application->request->isPost()=".$application->request->isPost();
echo "<br/>application->request->isPut()=".$application->request->isPut();
echo "<br/>application->request->isDelete()=".$application->request->isDelete();
echo "<br/>application->request->isHead()=".$application->request->isHead();
echo "<br/>application->request->isOptions()=".$application->request->isOptions();
echo "<br/>application->request->isPatch()=".$application->request->isPatch();
echo "<br/>application->request->isAjax()=".$application->request->isAjax();
echo "<br/> <br/><b>request headers</b>";
$headers = $application->request->headers;
foreach($headers as $k=>$v)
{
echo "<br/>$k => $v";
}
echo "<br/> <br/><b>request body</b>";
echo "<br/>body=".$application->request->getBody();
echo "<br/> <br/><b>request variables</b>";
echo "<br/>width=".$application->request->params('width');
echo "<br/>height=".$application->request->params('height');
echo "<br/> <br/><b>request get variables</b>";
echo "<br/>width=".$application->request->get('width');
echo "<br/>height=".$application->request->get('height');
echo "<br/> <br/><b>request post variables</b>";
echo "<br/>width=".$application->request->post('width');
echo "<br/>height=".$application->request->post('height');
echo "<br/> <br/><b>resource uri</b>";
/*From the below line I'm not able to see the output in a browser.*/
echo "<br/>rootUri=".$application->request->getUri();
echo "<br/>resourceUri=".$application->request->getResourceUri();
echo "<br/> <br/><b>request ajax check</b>";
echo "<br/>rootUri=".$application->request->isAjax();
echo "<br/>resourceUri=".$application->request->getResourceUri();
echo "<br/> <br/><b>request helpers</b>";
echo "<br/>content type=".$application->request->getContentType();
echo "<br/>media type=".$application->request->getMediaType();
echo "<br/>host=".$application->request->getHost();
echo "<br/>scheme=".$application->request->getScheme();
echo "<br/>path=".$application->request->getPath();
echo "<br/>url=".$application->request->getUrl();
echo "<br/>user agent=".$application->request->getUserAgent();
});
$application->run();
?>
The file 'requestdemo.php' is present in the directory titled "slimsamples" at location /var/www/slimsamples
As I hit the URL 'http://localhost/slimsamples/requestdemo.php/request' I'm able to see only the part of output in a browser window. From where I'm not able to see the output I've commented in my code. I'm not able to see the output after line resource uri. See the screenshot for further understanding.
Also there is no syntactical error in it then why it's happening I'm not understanding.
Can someone please find out the mistake I'm making here?
Thanks in advance.
Use: request->getUrl()
(You used request->getUri())
See http://dev.slimframework.com/phpdocs/classes/Slim.Http.Request.html#getUrl
I need to capture the output of a console command to be sent by email as well when requested. How can I do this?
How do I get the output generated from the following $this->info() calls?
$r = processData();
$this->info("\nSubmitted data:");
$this->info("SubmissionId: " . $r['submission_id']);
$this->info("Status: " . $r['status']);
Decided to just replace the $this->info() calls with a simple echo command and output buffer control. Looks good enough in the console and catches the data requested for emailing.
Example:
$r = processData();
if ($this->option('email-results'))
ob_start();
echo "\nSubmitted data:";
echo "\nSubmissionId: " . $r['submission_id'];
echo "\nStatus: " . $r['status'];
if ($this->option('email-results')) {
mail(
$this->option('email-results'),
'Results on ' . $start_time->toDateTimeString(),
ob_get_contents()
);
ob_end_flush();
}
an Artisan method could help:
\Illuminate\Support\Facades\Artisan::output()