ssh access to ec2 from php - 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');

Related

PHP fails to access the docker socket via curl

My Code fails to reach the Docker Socket
$client = new GuzzleHttp\Client();
$test = $client->request('GET','http://v1.40/containers/json',[
'curl' => [CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock']
]);
I only get a generic cURL error 7 from that and I´ve checked that the socket is available and working inside the Container with the cURL command from the cmd. Its only when i try to connect via PHP it fails ominously and frankly im out of ideas.
So just in case someone stumbles upon this in the future and has the same or a similar problem.
Guzzle was not the problem in this case but phpfpm. I did not realize that the phpfpm workers in the official php docker image used the www-data user by default. The way I used is to change the user in the www.conf (default /usr/local/etc/php-fpm.d/www.conf in docker)
user = root
group = root
you will have to append the -R flag to the run command to allow running the workers as root.

How to run the object file in remote computer?

I have a scenario please take a look.
I have two Pcs one is windows and another is ubuntu.
I have installed laravel (PHP) on windows and the necessary files.
On ubuntu, there is only an Object file.
What am i doing is:
on click of a button (from windows) run a function which will connect to ubuntu pc with its credentials,
and run the command to launch that Object file in ubuntu.
How can I achieve this with laravel? any help is appreciated.
all you need is ssh connection
$connection = ssh2_connect('server_address', 'port');
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'your terminal ubuntu command');
see more info here https://www.php.net/manual/en/function.ssh2-exec.php

PHP or Python on windows send command to remote Linux stack

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

ssh2.so unable to load on CentOS 5

I've been trying to install the SSH2 libraries for php onto a web server running CentOS 5 with PHP 5.1.6 and was able to successfully install all the dependencies, but after restarting the web server I get the following error:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/ssh2.so' - libssh2.so.1: cannot open shared object file: No such file or directory in Unknown on line 0
Has anyone run into this before? The ssh2.so file exists at '/usr/lib64/php/modules/ssh2.so' and has the same file permissions as all the other extensions (-rwxr-xr-x).
Thanks
The key to the error is this:
libssh2.so.1: cannot open shared object file: No such file or directory
Your ssh2.so file can't find it. Run this and you'll see what I mean:
ldd /usr/lib64/php/modules/ssh2.so
Try this:
updatedb
locate libssh2.so.1
If found, you may need to create a symlink inside /usr/lib64/ or something.
If that doesn't find it, a quick google search brings up this as a hit for libssh2 on centos5:
http://centos.karan.org/el5/extras/testing/x86_64/RPMS/libssh2-0.18-10.el5.kb.x86_64.rpm
Install that, restart apache, and try it again.
Maybe phpseclib, a pure PHP SSH implementation, would work better for you? An example:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

SCP with PHP - Does not work

Trying to upload a file to a different server using PHP. It connects with ssh_connect() successfully, but when I include ssh2_scp_send the page that is displayed is blank (as though there is a syntax error), and it certainly did not upload the file. Any idea why this may be? I'd prefer to use PHP, but if it doesn't work I suppose I could make a system call to CLI scp with private key files, but I'd prefer not to do that. Alternative suggestions are welcomed.
Info: debian w/apache
Installed: libssh2-1-dev libssh2-php php-pear php5-dev; pecl install -f ssh2
$ssh = ssh2_connect('localhost', 22);
ssh2_auth_password($ssh, 'user', 'password');
ssh2_scp_send($ssh, 'original', 'destination'); # does not work
ssh2_exec($ssh, 'exit'); #does not help
Any ideas?
Personally, I'd recommend using phpseclib, a pure PHP SFTP implementation:
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
Chief among libssh2's problems is the fact that it's not easy to install. The original authors have abandoned it. The source code on php.net requires modification for it to even compile and although you can install it with apt-get on Ubuntu right now what happens when it requires more than just two new lines to work? And what if the Linux box you're trying to get it working on isn't Ubuntu?
If you ever need to create a new server on which to host your website you don't want to be stuck spending hours on a bunch of esoteric dependencies.

Categories