Skip to content

Instantly share code, notes, and snippets.

@jeffmcjunkin
Created January 29, 2019 01:13
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 jeffmcjunkin/c51b433fc1a370d7a443e3a5d552e500 to your computer and use it in GitHub Desktop.
Save jeffmcjunkin/c51b433fc1a370d7a443e3a5d552e500 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import getpass
import os, stat
from neo4j import GraphDatabase, basic_auth
import sys
def set_computer_owned(computer):
with driver.session() as session:
session.run("MATCH (c:Computer) "
"WHERE LOWER(c.name) = LOWER({computer}) " # Index-preserving case-insensitive search from https://stackoverflow.com/a/41489087/372377
"SET c.owned = True", computer=computer)
if __name__ == "__main__":
# Make sure input is redirected
mode = os.fstat(0).st_mode
if not stat.S_ISFIFO(mode) and not stat.S_ISREG(mode):
print("ERROR: stdin is directly from a terminal")
print("USAGE: cat computers.txt | %s" % sys.argv[0])
exit(-1)
print("Welcome! This tool will mark computers as owned via STDIN")
uri = "bolt://127.0.0.1:7687"
print("Prepare DB connection to: %s" %(uri))
pswd = getpass.getpass('Password: ')
auth_token = basic_auth("neo4j", pswd)
driver = GraphDatabase.driver(uri, auth=auth_token)
for line in sys.stdin:
computer = line.rstrip()
print("Marking %s as high-value" % computer)
set_computer_owned(computer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment