PHP or Python on windows send command to remote Linux stack - php

I am developing a web application to send G-Code to the 3dprinter. This 3dprinter has a Linux stack. I could make it in Ubuntu application on Win by using the following command
ssh root#ip
password
python3 /usr/share/griffin/command_util.py
sendgcode G28
after typing python3 /usr/share/griffin/command_util.py,
it will retun(Cmd) type command here
I tried to use ssh2 in PHP, connection and authentication parts work. Besides,list,cd \usr\share\griffin && list also return the same result as what I get in windows cmd. But in the following code, after running the py file in linux stack, it only returns (Cmd). Nothing happened with sendgcode G28.
<?php
set_include_path('C:/xampp/htdocs/phpseclib1.0.15');
include('Net/SSH2.php');
$ssh = new Net_SSH2('ip');
if (!$ssh->login('root', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('python3 /usr/share/griffin/command_util.py && sendgcode G28');
?>
The following code in Python wouldn't receive any retun value
#
import paramiko
# realize transport
trans = paramiko.Transport(('ip', 22))
# connection
trans.connect(username='root', password='ultimaker')
# Specify the transport of the sshclient object as the above trans
ssh = paramiko.SSHClient()
ssh._transport = trans
# execute command
stdin, stdout, stderr = ssh.exec_command('python3 /usr/share/griffin/command_util.py \n sendgcode G28')
print(stdout.read().decode())
# close connection
trans.close()
The Linux stack shows that sendgcode command is undocumented. I don't understand why I could use it in Windows cmd but not in PHP or Python. Any help would be super appreciated!

Just delete '&&' in PHP or '\n' in Python, then it would work

Related

Laravel: TTY mode is not supported on Windows platform

I'm trying to use the "php artisan db" command and I'm getting this error. How can I fix it? I've already seen some similar problems, but none of them helped me.
PS C:\Users\Alisson\Documents\Programming stuff\study\first-laravel-crud> php artisan db
Symfony\Component\Process\Exception\RuntimeException
TTY mode is not supported on Windows platform.
at C:\Users\Alisson\Documents\Programming stuff\study\first-laravel-crud\vendor\symfony\process\Process.php:1059
1055▕ */
1056▕ public function setTty(bool $tty)
1057▕ {
1058▕ if ('\\' === \DIRECTORY_SEPARATOR && $tty) {
➜ 1059▕ throw new RuntimeException('TTY mode is not supported on Windows platform.');
1060▕ }
1061▕
1062▕ if ($tty && !self::isTtySupported()) {
1063▕ throw new RuntimeException('TTY mode requires /dev/tty to be read/writable.');
1 C:\Users\Alisson\Documents\Programming stuff\study\first-laravel-crud\vendor\laravel\framework\src\Illuminate\Database\Console\DbCommand.php:41
Symfony\Component\Process\Process::setTty()
2 C:\Users\Alisson\Documents\Programming stuff\study\first-laravel-crud\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
Illuminate\Database\Console\DbCommand::handle()
The real answer is to use Linux for development, which is (I assume) fairly easily done using Windows' built-in facilities.
But, all php artisan db does is call the MySQL command line, which you can do yourself. Check your .env file to get values for the host, username, and password. Then fill them into this command:
mysql -h host -u username -ppassword
Note the missing space after -p is intentional.

Command works in CMD prompt but not when sent via SSH2 with phpseclib

Im using phpseclib which id included with the command:
composer require phpseclib/phpseclib ~2.0
The target machine (receiver) is running Windows Server 2012.
On the target machine, I can run the following command in the cmd prompt and it works perfectly:
c:/maestro/__main__.py --process-report hourly_visitor -test
However, if I send that same command to the machine via SSH2, I get a strange error namely __main__.py: error: unrecognized arguments: -testh
Note the h in -testh. That's NOT in the sent command. Also, that extra letter seems to change, in a previous log entry, I saw:
__main__.py: error: unrecognized arguments: -testt with an extra t on the end but always a single letter added to the end.
I don't get it, what the heck is happening here?
Here is my php function:
public function triggerReport($config,$command)
{
$host = array_get($config,'host');
$username = array_get($config,'username');
$password = array_get($config,'password');
$ssh = new SSH2($host);
if (!$ssh->login($username, $password)) {
Log::error('Login Failed while attempting to trigger report with command: '.$command);
}
else{
$output = $ssh->exec($command);
Log::info('Output from running report with command: '.$command);
Log::info($output);
}
}
1st log output
Output from running report with command: C:\Python27\python.exe c:/maestro/__main__.py --process-report hourly_visitor -test
2nd log output:
usage: __main__.py [-h] --process-report [REPORT_TRIGGER] [-test]
[--to TO [TO ...]]
__main__.py: error: unrecognized arguments: -testh

Command works in SSH via MobaXterm, but not found in SSH via PHP?

I'm trying to connect to a server in my PHP code via SSH (using the Net_SSH2 class), and run a number of commands on the remote server.
The remote server outputs bash: runmqsc: command not found.
The odd thing is, that the exact same command is found and functional on that server, when I SSH to it using MobaXterm.
I validated that the code uses the correct hostname, user and password to connect.
Any ideas?
This is what I'm doing in PHP:
$this->ssh = new Net_SSH2("myhost");
$this->ssh->login("myuser", "mypass");
$command = "runmqsc MyQmgr \n DEFINE QLOCAL(MY_QUEUE) \n end \n";
$this->ssh->exec($command);
You probably need to do something more like this:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->read('username#username:~$');
$ssh->write("runmqsc MyQmgr\n"); // note the "\n"
echo $ssh->read('username#username:~$'); // or whatever the prompt is - does runmqsc have it's own prompt?
$ssh->write("DEFINE QLOCAL(MY_QUEUE)\n");
?>
...etc...

GearmanClient::do(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed in php shell_exec

I am trying to run gearman client from php shell_exec but it always throw following error GearmanClient::do(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed to send server-options packet -> libgearman/connection.cc:485 in /var/www/html/client.php
But if i run it from terminal then it works but not from php shell_exec. Even i passed server name and port in addServer method
I am running on centos 6.2.
Client.php
$client= new GearmanClient();
$client->addServer('127.0.0.1',4730);
print $client->do("reverse","Testing");
worker.php
$worker= new GearmanWorker();
$worker->addServer("127.0.0.1",4730);
$worker->addFunction("reverse", "my_reverse_function");
function my_reverse_function($job)
{
return strrev($job->workload());
}
Make sure your gearman packages are installed. Or manually installed if needed. Sometimes upgrading your system may cause your packages to be lost.
Second check your or launch it in debug mode as stated here http://gearman.org/getting-started/
gearmand -vvv
and then launch it run in the background
gearmand -d
I read on other questions treated to this matter that changes from
$woker->addServer('127.0.0.1',4730);
to
$worker->addServer('127.0.0.1:4730');
The same for $client->addServer('127.0.0.1',4730);

ssh access to ec2 from php

From local ubuntu linux machine connecting ec2 machine by ssh access.
when i run this php script from terminal it executes fine and write the tailed entries in file. when i run from browser got this error in apache error log
ssh: Could not resolve hostname proxy2: Name or service not known
Found its due to apache user permission problem. whether my guess is right or wrong am not sure. Any one helps me to fix this issue.
php code:-
<?php
$ss = 'ssh proxy2 '.'tail -n 3 /out/speed_log.txt.1'.' > proxy2temp1';
system($ss);
?>
**Finally found an solution using phpseclib and solved my problem. am recommend phpseclib to connect amazon ec2 machines from php uing .pem file to help others share my sample code.
make sure .pem file needs permission to read**
sample code:
include('Net/SSH2.php');
include('Crypt/RSA.php');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('/pathtokey.pem'));
$ssh = new Net_SSH2('ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('user', $key)) {
exit('Login Failed');
}
echo $ssh->exec('tail -n 3 /out/_log.txt.1');

Categories