I am trying to connect to a mac osx.
But i am getting errors.
How in the world can i copy files to this mac?
Also i have the following:
afp://192.xxx.x.x
smb://192.xxx.x.x
name#192.xxx.x.x
ERRORS:
Warning: ssh2_connect(): Unable to connect to 192.xxx.x.x on port 22
in /home/test.php on line 7
Warning: ssh2_connect(): Unable to connect to 192.xxx.x.x in
/home/test.php on line 7 fail: unable to establish connection
PHP Script:
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("192.xxx.xx.xx", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "", "")) {
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 123;
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;
}
ssh2_exec($con, 'ls -al; echo "__COMMAND_FINISHED__"' );
fclose($stream);
}
}
}
We open the port 22 to the wright.
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 need to establish a connection to an remote ssh server (debug2) via php. Inside the console everything works fine for me, but I doesnt get my php code running. I get following errormessage:
ssh2_auth_pubkey_file(): Authentication failed for [Username] using public key: Callback returned error
How can I establish a proper connection to debug2 without an registration on this.example.com?
My current code look like this:
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist" . PHP_EOL);
if(!($con = ssh2_connect("this.example.com", 22))){
echo "fail: unable to establish connection" . PHP_EOL;
} else {
if(!ssh2_auth_pubkey_file($con, "[Username]", $pubkeyfile, $privkeyfile)) {
echo "fail: unable to authenticate" . PHP_EOL;
//The point where the script fails
} else {
echo "okay: logged in..." . PHP_EOL;
$tunnel = ssh2_tunnel($con, 'xx.xx.xxx.17', 22);
if (!($stream = ssh2_exec($tunnel, "ls -al" ))) {
echo "fail: unable to execute command" . PHP_EOL;
} else {
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
My current ssh_config:
Host example
HostName this.example.com
User [Username]
ForwardAgent yes
IdentityFile ~/.ssh/id_rsa
Host debug2
ProxyCommand ssh exmaple -W xx.xx.xxx.17:22
User [otherusername2]
IdentityFile ~/.ssh/id_rsa
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
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.