Help - Search - Members - Calendar
Full Version: Example: Connecting to FTP server
Intellipool Network Monitor - Forum > Intellipool Network Monitor > Lua scripts
RA
CODE
----------------------------------------------------------------------------------------------------------
-- Name: Ftptest.lua
-- Author: Intellipool AB
-- Version: 2.0
-- Date: 2006-02-16
-- Description: The script connects to a FTP server and checks if a file exists
-- it can optionaly validate the size of the file.
-- Arguments:
-- Username
-- Password
-- FTP Port number
-- File name
-- Min size in bytes (optional)
----------------------------------------------------------------------------------------------------------
iArgumentCount = GetArgumentCount()
if iArgumentCount < 5 then
  SetExitStatus("To few arguments",false)
  return
end
ftp = TLuaFTPClient:new();

-- Enter the username and password for the session
sUsername = GetArgument(0)
sPassword = GetArgument(1)
iPort = tonumber(GetArgument(2))
sFilename = GetArgument(3)
iMinSize = -1
if iArgumentCount > 4 then
  iMinSize = tonumber(GetArgument(4))
end
-- Connect to FTP server using username and password
iRet = ftp:Connect(sUsername,sPassword,iPort)
-- Check return value from server
if iRet == 0 then
  -- Failed to connect, print why
  iRet = GetLastError()
  sErrorString = FormatErrorString(iRet)
  sError = "Error when connecting to FTP server, error: "..sErrorString
  SetExitStatus(sError,false);
  ftp:Close()
else
  -- Check size if needed
  iSize = ftp:GetFileSize(sFilename);
  print("Size"..iSize)
  if iMinSize ~= -1 then
     -- File found, check if its less then specified
     if iSize < iMinSize then
        SetExitStatus("File less then specifed size",false)
        ftp:Close()
        return
     end
     SetExitStatus("Test ok",true);
     ftp:Close()
  else
     -- File not found
     if iSize == -1 then
        SetExitStatus("File does not exsist",false)
        ftp:Close()
        return
     end
     SetExitStatus("Test ok",true);
     ftp:Close()
  end
end
ftp:Close()
Wintermute
Correct me if I'm wrong but shouldn't the line:
if iArgumentCount < 5 then
read
if iArgumentCount < 4 then
since "min size in bytes" is optional?

Also the file size check seems to check in KB not bytes from my test.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.