In order to upload a batch of files from the remote server, I use ssh2_scp_recv() in my php program's for loop. It works good for some point. But when there are more than 67 files in one folder, it start to have the error message as
PHP Warning: ssh2_scp_recv(): Unable to receive remote file
The program I use like the following:
<?php
$connection = ssh2_connect('host', 22);
ssh2_auth_password($connection, 'user', 'password');
$remote_dir="/remote_dir/";
$local_dir="/local_dir/";
$com ="ls $remote_dir";
$stream = ssh2_exec($connection, $com);
stream_set_blocking($stream,true);
$cmd=fread($stream,4096);
$arr=explode("\n",$cmd);
$total_files=sizeof($arr);
for($i=0;$i<$total_files;$i++){
$file_name=trim($arr[$i]);
if($file_name!=''){
$remote_file=$remote_dir.$file_name;
$local_file=$local_dir.$file_name;
if(ssh2_scp_recv($connection, $remote_file,$local_file)){
echo "File ".$file_name." was copied to $local_dir<br />";
}
}
}
fclose($stream);
Can anyone give me some suggestion?
One call to fread() may not be getting all the filenames. Use stream_get_contents to read until the end:
$cmd = stream_get_contents($stream);
Related
I am working on a project where we are using 2 VM's one is used as local and can be accessible and from this local VM, we need to access the remote VM. While working on this I am struck on a problem.There is a file in the remote in a directory.Every time a script gets executed the file gets loaded with new data and but I don't need that.I want the file to be deleted everytime before my script executed.
I have given the code below.
if($ssh = ssh2_connect('192.168.150.85', 22)) {
if(ssh2_auth_password($ssh, 'someUserName', 'somePassword')) {
$stream = ssh2_exec($ssh, "python file_scanner.py /home/nsadmin/$file_name");
$stream = ssh2_exec($ssh, 'rm /home/nsadmin/file_scanned');
stream_set_blocking($stream, true);
$data = '';
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
fclose($stream);
echo $data; // user
}
}
Here we can see that after ssh connection,Iam ecxecuting some script and the result of this file gets appended in the file file_scanned.But when ever I execute the ssh connection I previous results are also coming.So I did $stream = ssh2_exec($ssh, 'rm /home/nsadmin/file_scanned'); . But it is not working.
Any suggestions on how to change or modify my script so I can remove the file after execution of previous command.
There may be issue with permission I can say,
But this is solution you can give a try.
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection);
$stream = ssh2_sftp_unlink($sftp, '/home/nsadmin/file_scanned');
This is frequent thing, which we used for removing remote files.
ssh2_scp_send() function is hanging in php. Here's the code:
$debug_line_feed = "\n";
$conn = ssh2_connect($sftp_server, 22);
$return = ssh2_auth_password($conn, $sftp_user_name, $sftp_user_pass);
if ($return===true) echo "successfull connection".$debug_line_feed;
echo "uploading file".$debug_line_feed;
$local_filename = $product_feed_file_with_path;
$remote_filename = 'product_feed.txt';
ssh2_scp_send($conn, $local_filename, $remote_filename);
echo "successful".$debug_line_feed;
When i run it, it outputs "successful connection", "uploading file" then hangs. Any idea how to fix this?
I have tried a download as well with ssh2_scp_recv, and it hangs as well, with the local file being created as a 0 byte file.
My guess is that the server has a jail shell installed. At that point SCP wouldn't work but SFTP would.
recently,i use sftp to send file,linux to windows,ssh2_scp_send not work,i solve the problems use
$sftp = ssh2_sftp($conn);
$contents = file_get_contents($localPath);
$result = file_put_contents("ssh2.sftp://{$sftp}/{$remotePath}", $contents);
then works
i want to pass at commands using php dio extension and at command in windows.
dio_open(); function working well but dio_write(); give an error.
i dont know reason here is my code..
error..
Warning: dio_write(): cannot write data to file descriptor 3: Bad file descriptor in C:\xampp\htdocs\dio.php on line 17
code
$fd = dio_open('COM8:', O_RDONLY);
if(!$fd)
{
echo "Connection not working";
}
else
{
//echo "Connection working";
//$result = dio_write($fd, 'aaaaa_bbbbb_ccccc_ddddd_',12 );
dio_write($fd, "AT\n");
//dio_write($fd, "AT\r");
//dio_write($fd, "AT+CMGF=1\r");
//dio_write($fd, "AT+CMGL="ALL"\r");
}
I'm trying:
-to connect to the server
-to list all the files
-to read the files and echo the content.
The connection work well:
$conn_id = ssh2_connect('xxx.com', 22);
$login_result = ssh2_auth_password($conn_id, 'xxxxxx', 'xxxxxxxxx');
$sftp = ssh2_sftp($conn_id);
$dir = "ssh2.sftp://$sftp/home";
$ftp_contents = scanFilesystem($dir);
Then I loop over the directory and get all the filenames, work like a charm:
foreach($ftp_contents as $key => $currentFilename){
echo($currentFilename);
}
Then I try to get the content of the file.... And it doesn't work.
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
echo($stream);
There is the output error:
Warning: fopen(ssh2.sftp://Resource id #4/home/myfile.xml) [function.fopen]: failed to open stream: operation failed in /home/xxxxxx/public_html/xxx.php on line 38
The line 38 is the end of the foreach.
I try with:
fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
fopen("ssh2.sftp://$sftp/".$currentFilename, 'r');
fopen("/home/".$currentFilename, 'r');
fopen("home/".$currentFilename, 'r');
Nothing work, always the same type of error, can some one help me please?
Thanks.
update: I try with : $stream = file_get_contents("ssh2.sftp://$sftp/home/$data"); does'nt work either ...
Still got the error :
Warning: file_get_contents() [function.file-get-contents]: Unable to open ssh2.sftp://Resource id #3/xxx.xml on remote host in /home/xxxxxx/public_html/xxxx.php on line 40
I don't have a clue ... can some one help me?
I had the same problem. Resolved by converting the $sftp resource to an integer before using it as a url, which is a string.
Should work like that:
$sftp = (int)$sftp;
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
I have a php code which will ssh to a remote machine and execute a shell script to get list of folders. The remote machine contain more than 300 folders in the specified path in the shell script.Shell script executes well and return the list of all folders.But while I retrieve this output in php, I'am getting only around 150, 200 number of folders.
Here is my php code,
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("ip.add.re.ss", "port")))
{
echo "fail: unable to establish connection";
}
else
{
if(!ssh2_auth_password($con, "username", "password"))
{
echo "fail: unable to authenticate";
}
else
{
$stream = ssh2_exec($con, "/usr/local/listdomain/listproject.sh");
stream_set_blocking($stream, true);
$item = fread($stream,4096);
$items = explode(" ", $item);
print_r($items);
}
}
?>
And this is my shell script.
#!/bin/bash
var=$(ls /home);
echo $var;
What is the issue with php here. Is there any limit in array size in php while getting data dynamically like here.Please advise as I am very beginner to PHP.
Thanks.
You're only reading one block of 4096 characters from your stream. if your folder list is longer than this you'll lose the rest. You need something like this:
stream_set_blocking($stream, true);
$item = "";
// continue reading while there's more data
while ($input = fread($stream,4096)) {
$item .= $input;
}
$items = explode(" ", $item);
print_r($items);
You asked fread() to only read 4096 bytes. In the examples portion of fread()’s documentation, it is suggested that stream_get_contents() be used for reading a file handle out to its end. Otherwise, you have to use a loop and keep on reading data until feof($stream) returns FALSE.