Auto find serial port, script problem

How can I tell if a port is used without attempting to connect to it?