Issue
FTP and SFTP are different, but I wanted to know is there a equivalent to a "SYST" command in FTP for SFTP. If I have an SFTP connection using JSch and I wanted to return the system type, how would I do that? I am converting FTP connection to SFTP. If there is no way to do with an SFTP, is there a workaround for it?
public String getSystemType()
throws IOException, FtpException
{
if(out == null)
return null;
acquire(); // Acquire the object
try {
ftpCommand("SYST"); //sends command to FTP to retrieve the system type.
if(!reply.substring(0, 3).equals("215"))
throw new FtpException(reply);
} finally {
release(); // Release the object
}
return reply.substring(4);
}
I need this "system type" to parse the result of information of a file or directory from a location. It could be a Windows server or a UNIX server. Depending on that I need to parse it to retrieve
1) Name
* 2) Permission ..not available in MSDOS format list
* 3) Type
* 4) Owner ..not available in MSDOS format list
* 5) Group ..not available in MSDOS format list
* 6) Size
* Parse a UNIX format list. With skipping number of lines.
* The columns of a typical UNIX format list is like:
* drwxr-xr-x 6 appli appli 1024 May 17 20:33 java
* -rw------- 1 appli appli 1005 May 1 21:21 mp3list
* drwxr-xr-x 5 appli appli 1024 May 17 16:56 perl
* drwxr-xr-x 6 appli appli 1024 Apr 30 23:18 public_html
// Some UNIX format list doesn't contain the column for group like this:
// -rw-r--r-- 1 daemon 0 Dec 7 10:15 .notar
// -r--r--r-- 1 daemon 828 Mar 30 1995 HOW_TO_SUBMIT
// -r--r--r-- 1 daemon 627 Mar 30 1995 README
// -r--r--r-- 1 daemon 876 Mar 30 1995 WELCOME
// drwxr-xr-x 2 ftp 512 Oct 13 1999 bin
Solution
You can use SSH version string.
It is like:
- SSH-2.0-OpenSSH_5.3
- SSH-2.0-7.34 FlowSsh: Bitvise SSH Server (WinSSHD) 7.34
- SSH-2.0-1.36_sshlib GlobalSCAPE
- SSH-2.0-mod_sftp/0.9.8
In JSch you can retrieve it by using Session.getServerVersion
.
SFTP itself provides information about the remote system only using an optional extension "vendor-id", which is rarely supported. It's definitely not supported by JSch itself and neither by the most widespread SFTP server implementation, the OpenSSH.
Though note that with SFTP, you typically do not need to parse the directory listing. In SFTP most of the listing data come in a form of binary structure, not a textual listing like with FTP.
Answered By - Martin Prikryl