how to conecct remote server by using ssh in php

Recommended Answers

All 9 Replies

For that you will PHP bindings for libssh2.
You can check out one of them here or here.

Note both the links refer to the same extension.

i am sending my file by using ftp_connect sucessfully. but now the server details are changed .i need to send my data bu ssh.
it is new and difficult for .
if can u explain to me the main steps.......
i will be very thankfull to u for your time.
thanks

I am not going to spoon feed you the exact steps and I do not prefer anyone bugging me for help via PM,
See the following, page it was already present in my earlier link, if you had cared yo take the time to read it, you would have found it out yourself.
http://in.php.net/manual/en/function.ssh2-scp-send.php

Thanks my friend
it is not about for something i ask you to give me any code.
i just want to understand,
how i can work on ssh.
it new for me.
i already work on it from last week, i gather alot of data about it but still i am confused.
i think i will tell u the problom i have,
my server is ssh.
through ftp i am accessing the the server and i can read file over there, but when i am trying to upload file then i have the following error,
Starting FTP! in/ may already exist
array(5) { [0]=> string(3) "in:" [1]=> string(7) "total 0" [2]=> string(0) "" [3]=> string(4) "out:" [4]=> string(7) "total 0" } . .. su86.zip
Warning: ftp_fput() [function.ftp-fput]: Access is denied. in G:\WEBROOT\aa\ftpcopy_ver2.php on line 35
There was a problem while uploading su86.zip
Warning: ftp_site() [function.ftp-site]: su86.zip: No such file or directory. in G:\WEBROOT\aa\ftpcopy_ver2.php on line 46
FTP completed! PHP Warning: ftp_fput() [function.ftp-fput]: Access is denied. in G:\WEBROOT\aa\ftpcopy_ver2.php on line 35 PHP Warning: ftp_site() [function.ftp-site]: su86.zip: No such file or directory. in G:\WEBROOT\aa\ftpcopy_ver2.php on line 46


if you like then i am sending my code as well check it if you want, i am not forcing you,

<?php
function rec_copy ($source_path, $destination_path, $con){
   //print "hello\n";
   if (@ftp_mkdir($con, $destination_path)) {
	 echo "successfully created $destination_path\n";
	} else {
	 echo "$destination_path may already exist\n";
	}
	
   ftp_site($con, 'CHMOD 0777 '.$destination_path);		//set writeable
      	
$buff = ftp_rawlist($con, ".");

echo "<br/>";	
var_dump($buff);

   
   ftp_chdir($con,$destination_path);					//move to remote directory

   if (is_dir($source_path)) {							//check for local directory
       chdir($source_path);								//change to local directory
       $handle=opendir('.');							//open current directory
       while (($file = readdir($handle))!==false) {		//read files
	   	   echo $file . "\n";
           if (($file != ".") && ($file != "..")) {		//if not an unwated file
               if (is_dir($file)) {						//if is a directory
                   if($file != "propertyimages") {	    //here i am restricting the folder name 'propertyimages' from being copied to remote server. -- VK
                       rec_copy ($source_path."/".$file, $file, $con);	//call this function on new directory
                       chdir($source_path);								//change to server local directory because after calling this function the ftp handle would be inside said directory
                       ftp_cdup($con);									//change to parent directory on server
                   }
               }
               if (is_file($file))	 {									//if is a file
                   	$fp = fopen($file,"r");								//open local file for reading
					if (ftp_fput ($con, $file, $fp,FTP_BINARY)) {
					// echo "successfully uploaded $file\n";
					 fclose($fp);
					$command ="echo Y| cacls \"" . realpath($source_path) . "\\" . $file."\" /G administrators:F";
					//echo $command . "\n";
					 $e = exec($command) . "\n";
					 unlink($file);
					} else {
					 echo "There was a problem while uploading $file\n";
					}

                   	ftp_site($con, 'CHMOD 0755 '.$file);		//set permissions
               }
           }
       }
       closedir($handle);
   }
}

print "Starting FTP!\n";

$con = ftp_connect("bbbbbbbbbbbbbbbbbbbbbbbb);

 //$con = ftp_ssl_connect("bbbbbbbbbbbbbbbbbbbbbbb);

// make a FTP connection --VK

$login_result = ftp_login($con,"nnnnnnnnnnnnnnnnnnnnnnnnnnnnn");  // login
//$con =ftp_connect("bbbbbbbbbbbbbbbbbbbbbbbbb);
//$login_result=ftp_login($con,"bbbbbbbbbbbbbbbbbbbbbbb");

ftp_pasv($con,true);
$rootpath = "in";   							// this is the root path for the remote server-- VK
//$rootpat,,,,,,,,,,,,,,,,,,,,,,,,,,/";
ftp_chdir($con,$rootpath);
$sourcepath = realpath("nnnnnnnnnnnnnnnnnnnnn");					// this is the physical path of the source directory. actually u can also use the relative path. -- VK
$destination_dir_name = "in/";					// this directory name will only change the top most directory and not the inner one -- VK
rec_copy ($sourcepath, $destination_dir_name, $con);	//copy files
if (function_exists("ftp_close")){						//close connection
   ftp_close($con);
}

print "FTP completed!\n";
?>

This code and problom is here ,Anyone Who wish to contribute his knowlege then he can reply to this problom, otherwise for all those people who are roud then i am in advance asking them, please dont disturb me with your roud advoices,
many thanks

There was no rude advice from anyone as far as I can see. You asked general question and you got general answer. You asked little more specific, the answer was given in that way.
If you want to get maximum support or help you better provide everything what you got.

This code and problom is here ,Anyone Who wish to contribute his knowlege then he can reply to this problom, otherwise for all those people who are roud then i am in advance asking them, please dont disturb me with your roud advoices,
many thanks

You wanted to send a file via SSH and I gave you the link to it, Unfortunately you still couldn't figure it out so I gave you the exact link to the page where the example was mentioned, Now do you want me to do the coding also for you. Also the code you have pasted here is for FTP connectivity not SSH, so definitely it shouldn't work.
Have you even gone through the examples mentioned in my previous link ??? Here I will paste the code for you:-

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>

Also the same page contains an example for sftp :-

<?php
$srcFile = '/var/tmp/dir1/file_to_send.txt';
$dstFile = '/var/tmp/dir2/file_received.txt';

// Create connection the the remote host
$conn = ssh2_connect('my.server.com', 22);

// Create SFTP session
$sftp = ssh2_sftp($conn);

$sftpStream = @fopen('ssh2.sftp://'.$sftp.$dstFile, 'w');

try {

    if (!$sftpStream) {
        throw new Exception("Could not open remote file: $dstFile");
    }
   
    $data_to_send = @file_get_contents($srcFile);
   
    if ($data_to_send === false) {
        throw new Exception("Could not open local file: $srcFile.");
    }
   
    if (@fwrite($sftpStream, $data_to_send) === false) {
        throw new Exception("Could not send data from file: $srcFile.");
    }
   
    fclose($sftpStream);
                   
} catch (Exception $e) {
    error_log('Exception: ' . $e->getMessage());
    fclose($sftpStream);
}
?>

I bet you have not even read it else you wouldn't have needed to post again. Also if you cannot understand the examples there on your own, I suggest you hire a programmer who does know something about PHP and SSH.

BTW I think you are the one who first broke the community rules and begged me for help via PM. If you want help then learn to control your tongue and don't expect anyone else to do your coding for you.

Sorry Peter I couldn't control my rage on this one.

You are good person, but a bit angry, i realy dont know why?
anyway dont mind.
be happy.
i just wanted to understand how to establish a connection between two servers. i did it by ftp. now i was looking for secure connection.just for learning i did it.
if it is not possible then no worry, its ok.it is not important.
how you take my words that i am bagging you for help. this is the word which is problamic. dont use it, becouse it will make you angry and tense. be happy and enjoy your php buddy.
have very good day and nights.
bye bye for
ever.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.