Checking free space on FTP server

2019-07-18 06:17发布

I am running a build script in which three executable files (100mb each) are uploaded to an FTP server.

The first upload can fail due to an FTP server space problem so our build process fails. This means I have to free some space from the server and run the build script again which is waste of time. I'd like to check the FTP size before uploading the exe to make sure space is there. If sufficient space is there then upload otherwise prompt user for input.

How can I check the FTP size in an ant script.

标签: java ant build ftp
4条回答
▲ chillily
2楼-- · 2019-07-18 06:54

Our product, CompleteFTP, allows you to add custom commands. Custom commands can be called from any protocol (FTP, SFTP, SSH, HTTP). In this case it'd be called as an FTP SITE command.

Custom commands can be added via embedded Javascript or via a .NET assembly. Embedded Javascript is a lot easier to work with, so I've developed a script for you that does this:

function drivespace(driveLetter) {
    var driveName = driveLetter.toUpperCase() + ":\\";
    var drives = System.IO.DriveInfo.GetDrives();
    for (var i=0; i<drives.Length; i++) {
        var drive = drives[i];
        if (drive.Name == driveName)
            return drive.TotalFreeSpace;
    }
    throw "Could not find drive " + driveName;
}

Notice that, even though it's Javascript, it's calling into the .NET class, System.IO.DriveInfo. The script is entered directly into the administration tool and is available immediately.

Here's a sample command-line session that shows it being called:

Connected to MyServer.
220-Complete FTP server
220 CompleteFTP v 10.1.0
530 Please login with USER and PASS
User (MyServer:(none)): myuser
331 Password required for myuser
Password:
230 User myuser logged in.
ftp> SITE drivespace C
200-170992414720
200 SITE command successful.
ftp> SITE drivespace g
501-Could not find drive G:\
501 SITE command failed.
ftp>
查看更多
戒情不戒烟
3楼-- · 2019-07-18 06:57

FTP servers usually do not allow this, but you can try the command:

ftp> site df -kl

However, I suggest you to schedule a script on the FTP server which writes to a TXT file the current free space (which in turn you may read from your client).

查看更多
劳资没心,怎么记你
4楼-- · 2019-07-18 07:12

Possible solution is ftp > df -h

查看更多
Juvenile、少年°
5楼-- · 2019-07-18 07:14

As discussed in the comments, you can write a shell script (assuming the FTP server is a linux box) along the lines of:

#!/bin/bash

FTPDIR="/path/to/ftp/dir"
echo `df -kl / | tail | awk '{print $4}'` > ${FTPDIR}/free_space.txt

Make sure you replace path/to/ftp/dir with the actual path to the directory you will be downloading from.

Add that script to the server's crontab (crontab -e) and let it run every hour (0 * * * *) or so. You can download the free_space.txt file which contains the hourly updated free space of the FTP server.

查看更多
登录 后发表回答