I'm trying to backup mysql database through SSH using PHP.
I have made the ssh connection through ssh but I'm not making any progress
with the database backup.
This is my code :
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("server.hosting.com", 22))){
echo "fail: unable to establish connection\n";
} else {
if(!ssh2_auth_password($con, "user", "password")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
if (!($stream = ssh2_exec($con, 'echo "mysqldump -u userdb -p pass
dbname tablename > mydb_tab.sql"|mysql'))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
?>
Your command is not correct, it should read like this:
mysqldump -h server –uuserdb -ppass dbname > mydb_tab.sql
Related
I am fairly new to php and am currently working on a website for my university. What I am trying to do is make a log in page that allows a user to establish a connection to a remote SSH server and then run linux scripts all from wihtin php. So far, I've found this code that -- I guess is supposed to allow a user to log in. I've tried running the code, but it doesn't seem to work.
Edit: When I run the code, the page simply says "function ssh2_connect doesn't exist."
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("server1.example.com", 22))) {
echo "fail: unable to establish connection\n"; }
else {
// try to authenticate with username and password
if(!ssh2_auth_password($con, "username", "password")) {
echo "fail: unable to authenticate\n";
}
else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
if (!($stream = ssh2_exec($con, "ls -al" ))) {
echo "fail: unable to execute command\n";
}
else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
?>
I try to use PHP + SSH2 extension to reboot my Ubuntu 16.04 machine. I successfully install SSH2 extension:
And then I write the PHP code, It successfully logs in but not execute command.
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("localhost", 22))){
echo "fail: unable to establish connection\n";
} else {
if(!ssh2_auth_password($con, "lam", "root")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
if (!($stream = ssh2_exec($con, "reboot" ))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
?>
I dont't know what problem because I do similar in Centos 7.0 it works.
I am tryin to send a exec command in php to a remote server(aerohive). but the exec command does nothing..
I have my ssh2 extension installed and the authetication is also working fine.
Here is the code :
if (!function_exists("ssh2_connect"))
die("function ssh2_connect doesn't exist");
if (!($con = ssh2_connect("myipadresherenotshowingtoyouguys", 22))) {
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if (!ssh2_auth_password($con, "blablabla", "blablabla!")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
$command = 'ssid "Wentzo test2" hide-ssid';
if (!($stream = ssh2_exec($con, $command))) {
echo "fail: unable to execute command\n";
} else {
$stream2 = ssh2_exec($con, $command_save);
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream, 4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
Check the file php.ini if it does disabled the function disable_functions
When I execute the command enable with ssh2_exec(), I can't load the page because it is waiting for the password.
I tried to do ssh2_exec(connection,'password') and the problem persists.
My question is how to put the enable password ?
here is my code :
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("9.0.0.1", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "mehdi", "123")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
if (!($stream = ssh2_exec($con, 'enable'))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
ssh2_exec($con, 'PASSWORD');
$data = "";
while ($buf = fread($stream,4096)) {
$data .=PHP_EOL. date("Y-m-d H:i:s ").$buf;
$data .= PHP_EOL."------------------------------------------------------------------------------------------------------------------------\n";
}
echo $data;
$fh = fopen("log".date("Y-m-d").".txt", 'a+') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
fclose($stream);
}
}
}
?>
ps: i am trying to configure a Cisco router
what about this (with phpseclib)?
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('9.0.0.1');
$ssh->login('mehdi', '123');
$ssh->setTimeout(3);
$ssh->enablePTY();
$ssh->exec('enable');
$ssh->read('password:'); // or whatever the password prompt is
$ssh->write("password\m"); // or maybe without the \n
echo $ssh->read();
I've been trying to follow the example code found at:
http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/
I am able to connect to my switch, and successfully authenticate. BUt when I try to run a command, it fails with the error message in the title of this post.
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("10.14.123.12", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "mypassword")) {
echo "fail: unable to authenticate\n";
} else {
// execute a command
if (!($stream = ssh2_exec($con, "show mac-address" ))) {
echo "fail: unable to execute command\n";
} else {
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
It's dying on the line where I'm trying to execute the show mac-address command.
I've confirmed that this is the correct syntax for the switch command - tried it manually...and it returns data just fine.
Any suggestions?
Thanks.