Net::FTP - How Do I Read The Available Space On A Partition ???

T

tommo_blade

Hello All,
I have read thru the perl docs for this module and I have
tried a few of the 'methods' available but as yet I have not been able
to get my script to pull the data I need from the server. basically I
want to know if there is enough 'available space' on a unix partition
prior to me FTP'ing a file to that host/partition, I have tried the
'ls' and 'dir' methods but I cannot get what I need. The data I need at
unix level can be gained by running a 'df -k' command which when run
returns the available disk space in the 'avail' column.

Does anyone know of any neat tricks to get this data.


cheers, Mark.
 
D

David Squire

tommo_blade said:
Hello All,
I have read thru the perl docs for this module and I have
tried a few of the 'methods' available but as yet I have not been able
to get my script to pull the data I need from the server. basically I
want to know if there is enough 'available space' on a unix partition
prior to me FTP'ing a file to that host/partition, I have tried the
'ls' and 'dir' methods but I cannot get what I need. The data I need at
unix level can be gained by running a 'df -k' command which when run
returns the available disk space in the 'avail' column.

Does anyone know of any neat tricks to get this data.

This works:

----

#!/usr/bin/perl

use strict;
use warnings;

my $df_out = `df -k`;

foreach my $Line (split /\n/, $df_out) {
my ($Filesystem, $OneKBlocks, $Used, $Available, $UsePercent,
$MountPoint) = split /\s+/, $Line, 6;
print $Available if ($MountPoint eq '/'); # substitute whatever
criteria you are interested in
}

----

but I am sure that someone will come up with a nifty one-liner. Also, it
depends on your 'df' output having the same format as mine (tested on
Ubuntu and Mac OS X).


DS
 
P

Paul Lalli

tommo_blade said:
I have read thru the perl docs for this module and I have
tried a few of the 'methods' available but as yet I have not been able
to get my script to pull the data I need from the server. basically I
want to know if there is enough 'available space' on a unix partition
prior to me FTP'ing a file to that host/partition, I have tried the
'ls' and 'dir' methods but I cannot get what I need. The data I need at
unix level can be gained by running a 'df -k' command which when run
returns the available disk space in the 'avail' column.

Does anyone know of any neat tricks to get this data.

Uh. Is this something you can do through FTP? Forget Perl for a
moment - can you do this with a normal ftp command line session? If
not, what makes you think you'd be able to do it with a Perl interphase
to the File Transfer Proticol?

Paul Lalli
 
A

anno4000

David Squire said:
This works:

----

#!/usr/bin/perl

use strict;
use warnings;

my $df_out = `df -k`;

foreach my $Line (split /\n/, $df_out) {
my ($Filesystem, $OneKBlocks, $Used, $Available, $UsePercent,
$MountPoint) = split /\s+/, $Line, 6;
print $Available if ($MountPoint eq '/'); # substitute whatever
criteria you are interested in
}

According to the OPs words "want to know if there is enough 'available
space' ... prior to me FTP'ing a file to that host/partition" that would
have to be run on the remote system, through ssh perhaps.

No solution exists using only the FTP protocol. The designers of FTP
have wisely decided not to require that. At the time, there were
machines that measured disk space in odd-sized "words" or "pages".
How many bytes are 1000 36-bit words?

Not that any of this has a relation to Perl...

Anno
 
T

tommo_blade

To clarify, I need to get this data using the Net::FTP module and not
by some preliminary perl script as has been mentioned in this thread.
Does anyone know if there are any other add-on modules to the Net::FTP
module that could get this data, i.e. Net::FTP::Common etc, etc.

cheers, Mark.
 
J

John Bokma

tommo_blade said:
To clarify, I need to get this data using the Net::FTP module and not
by some preliminary perl script as has been mentioned in this thread.
Does anyone know if there are any other add-on modules to the Net::FTP
module that could get this data, i.e. Net::FTP::Common etc, etc.

There is no reliable way, even if it was supported by FTP unless the ftp
server would stop all processes that can generate data on your partition.

While you request how much free space there is, the information will
already be outdated by the time you start the upload. During the upload
another process can generate data. Imagine two programs doing the same:

free space = 1GB

program 1 asks free space and gets 1GB
program 2 asks free space and gets 1GB
program 1 starts to upload 700 MB
program 2 starts to upload 700 MB
???
profit
 
B

Bob Walton

tommo_blade wrote:
....
I have read thru the perl docs for this module and I have
tried a few of the 'methods' available but as yet I have not been able
to get my script to pull the data I need from the server. basically I
want to know if there is enough 'available space' on a unix partition
prior to me FTP'ing a file to that host/partition, I have tried the
'ls' and 'dir' methods but I cannot get what I need. The data I need at
unix level can be gained by running a 'df -k' command which when run
returns the available disk space in the 'avail' column.

Does anyone know of any neat tricks to get this data.

Well, I don't know how "neat" it is, but you can use Net::FTP's quot()
method to execute a command on the remote system. I use it to chmod CGI
programs to executable on ISP systems that don't provide command line
access, for example (yeah, I know, I could get a better ISP). To get
the result of the command returned back to you, you will probably need
to redirect the STDOUT of the command to a file, then use the get()
method to get the file, then open and read it. I don't know if the
remote FTP server will run operating system commands with STDOUT
redirected -- you'll have to try it and see. You can check out how it
works with a command-line FTP client -- when you see you can do what you
want with a manual client, then implement it using Net::FTP.

You might be just as well off to do your put() and check the return for
an error.

HTH.
 
T

tommo_blade

Thanks to all who replied, I think I will go with the suggestion of
sticking the file in regardless and then checking that it is the same
size (->size()) as the source file.
 
D

Dr.Ruud

tommo_blade schreef:
Thanks to all who replied, I think I will go with the suggestion of
sticking the file in regardless and then checking that it is the same
size (->size()) as the source file.

If the transport is compressed, and the quota-calculation isn't, you
could test by first sending the file with all 0 bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top