Skip to content

Instantly share code, notes, and snippets.

@sheagcraig
Created May 4, 2017 14:05
Show Gist options
  • Save sheagcraig/95c957b879edb97da29d8fa047f5d3f9 to your computer and use it in GitHub Desktop.
Save sheagcraig/95c957b879edb97da29d8fa047f5d3f9 to your computer and use it in GitHub Desktop.
Get IP Address for all computers in the JSS
#!/usr/bin/env python
"""https://twitter.com/krispayne/status/859833552078225408:
@shea_craig is possible with python-jss to generate a list of IP addresses for
computers in the JSS?
"""
from operator import itemgetter
# This example uses python-jss 2.0.0+
import jss
def main():
# Configure the JSS API object. Fill in with your information.
# You will need the add the JSS' certificate to your keychain and trust
# it.
j = jss.JSS(
url="https://tacos.com:8443", user="python-jss", password="abc123",
ssl_verify=True)
# Alternate form, if you have built preferences already (see the docs):
# j = jss.JSS(jss.JSSPrefs())
# Query the JSS for all Computer objects. We use the subset argument to
# minimize the amount of data retrieved to just the 'general' data,
# which includes IP address.
computers = j.Computer(subset='general')
# Build a list of the IP address value from each computer's general
# section. You can descend the tag tree with dot syntax notation.
ip_addresses = [comp.general.ip_address.text for comp in computers]
# Or as 3-tuples of hostname, serial, IP...
detailed = [
(comp.name,
comp.general.serial_number.text,
comp.general.ip_address.text) for comp in computers]
# Show it!
for index, ip_address in enumerate(ip_addresses):
print '{:>3}: {:>14}'.format(index, ip_address)
print
# Show it showing off!
# Build a dict of max lengths per column for table output.
lengths = [0, 0, 0]
for computer in detailed:
for index, item in enumerate(computer):
length = len(item)
if length > lengths[index]:
lengths[index] = length
fmt = (
'| {index:>5} | {name:>{name_width}} | {serial:>{serial_width}} | '
'{ip_addr:>14} |')
name_width, serial_width, _ = lengths
header = fmt.format(
index='Index', name='Name', name_width=lengths[0],
serial='Serial', serial_width=lengths[1], ip_addr='IP Address')
head_length = len(header)
bar = '-' * head_length
print bar
print header
print bar
for index, data in enumerate(sorted(detailed, key=itemgetter(0))):
name, serial, ip_addr = data
print fmt.format(
index=index, name=name, name_width=name_width, serial=serial,
serial_width=serial_width, ip_addr=ip_addr)
print bar
print
tacos = (u'\U0001F32E' * head_length).encode('utf-8')
print tacos
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment