dave9909
dave9909

Reputation: 129

Network - testing connectivity

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.

Upvotes: 1

Views: 930

Answers (2)

Doug F
Doug F

Reputation: 348

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)

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838276

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.

Upvotes: 1

Related Questions