Created
May 4, 2017 14:05
-
-
Save sheagcraig/95c957b879edb97da29d8fa047f5d3f9 to your computer and use it in GitHub Desktop.
Get IP Address for all computers in the JSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
# 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 | |
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