SSH within PHP script [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
preface
I am setting up a web environment intended to be hosted on a local server strictly accessible on the local network. There's about 50 employees within the office and no employee has an assigned computer.
problem
Via an HTML form with parameters ('host', 'username', 'password'), the PHP will attempt an ssh.
Main Point
Basically I'm trying to find out how to interact with ssh from PHP (i.e. edit and view files/directories.
ie
user enters username, password into an html form. My php code takes this information and (if valid) ssh into their user on server. Giving them access to directories and files.

You can use phpseclib to handle the SSH. Here is some simple code to get you started:
<div>
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
function display_home_directory($hostname, $username, $password) {
$ssh = new Net_SSH2($hostname);
if (!$ssh->login($username, $password)) {
echo 'Login Failed';
}
echo '<pre>';
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
echo '</pre>';
}
display_home_directory('example.com', 'myusername', 'mypassword');
?>
</div>

Related

MySQL Connectivity Error on mysql_pconnect command using PHP 5 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I tried to access the database on IP 192.168.1.102 using Raspberry PI that have IP 192.168.1.129.
this is my code on constans.php
<?php
// Database Constants
//define("DB_SERVER", "127.0.0.1");
define("DB_SERVER", "192.168.1.102");
define("DB_USER", "aas");
define("DB_PASS", "aas");
define("DB_NAME", "db_adis_2");
?>
And this is my code on connection.php
<?php
require ("constants.php");
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
error_log(mysql_error() . "\n", 3, "my-errors.log");
}
// 2. Select a database to use
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
error_log(mysql_error() . "\n", 3, "my-errors.log");
}
?>
But i keep getting the error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Here is my XAMPP on 192.168.1.102
Xampp
That makes me confuse because when i try to connect it to different IP (192.168.1.5) with the same DB, it came out successfully. So where did i do wrong? or there is anything extra that i should do?

can't connect to sql database in godaddy server [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I created a connect.php file in my website that I've hosted in godaddy, but when I include this file in my index.php page. All the content in the page went blank.
this is my connect.php file code
<?php
$host = 166.62.10.223;
$con = mysqli_connect("host","root","");
if(mysqli_connect_errno())
{
echo "error".mysqli_connect_errno();
}
?>
I really need some help
Change your code from
$host = 166.62.10.223;
$con = mysqli_connect("host","root","");
To
$host = 166.62.10.223;
$con = mysqli_connect($host,"root","");
And You need a password too if you are working on live server and username might not be root So replace them by actual values.
Example
$user = "liveserver_username"; //which is created in godaddy mysql wizard section
$password = "liverserver_password"; //which is created in godaddy mysql wizard section
$host = 166.62.10.223;
$con = mysqli_connect($host,"$user","$password");
more information on how to create DB,User and password.

IBM Bluemix PostgreSQL and psql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've created a PHP runtime enviroment in Bluemix and attached a postgresql server to it.
When trying to use psql from terminal, it doesn't seem to can reach the database?
Also, adding ./bp-config/options.json and:
{
"PHP_EXTENSIONS": ["mysqli"]
}
I'm not able to conncet via mysqli from my php scripts.
The extension mysqli for PHP is useful to connect to a MySQL database server.
As you described you bound to your application a PostgreSQL database, so this extension is completely useless.
To connect to a PostgreSQL you could use composer dependency manager (supported on Bluemix) with you PHP application and set your composer.json with the dependency. You can obviously add other dependencies to your composer.json.
"require": {
"ext-pgsql": "*"
}
Then you could connect to your PostgreSQL service using a code like the following one in order to get service credentials from VCAP_SERVICES variables available from CloudFoundry
if (getenv("VCAP_SERVICES")===false) {
$db = 'XXXX';
define("APP_DB_SCHEMA", $db);
define("APP_DB_HOST", 'xxxx');
define("APP_DB_PORT", "xxxx");
define("APP_DB_USERNAME", 'xxxx');
define("APP_DB_PASSWORD", "xxxx");
} else {
// getting VCAP configuration
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);
$pgsql_config = $services_json["postgresql-9.1"][0]["credentials"];
define("APP_DB_SCHEMA", $pgsql_config["name"]);
define("APP_DB_HOST", $pgsql_config["host"]);
define("APP_DB_PORT", $pgsql_config["port"]);
define("APP_DB_USERNAME", $pgsql_config["user"]);
define("APP_DB_PASSWORD", $pgsql_config["password"]);
}
and then establish the DB connection through:
$dbConnectionString = "host=" . APP_DB_HOST . " port=" . APP_DB_PORT . " dbname=" . APP_DB_SCHEMA . " user=" . APP_DB_USERNAME . " password=" . APP_DB_PASSWORD;
$dbConnection = pg_connect($dbConnectionString);

Running SSH command with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have this code
<?php
$host = "MYSERVERIP";
$user = "root";
$password = "123456";
$command = $_GET["command"];
if($command != null) {
//HERE I WANT THE PHP TO SEND THE COMMAND TO THE SERVER
} else {
echo"No command were given!";
}
?>
Simply I want it to work like this: Let's say I go to MYSERVERIP/sendcommand.php?command=reboot then I want my server to reboot, how can I do this? My server is running Debian.
I've tried to understand it by reading http://php.net/manual/en/function.ssh2-exec.php, but that doesn't get me anywhere.
if you want to run command on the same server php runs - just use exec or shell_exec
ther is an example right there on your link...
mix with your stuff would look like
$host = "MYSERVERIP";
$user = "root";
$password = "123456";
$command = $_GET["command"];
if($command != null) {
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, $_GET["command"]);
} else {
echo"No command were given!";
}
Assuming the server would accepts commands like this
If your file is same server with your php file, you can use
exec() or shell_exec( string $cmd )
If Not same server, it's not possible, that all i know
If you want to issue a command on a different server than the server that runs PHP, you need to start with ssh2_connect.
If you want to issue commands locally on the server, start out by reading about program execution.
I suggest you pay close attention to the security aspect of this.
You should also know that these two options are probably disabled if you are on a shared hosting environment.

Access a database without a password? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to SQL database and was wondering If a user can have access to a database without providing a password. Or do I have to pass in an empty password field 'PWD' => ''
Sample code with password filed removed:
$connInfo = array(
'Database' => 'mystore',
'UID' => 'admin_user',
/*password field removed*/
'ReturnDatesAsStrings' => true
);
$connectString = sqlsrv_connect('some.sever.name', $connInfo) or die("Can't connect to the database");
$query = 'SELECT * FROM products';
$data = sqlsrv_query($connectString, $query) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));
while ($row = sqlsrv_fetch_array($data))
{
//Graduate
echo "<tr>";
echo " " .$row['NAME'] ." - " .$row['EMAIL'] ." ";
echo "</tr><br>";
}
sqlsrv_free_stmt($data);
sqlsrv_free_stmt($query);
?>
I am doing this for a testing purposes and not going to upload this in to a website without a password. Can you please tell me if the syntax of the above code is valid?
There are only two ways to connect to SQL Database:
1) Using SQL Password where you need to specified the credentials
2) or using domain authentication, where the credentials are same as you logged in your pc:
Option 1: Data source=localhost; initial catalog=master;trusted connection = true
Option 2: Data source=localhost; initial catalog=master;Integrated security=SSPI
But you can check the following link where you will find any connection string to most databases and all variants of connection:
http://www.connectionstrings.com/
Regards.
Majahide
The simplest way I can think that you can do what you say is by creating a user with an empty string as a password. You can do that directly in your RDMBS administrator.
However, from my personal point of view, that is a really dumb thing to do:
Anyone who has that username can access your data. Even if you provide read-only permissions to such username, that would be undesirable.
Your attempt to do such thing only denotes that you are lazy and that you are trying to take shortcuts, instead of following the (elementary) rules.
Besides that, instead of asking others to test your code, do it yourself. If you have any specific questions regarding specific problems, then feel free to ask.
Final note: Your question shows that you are using SQL server... but you are tagging as MySQL... was that accidental, or deliberate?

Categories