tags:

views:

52

answers:

2

Let's say I want to see if my ftp server is online, how could I do this in a program. Also, what do you think would be the easiest least intrusive way.

+1  A: 

Connect to the port of your FTP server to see if it is accepting connections.

If you want to go one step further you could send an ls command and check that you get a sensible response.

If you want to do it in Python you can use ftplib.

Mark Byers
What if I wanted to do this to just any computer, to see if it is online?
dave9909
@dave9909: Then ping it.
Mark Byers
+1  A: 

Personally, I would try nmap first to do this, http://nmap.org.

nmap $HOSTNAME -p 21

To test port 21 (ftp) on a list of servers in python might look like this:

#!/usr/bin/env python  
from socket import *   

host_list=['localhost', 'stackoverflow.com']

port=21 # (FTP port)

def test_port(ip_address, port, timeout=3):
    s = socket(AF_INET, SOCK_STREAM)
    s.settimeout(timeout)
    result = s.connect_ex((ip_address, port))
    s.close()
    if(result == 0):
        return True
    else:
        return False

for host in host_list:
    if test_port(gethostbyname(host), port):
        print 'Successfully connected to',
    else:
        print 'Failed to connect to',
    print '%s on port %d' % (host, port)
Goladus