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);
}
}
}
?>
Related
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 have problem to input a password for new user that generated via ssh with php to a Debian Server.
To add new user I just simpy write "useradd $username". But if I want to set the password for that user it should be like "passwd $username" and then I should insert the password. But how to do this in PHP?
This is my complete code :
<?php
$username = "abcd";
$password = "456789";
$expired = "5";
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("128.199.xxx.xx", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "xxxxxx")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
if (!($stream = ssh2_exec($con, "passwd 'haha'"))){
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;
}
echo "Username berhasil dibuat";
echo $data;
fclose($stream);
}
}
}
?>
Seems you are looking for non-interactive way of changing user password.
Try this
echo "username:newpass"|chpasswd
Here is chpasswd manual page.
This method also suits for changing passwords for larger number of users.
UPD:
To know currently logged in username whoami can be of use.
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.
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.