943,915 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3650
  • PHP RSS
Dec 1st, 2008
0

how to connect to remote server usin ssh

Expand Post »
how to conecct remote server by using ssh in php
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asifkamalzahid is offline Offline
17 posts
since Nov 2008
Dec 1st, 2008
0

Re: how to connect to remote server usin ssh

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.
Last edited by stephen84s; Dec 1st, 2008 at 8:29 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 1st, 2008
0

Re: how to connect to remote server usin ssh

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asifkamalzahid is offline Offline
17 posts
since Nov 2008
Dec 2nd, 2008
0

Re: how to connect to remote server usin ssh

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
Last edited by stephen84s; Dec 2nd, 2008 at 9:55 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 3rd, 2008
0

Re: how to connect to remote server usin ssh

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 Syntax (Toggle Plain Text)
  1. <?php
  2. function rec_copy ($source_path, $destination_path, $con){
  3. //print "hello\n";
  4. if (@ftp_mkdir($con, $destination_path)) {
  5. echo "successfully created $destination_path\n";
  6. } else {
  7. echo "$destination_path may already exist\n";
  8. }
  9.  
  10. ftp_site($con, 'CHMOD 0777 '.$destination_path); //set writeable
  11.  
  12. $buff = ftp_rawlist($con, ".");
  13.  
  14. echo "<br/>";
  15. var_dump($buff);
  16.  
  17.  
  18. ftp_chdir($con,$destination_path); //move to remote directory
  19.  
  20. if (is_dir($source_path)) { //check for local directory
  21. chdir($source_path); //change to local directory
  22. $handle=opendir('.'); //open current directory
  23. while (($file = readdir($handle))!==false) { //read files
  24. echo $file . "\n";
  25. if (($file != ".") && ($file != "..")) { //if not an unwated file
  26. if (is_dir($file)) { //if is a directory
  27. if($file != "propertyimages") { //here i am restricting the folder name 'propertyimages' from being copied to remote server. -- VK
  28. rec_copy ($source_path."/".$file, $file, $con); //call this function on new directory
  29. chdir($source_path); //change to server local directory because after calling this function the ftp handle would be inside said directory
  30. ftp_cdup($con); //change to parent directory on server
  31. }
  32. }
  33. if (is_file($file)) { //if is a file
  34. $fp = fopen($file,"r"); //open local file for reading
  35. if (ftp_fput ($con, $file, $fp,FTP_BINARY)) {
  36. // echo "successfully uploaded $file\n";
  37. fclose($fp);
  38. $command ="echo Y| cacls \"" . realpath($source_path) . "\\" . $file."\" /G administrators:F";
  39. //echo $command . "\n";
  40. $e = exec($command) . "\n";
  41. unlink($file);
  42. } else {
  43. echo "There was a problem while uploading $file\n";
  44. }
  45.  
  46. ftp_site($con, 'CHMOD 0755 '.$file); //set permissions
  47. }
  48. }
  49. }
  50. closedir($handle);
  51. }
  52. }
  53.  
  54. print "Starting FTP!\n";
  55.  
  56. $con = ftp_connect("bbbbbbbbbbbbbbbbbbbbbbbb);
  57.  
  58. //$con = ftp_ssl_connect("bbbbbbbbbbbbbbbbbbbbbbb);
  59.  
  60. // make a FTP connection --VK
  61.  
  62. $login_result = ftp_login($con,"nnnnnnnnnnnnnnnnnnnnnnnnnnnnn"); // login
  63. //$con =ftp_connect("bbbbbbbbbbbbbbbbbbbbbbbbb);
  64. //$login_result=ftp_login($con,"bbbbbbbbbbbbbbbbbbbbbbb");
  65.  
  66. ftp_pasv($con,true);
  67. $rootpath = "in"; // this is the root path for the remote server-- VK
  68. //$rootpat,,,,,,,,,,,,,,,,,,,,,,,,,,/";
  69. ftp_chdir($con,$rootpath);
  70. $sourcepath = realpath("nnnnnnnnnnnnnnnnnnnnn"); // this is the physical path of the source directory. actually u can also use the relative path. -- VK
  71. $destination_dir_name = "in/"; // this directory name will only change the top most directory and not the inner one -- VK
  72. rec_copy ($sourcepath, $destination_dir_name, $con); //copy files
  73. if (function_exists("ftp_close")){ //close connection
  74. ftp_close($con);
  75. }
  76.  
  77. print "FTP completed!\n";
  78. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asifkamalzahid is offline Offline
17 posts
since Nov 2008
Dec 3rd, 2008
0

Re: how to connect to remote server usin ssh

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asifkamalzahid is offline Offline
17 posts
since Nov 2008
Dec 3rd, 2008
0

Re: how to connect to remote server usin ssh

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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Dec 4th, 2008
0

Re: how to connect to remote server usin ssh

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 Syntax (Toggle Plain Text)
  1. <?php
  2. $connection = ssh2_connect('shell.example.com', 22);
  3. ssh2_auth_password($connection, 'username', 'password');
  4.  
  5. ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
  6. ?>

Also the same page contains an example for sftp :-
php Syntax (Toggle Plain Text)
  1. <?php
  2. $srcFile = '/var/tmp/dir1/file_to_send.txt';
  3. $dstFile = '/var/tmp/dir2/file_received.txt';
  4.  
  5. // Create connection the the remote host
  6. $conn = ssh2_connect('my.server.com', 22);
  7.  
  8. // Create SFTP session
  9. $sftp = ssh2_sftp($conn);
  10.  
  11. $sftpStream = @fopen('ssh2.sftp://'.$sftp.$dstFile, 'w');
  12.  
  13. try {
  14.  
  15. if (!$sftpStream) {
  16. throw new Exception("Could not open remote file: $dstFile");
  17. }
  18.  
  19. $data_to_send = @file_get_contents($srcFile);
  20.  
  21. if ($data_to_send === false) {
  22. throw new Exception("Could not open local file: $srcFile.");
  23. }
  24.  
  25. if (@fwrite($sftpStream, $data_to_send) === false) {
  26. throw new Exception("Could not send data from file: $srcFile.");
  27. }
  28.  
  29. fclose($sftpStream);
  30.  
  31. } catch (Exception $e) {
  32. error_log('Exception: ' . $e->getMessage());
  33. fclose($sftpStream);
  34. }
  35. ?>
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.
Last edited by stephen84s; Dec 4th, 2008 at 2:38 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 4th, 2008
0

Re: how to connect to remote server usin ssh

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asifkamalzahid is offline Offline
17 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Dynamic Title Generation using PHP and MySql
Next Thread in PHP Forum Timeline: PHP extension querie! .php?id=2





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC