Check FTP connection

Hi,

I use curl to ftp PDFs to a network printer. Occasionally the printer rip crashes or the ftp services stop. This causes problems with my workflow and so I would like a way to check the connection, before the script attempts to upload the files, maybe just a simple login and then disconnect. If there’s a connection problem then I can instruct my script to ftp to another printer.

Thanks for looking,
Nik

Hi,

try this small command line tool I wrote

reachable

save it somewhere and call it with


set isReachable to (do shell script "/full/path/to/reachable [url=ftp://ftp.thePrinter.local]ftp.thePrinter.local[/url]") as boolean

the CLI uses the Reachability API of the SystemConfiguration framework.
The argument can be an URL or a bonjour host name. IPv4 Addresses might not work
It can take a few seconds to return the result (true or false).

Hi Stefan,

Many thanks for your reply and apologies for taking so long to reply.

I have been testing this morning and the reachable CLI is always returning true when I know the server is down/rebooting!

I logged onto the printer and rebooted the server, then I simultaneously ran the below script and kept refreshing a cyberduck connection to the server. Each time I ran the script it returned true but when I refreshed the Cyberduck connection it failed until the server had rebooted.

set isReachable to (do shell script "reachable ftp://anonymous@126.0.17.132/") as boolean

I put the binary in /usr/bin

Any ideas why this would still be returning true when the server is down?

Many Thanks,
Nik

Can the printer be accessed by name?

Hello.

If there is no other way, then I think you can curl the status page of Cups, and read out the status of the queue that way.

There should be a post somewhere here, doing just that. (If everything else should fail.)

Hi Stefan,

just checked the printer settings and it does have a name, but do you mean a dns name or a name entered for the printer in my hosts file or some other name associated with printer?

Thanks,
Nik

Hi,

after a bit more searching and testing I came up with this:

set ftpStatus to true
try
	set curlResponse to do shell script "curl --connect-timeout 5 ftp://126.0.17.131"
on error theError
	set ftpStatus to false
end try

Many thanks again for your time Stefan & McUsrII,
Nik

Very useful, I made a general handler out of it.

# works with both http and ftp protocols, 
# that is, names and IP's

hostUP for "http://macscripter.net"

on hostUP for anAddress
	set hostStatus to true
	try
		set curlResponse to do shell script "curl --connect-timeout 5  " & anAddress
	on error theError
		set hostStatus to false
	end try
	return hostStatus
end hostUP

Hi,

I’ve also created a handler, because it’s running on a remote machine I got the script to email me when one of the printers is down:

set printerIP to my checkPrinterFtpStatus()

on checkPrinterFtpStatus()
	try
		do shell script "curl --connect-timeout 5 ftp://126.0.17.131"
		set printerIP to "126.0.17.131"
	on error theError
		my printerFtpError("Fiery1")
		try
			do shell script "curl --connect-timeout 5 ftp://126.0.17.132"
			set printerIP to "126.0.17.132"
		on error theError
			my printerFtpError("Fiery2")
			set printerIP to "dead"
		end try
	end try
	return printerIP
end checkPrinterFtpStatus

on printerFtpError(Fiery)
	try
		set messTo to "macadmin@myemail.co.uk"
		set messSub to Fiery & " ftp services have stopped running!"
		set messBody to Fiery & " ftp services have stopped running!" & return & "Please reboot the " & Fiery & " server."
		do shell script "echo " & (quoted form of messBody) & " | mail -s " & (quoted form of messSub) & space & messTo & space & " -f Fast_Track_Proofing2"
	on error
		display dialog theError giving up after 10
		error number -128
	end try
end printerFtpError

Thanks,
Nik