2

I've a link where I saw the reference to change the default attributes & set the required mode to transfer the dataset through SFTP.

Here is how it is..

sftp> ls /+mode=text,lrecl=80,recfm=fb

In this, we change the mode to text (where default is the binary mode).

When I execute this through command prompt it works fine and gives the same response which means the command executed perfectly!

(Response after executing in command prompt)

sftp> ls /+mode=text,lrecl=80,recfm=fb
/+mode=text,lrecl=80,recfm=fb 

But if I try to use the same through the PHP code it throws me the error as

ls: FSUM6785 File or directory "/+mode=text,lrecl=80,recfm=fb" is not found

Here is how I used using the phpseclib library:

 define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
 $user = 'username';
 $host = 'host';
 $key = new Crypt_RSA();
 $key->loadKey(file_get_contents('file.ppk'));
 $sftp = new Net_SFTP($host);
 if ($sftp->login($user,$key)) {
     echo $sftp->exec('ls /+mode=text,lrecl=80,recfm=fb');
 } else {
     return false;
 }

In the above if I try with $sftp->exec('ls -la'); and $sftp->exec('pwd'); it gives me the list of directories present & the current directory respectively.. which means the SFTP connection is fine and the exec command works.

But the same throws error when I use the ls /+mode=text,lrecl=80,recfm=fb commands as mentioned above.

Is there any other thing that need to be set to make these /+mode=text..... commands to work

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
kumar
  • 1,796
  • 2
  • 15
  • 37

1 Answers1

1
sftp> ls /+mode=text,lrecl=80,recfm=fb

The above is OpenSSH sftp client command ls. Internally, it issues a sequence of SFTP requests (OPENDIR, READDIR, CLOSE) to retrieve a directory listing.

The SFTP server is implemented to treat OPENDIR with that unique syntax specifically, as documented in the article you link to.


echo $sftp->exec('ls /+mode=text,lrecl=80,recfm=fb');

While the above executes a remote shell ls command. The shell ls command does not have a special treatment for the syntax and considers it a path and fails.

Note that while you execute the exec on $sftp instance, the method is actually implemented in parent SSH2 class and has nothing to do with SFTP.


To emulate the ls command of OpenSSH sftp (i.e. to send OPENDIR SFTP request), use some of the directory listing commands of SFTP class, like nlist or rawlist:

$sftp->nlist('/+mode=text,lrecl=80,recfm=fb');
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992