Skip to content

Instantly share code, notes, and snippets.

@jbaker10
Created February 23, 2016 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaker10/9e6c41a2637d7c05faaa to your computer and use it in GitHub Desktop.
Save jbaker10/9e6c41a2637d7c05faaa to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import logging
import time
import os
import sys
import syslog
import subprocess
from subprocess import Popen
import plistlib
import signal
AMSERVER = "AM.nowhere.com"
AMDIR = "/Library/Application Support/LANrev Agent"
AMAGENT = AMDIR + "/LANrev Agent.app/Contents/MacOS/LANrev Agent"
LOGFILE = "/var/log/gov.nih.nimh.onset.log"
i = 1
def bashCommand(script):
try:
return subprocess.check_output(script)
except (subprocess.CalledProcessError, OSError), err:
return "[* Error] **%s** [%s]" % (err, str(script))
def pingCheck():
try:
subprocess.check_call(["/sbin/ping", "-q", "-c", "1", AMSERVER])
except (subprocess.CalledProcessError, OSError), err:
print "\n**********************\n"
print "Unable to reach " + AMSERVER
print "\n**********************\n"
global i
i = 0
def checkAvailableAdapters():
pingCheck()
global i
if i == 1:
return
print "The list of available network interfaces is:"
availableNetworks = bashCommand(["/usr/sbin/networksetup", "-listallnetworkservices"])
print""
print availableNetworks
if "Thunderbolt Ethernet" in availableNetworks:
print "\nThe Thunderbolt Ethernet interface adapter has been detected. Will proceed with rest of script.\n"
return
else:
print "Did not detect a Thunderbolt Ethernet interface. Will attempt to create it now"
createThunderboltEth()
# if "Thunderbolt Ethernet" in availableNetworks:
# print ""
# print "The Thunderbolt Ethernet adapter has been detected. Will proceed with rest of script."
# print ""
# else:
# print ""
# print "**********************"
# print "Unable to detect a network adapter automatically. Will try to manually set one up."
# print "**********************"
# print ""
# createThunderboltEth()
def createThunderboltEth():
print "\n**********************"
print "\nAttempting to create a Thunderbolt Ethernet connection."
print "\n**********************"
bashCommand(["/usr/sbin/networksetup", "-createnetworkservice", "Thunderbolt Ethernet", "en3"])
time.sleep(10)
availableNetworks = bashCommand(["/usr/sbin/networksetup", "-listallnetworkservices"])
print availableNetworks
if "Thunderbolt Ethernet" in availableNetworks:
print "\nThe Thunderbolt Ethernet adapter has been successfully added\n"
print "\nSleeping 10 seconds to allow interface to get IP\n"
time.sleep(10)
print "\nAttempting to ping with new interface\n"
pingCheck()
global i
if i == 0:
print "\nStill unable to reach the internal server, will try to add the USB Ethernet and connect with that\n"
createUSBEth()
else:
print "\nWe are now able to communicate with the " + AMSERVER + ". Will proceed with rest of script\n"
else:
print "\nThe Thunderbolt Ethernet adapter was not added, will attempt to create USB Ethernet\n"
createUSBEth()
def createUSBEth():
print "\n**********************"
print "\nAttempting to create a USB Ethernet connection."
print "\n**********************"
bashCommand(["/usr/sbin/networksetup", "-createnetworkservice", "USB Ethernet", "en5"])
time.sleep(2)
availableNetworks = bashCommand(["/usr/sbin/networksetup", "-listallnetworkservices"])
if "USB Ethernet" in availableNetworks:
print "\nThe USB Ethernet adapter has been successfully added"
print "\nSleeping 10 seconds to allow interface to get IP"
time.sleep(10)
print "\nAttempting to ping with new interface\n"
pingCheck()
global i
if i == 0:
print "\nUnable to connect to the internal server."
print "\nPlease make sure a network adapter is connected."
print "\nThe script will cycle through every 10 seconds and try to add network adapters."
print "\nIf a network adapter is plugged in, try removing it and plugging it back in and wait 10 seconds."
print "\nIf the script is STILL not working, quit the app (Cmd+Q) and login to the machine\n"
time.sleep(10)
checkAvailableAdapters()
else:
print "\nWe are now able to communicate with the " + AMSERVER + ". Will proceed with rest of script\n"
else:
print "\n**********************"
print "\nThe attempt to add the USB Ethernet adapter failed. Please make sure a network adapter is connected."
print "\nThe script will cycle through every 10 seconds and retry to add network adapters."
print "\nIf a network adapter is plugged in, try removing it and pluggin it back in and wait 10 seconds."
print "\nIf the script is STILL not working, quit the app (Cmd+Q) and login to the machine."
print "\n**********************"
time.sleep(10)
checkAvailableAdapters()
def waitForPath():
while os.path.exists(AMAGENT) != True:
print "..."
time.sleep(5)
def waitForPing():
print "Waiting for " + AMSERVER + " to respond."
pingCheck()
while i == 0:
time.sleep(1)
pingCheck()
## Will need to come back to this as I'm confused as all get out.
def waitAMagentIdle():
sdstate = ""
while True:
amstatus = bashCommand([AMAGENT, "--GetSDState"])
#returncode = bashCommand(["$?"])
print amstatus
#print returncode
if int(amstatus) == 0:
return
else:
newstate = bashCommand([AMAGENT, "--GetSDStateString"])
if str(newstate) != str(sdstate):
sdstate = newstate
print sdstate
time.sleep(5)
def amSendInventory():
waitAMagentIdle()
print "\n**********************"
print "\nSending Inventory"
print "\n**********************"
bashCommand([AMAGENT, "--SendInventory"])
time.sleep(5)
print "\n**********************"
print "\nInventory was successfully sent"
print "\n**********************"
def amSDCheck():
waitAMagentIdle()
print "\n**********************"
print "\nChecking Software Distribution"
print "\n**********************"
bashCommand([AMAGENT, "--SDCheck"])
time.sleep(5)
print "\n**********************"
print "\nThe software installation will now begin"
print "\n**********************"
def enableAMLogging():
print "\n**********************"
print "\nEnable AM Debug Logs"
print "\n**********************"
bashCommand(["/usr/bin/defaults", "write", "/Library/Preferences/com.poleposition-sw.lanrev_agent", "DefaultLogLevel", "6"])
bashCommand(["/bin/launchctl", "unload", "/Library/LaunchDaemons/com.poleposition-sw.LANrevAgent.plist"])
bashCommand(["/bin/launchctl", "load", "/Library/LaunchDaemons/com.poleposition-sw.LANrevAgent.plist"])
##bashCommand(["/usr/bin/tail", "-f", "/Library/Logs/LANrev Agent.log", ">", "/var/log/gov.nih.nimh.onset.log", "&"])
##childPID = subprocess.call(["/usr/bin/tail", "-f", "/Library/Logs/LANrev Agent.log", ">", "/var/log/gov.nih.nimh.onset.log"])
cmd = "/usr/bin/tail -f /Library/Logs/LANrev\ Agent.log > /var/log/gov.nih.nimh.onset.log"
global pro
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
def disableAMLogging():
print "\n**********************"
print "\nDisable AM Debug Logs"
print "\n**********************"
bashCommand(["/usr/bin/defaults", "write", "/Library/Preferences/com.poleposition-sw.lanrev_agent", "DefaultLogLevel", "5"])
bashCommand(["/bin/launchctl", "unload", "/Library/LaunchDaemons/com.poleposition-sw.LANrevAgent.plist"])
bashCommand(["/bin/launchctl", "load", "/Library/LaunchDaemons/com.poleposition-sw.LANrevAgent.plist"])
os.killpg(pro.pid, signal.SIGTERM)
def checkIfRoot():
if os.geteuid != 0:
print "Not root, please re-run as root."
def main():
checkIfRoot()
pingCheck()
j = 1
name = bashCommand(["/usr/sbin/scutil", "--get", "ComputerName"])
datestamp = time.strftime("%x %I:%M:%S")
print "\n**********************"
print "\nAbsolute Manage kickstart for " + name + " " + datestamp
print "\n**********************"
time.sleep(5)
waitForPath()
waitForPing()
enableAMLogging()
for i in 2:
amSendInventory()
amSDCheck()
print "Running"
disableAMLogging()
print "\n**********************"
print "\nKickstart done " + datestamp
print "\n**********************"
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment