56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
from ldap3 import Server, Connection, ALL, SUBTREE
|
||
import pandas as pd
|
||
|
||
# LDAP connection details from the wiki
|
||
server_url = 'ldap://207.246.98.219:389' # Server and port (no TLS, as per "TLS Mode: LDAP or No" – use 'ldaps://' if TLS needed)
|
||
search_base = 'ou=people,dc=hamsoverip,dc=com'
|
||
search_filter = '(telephoneNumber=*)' # Fetch all entries with an extension; adjust if needed, e.g., '(objectClass=person)'
|
||
attributes = ['cn', 'sn', 'telephoneNumber'] # Key attributes; add more if you discover others like 'callsign'
|
||
|
||
# Connect anonymously (no auth)
|
||
server = Server(server_url, get_info=ALL)
|
||
conn = Connection(server, auto_bind=True) # No user/password
|
||
|
||
# Perform the search
|
||
conn.search(
|
||
search_base=search_base,
|
||
search_filter=search_filter,
|
||
search_scope=SUBTREE,
|
||
attributes=attributes
|
||
)
|
||
|
||
# Extract data
|
||
data = []
|
||
for entry in conn.entries:
|
||
extension = entry.telephoneNumber.value if 'telephoneNumber' in entry else ''
|
||
cn_value = entry.cn.value if 'cn' in entry else ''
|
||
sn_value = entry.sn.value if 'sn' in entry else ''
|
||
|
||
# clean up data
|
||
hoipnumber = extension.partition(' ')[0]
|
||
callsign = cn_value.partition(' ')[0]
|
||
hamname = sn_value.partition(' ')[0]
|
||
|
||
if hoipnumber: # Only include entries with extensions
|
||
data.append({
|
||
'HOIP Extension': hoipnumber,
|
||
'Call': callsign,
|
||
'Name': hamname # Include raw for reference
|
||
})
|
||
|
||
# Save to CSV
|
||
if data:
|
||
df = pd.DataFrame(data)
|
||
|
||
# CSV output for debugging...
|
||
df.to_csv('hoip_subscribers.csv', index=False)
|
||
print(f'Saved {len(data)} entries to hoip_subscribers.csv')
|
||
|
||
# Excel output for production...
|
||
df.to_excel('hoip_subscribers.xlsx', index=False)
|
||
print(f'Saved {len(data)} entries to hoip_subscribers.xlsx')
|
||
else:
|
||
print('No entries found – check filter or connection.')
|
||
|
||
# Unbind
|
||
conn.unbind()
|