Phpseclib securely communicating with remote servers via php

July 6, 2021 . 2 MIN READ

<?php

 

//$connection = ssh2_connect(‘66.175.213.168’, 22);

//ssh2_auth_password($connection, ‘root’, ‘password’);

 

//$stream = ssh2_exec($connection, ‘/usr/local/bin/php -i’);

 

set_include_path(get_include_path() . PATH_SEPARATOR . ‘phpseclib’);

 

include(‘/home/runcloud/webapps/app-midas-prosite/phpseclib/Net/SSH2.php’);

include(‘/home/runcloud/webapps/app-midas-prosite/phpseclib/Crypt/RSA.php’);

 

define(‘NET_SSH2_LOGGING’, NET_SSH2_LOG_COMPLEX);

 

$server   = “45.79.69.220”; // server IP/hostname of the SSH server

$username = ‘root’; // username for the user you are connecting as on the SSH server

$password = ‘password’; // password for the user you are connecting as on the SSH server

 

$ssh = new Net_SSH2($server);

if (!$ssh->login($username, $password)) {

exit(‘Login Failed’);

}

 

$ssh->setTimeout(0);

 

$runcloud_command= ‘https://manage.runcloud.io/script/installer/A1ldJRYkIvyYCHDQaHSAGX7oQr1559088447ekPjbx3hfMhwcg3quvcf1SNgVI2YKm782lmbET7gONEX8O9zttOlZg2bZgQqDi9B/CkUpQuTdX13IdGKzR9yQkDBDFwAQZObOooH28RO3N6OrsU5Ukvfy374D079KVDOl6IPHcrXDDFZNDP1xURGGVEfE9ebJQcQqyhH4UP3XmbdxaQIcDwZVu8JkanUcHghD’;

 

$command  = ‘export DEBIAN_FRONTEND=noninteractive; echo \’Acquire::ForceIPv4 “true”;\’ | tee /etc/apt/apt.conf.d/99force-ipv4; apt-get update; apt-get install curl netcat-openbsd -y; apt-get -o Dpkg::Options::=”–force-confdef” -o Dpkg::Options::=”–force-confold” upgrade -y; curl –silent –location ‘. $runcloud_command .’ | bash -; export DEBIAN_FRONTEND=newt’; // could be anything available on the server you are SSH’ing to

// Establish a connection to the SSH Server. Port is the second param.

 

$deployment_script = ‘set -e;’ . PHP_EOL

. ‘export DEBIAN_FRONTEND=noninteractive;’ . PHP_EOL

. ‘echo \’Acquire::ForceIPv4 “true”;’ . PHP_EOL

. ‘\’ | tee /etc/apt/apt.conf.d/99force-ipv4;’ . PHP_EOL

. ‘apt-get update; apt-get install curl netcat-openbsd -y;’ . PHP_EOL

. ‘apt-get -o Dpkg::Options::=”–force-confdef” -o Dpkg::Options::=”–force-confold” upgrade -y;’ . PHP_EOL

. ‘curl –silent –location https://manage.runcloud.io/script/installer/UdVceBkeVvcQJ1Qo8ruMq8AQwc1559333028lI2c0j4r2fbQManftrzBzllQxEqO0WRV9KwcQSo8r1G4PYsIP8tCbP2h3FNkLWLv/f4fTVb0xKokQfcwgG9W9nD2r98lN2bo3gE18QvSHXagtPC95c3bjhlEwOD8b8pXnOdaB9RQbeyqVrpskzCsHPvHidxwS509yfm0SBMqP9prhLE2SHItaw1hx0FSiBTWh  | bash -;’

. PHP_EOL . ‘export DEBIAN_FRONTEND=newt’;

 

$output = $ssh->exec($deployment_script);

echo $output;

 

?>

Leave a Reply

Your email address will not be published. Required fields are marked *