Categories

Server requests through php

11.05.2011
Server requests through php
Author:

Being involved in web development process one has often got to realize actions/commands for which he has not been granted enough rights or the user whose server has launched and executed Drupal, is lacking access to some directories.  libssh2 library seems to render the way out of this situation as it helps provide access to the resources (shell) of the remote server using an encrypted connection.

Shell is a command interpreter in the operating system.

In order to use shell commands in php, it's necessary to know the port, host, user name and password detais. For server connection set up the ssh2_connect function is being used and for user authorization the ssh2_auth_password is.

$port   = 2002;
$server = 'example.com';
$login  = '[email protected]';
$pass   = '12345';
if (!function_exists("ssh2_connect")) {
 exit("ssh2_connect disable");
}
if(!($con = ssh2_connect($server, $port))){ 
//переменный $con в случае успешного соединения
//присваивается ссылка идентификатора SSH з’соединения, 
//необходимое позже для вызова функции ssh2_auth_password, 
//при неудачном соединении присваеваится значение FALSE.
  exit("could not connect to {$server} with port {$port}");
}
if(!ssh2_auth_password($con, $login, $pass)) {
 exit("login/password is incorrect");
}

There also exist the following ways of authorization:

Here are examples of sftp functions that get accessible after connection has been set up:

And the list is far from being complete.

$sftp = ssh2_sftp($con);
ssh2_sftp_mkdir($sftp, '/new_directory');
ssh2_sftp_rmdir($sftp, '/old_directory')

ssh2_exec function seems to be a most functional as it facilitates executing server shell commands that are available for the above user.

if (!(ssh2_exec($con, "rm {$file}" ))) {
 exit("remove file failed");
}  
if (!(ssh2_exec($con, "cp {$path_from} {$path_to}" ))) {
 exit("file copy failed");
}

In other words, the mentioned library allows for shell commands to be used in php code and that affords advanced opportunities for development.

5 votes, Rating: 5

Read also

1

One of Drupal 7's major advantages over its precursors is its flexibility with settings and systems. Drupal...

2

Late in April of the current year Google Analytics representatives have announced in their official blog about launching of beta-testing of new interface.

In this article I want to share...

3

If someone's likely to be challanged with the necessity to have the attached PDF document displayed on the content view ...

4

Sometimes there is a need to create autocomplete field in order to enhance usability. As worked examples of such fields w...

5

The use of batch operations ebables forms processing to be performed in the...

Subscribe to our blog updates