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.
Related
Context
PHP 5.6
IIS
Windows 11
Issue
I am trying to write a file in a specific folder but it gives me permission denied.
So, I verified the permissions and everything seemed all right. Because this is in a development environment, I decided to change the permissions to be "Every one can do anything" to the "root" folder where all my files are written. I tried to replace all the permissions underneath. I tried to remove the inherited permissions. Nothing does it.
I then tried to write a file in the "root" folder. It worked.On the subfolder, it worked. On the subsubfolder. It worked. In the subfolder chain there is a point when it doesn't work, but before the real subfolder.
Here is the path hierachy when it crashes.
$f = new \wee\common\IO\File();
$f->write("DRIVE:/BASE_PATH/files/-/00.jpg", "hello"); // WORKS
$f->write("DRIVE:/BASE_PATH/files/-/mod/00.jpg", "hello"); // WORKS
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/00.jpg", "hello"); // WORKS
$pathLength = strlen("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/00.jpg"); // Real path length is 85
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/00.jpg", "hello"); // FAILS
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/WorkersManager/00.jpg", "hello");
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/WorkersManager/workers/00.jpg", "hello");
The class \wee\common\IO\File is my creation, but works at a lot of other places.
The exact error message I am getting is:
fopen(DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/00.jpg): failed to open stream: Permission denied
Just to be 100% clear: The "root" folder is DRIVE:/BASE_PATH/files/
Edit #1
Here is the implementation of the write method of the File class.
public function write($fileName, $data, $lock = false) {
$this->_write($fileName, $data, $lock);
}
private function _write($fileName, $data, $lock = false, $openMode = 'w') {
if ($data === null)
$data = "";
$fh = fopen($fileName, $openMode) or die("can't open file"); // FAILS HERE
if ($lock) {
flock($fh, LOCK_EX);
}
fwrite($fh, (is_array($data) ? join("\n", $data) : $data));
if ($lock) {
fflush($fh);
flock($fh, LOCK_UN);
}
fclose($fh);
}
The issue was coming from PHP 5.6.26. Using PHP 5.6.40 fixed it.
I reset to my original permissions and everything is fine!
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
When I try to use ssh2_exec to run a python script on a remote server, and try to make it running under un-blocking model. An error shows up:
stream_select(): cannot represent a stream of type SSH2 Channel as a
select()able descriptor
My code:
$ssh_connection = ssh2_connect('server ip address',22);
ssh2_auth_password($ssh_connection, 'root', 'password');
$stream = ssh2_exec($ssh_connection, 'cd /opt/; python db.py');
stream_set_blocking($stream, false);
$read = array($stream);
$write = array();
$except = array();
$return_string = stream_get_contents($stream);
//echo $return_string;
stream_select($read, $write, $except, 300);
Does anybody know why it shows me that error?
Looks like you are you dont have acces or banned on remote server. Try to run from another IP.
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);
I am working on a tool that reads an iptables configuration from a remote host over SSH2 using the PECL SSH2 extension. I am able to successfully make the connection to the host, authenticate, and execute commands. The trouble I am having is sometimes the stream doesn't contain any data.
/**
* Load the current firewall configuration
* #return bool
*/
public function loadRules() {
$stream = ssh2_exec($this->connection,"~/iptsave;");
stream_set_blocking($stream,true);
$iptablesSave = stream_get_contents($stream);
if(empty($iptablesSave)) {
return false;
}
parent::restore($iptablesSave);
return true;
}
About 25% of the time, loadRules() returns false, even when connecting to locahost instead of the remote system. I was able to work around the problem by changing the ssh2_exec call to
$stream = ssh2_exec($this->connection,"~/iptsave; sleep .5");
but I am concerned that something is wrong.
phpSecLib may be able to help:
According to this post, it always returns the output, unlike ssh2.so.
I've got the same issue here. Somehow you need to set a delay for getting the result of the stream.
The way you've done it is possible, but you could also set a sleep(1) after the stream_set_block($stream, true) function.
You could try the usleep() function. Haven't tried it yet
May be this will solve the issue:
$stream = ssh2_exec($this->connection,"~/iptsave;");
stream_set_blocking($stream,true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$iptablesSave = stream_get_contents($stream);
With some severs you have to use 'interactive shell'. And sometimes you have to set the delay / sleep manually. A working example:
$connection = ssh2_connect($IP, 22);
$auth = ssh2_auth_password($connection, $User, $Pass);
$cmd = "help" . PHP_EOL;
if (!$auth) {
echo "Login Failed;
exit(1);
}
$shell = ssh2_shell($connection);
stream_set_blocking($shell, false); // we will use manual sleep
sleep(1); // This sleep to make sure that you get the prompt back
fwrite ($shell, $cmd . ";" . PHP_EOL);
sleep(1); // This to make sure that the command executes and we get the prompt back again!
while($output = fgets($shell)){
echo $output;
}
fwrite ($shell, "exit;" . PHP_EOL); // If needed
sleep(1);
ssh2_disconnect($connection);
unset($shell);
unset($connection);