Ejemplo n.º 1
0
def ExportClientsByKeywords(keywords, filename, token=None):
    r"""A script to export clients summaries selected by a keyword search.

  This script does a client search for machines matching all of keywords and
  writes a .csv summary of the results to filename. Multi-value fields are '\n'
  separated.

  Args:
    keywords: a list of keywords to search for
    filename: the name of the file to write to, will be replaced if already
      present
    token: datastore token.
  """
    index = client_index.CreateClientIndex(token=token)
    client_list = index.LookupClients(keywords)
    logging.info("found %d clients", len(client_list))
    if not client_list:
        return

    writer = csv.DictWriter([
        u"client_id",
        u"hostname",
        u"last_seen",
        u"os",
        u"os_release",
        u"os_version",
        u"users",
        u"ips",
        u"macs",
    ])
    writer.WriteHeader()

    for client in aff4.FACTORY.MultiOpen(client_list, token=token):
        s = client.Schema
        writer.WriteRow({
            u"client_id": client.urn.Basename(),
            u"hostname": client.Get(s.HOSTNAME),
            u"os": client.Get(s.SYSTEM),
            u"os_release": client.Get(s.OS_RELEASE),
            u"os_version": client.Get(s.OS_VERSION),
            u"ips": client.Get(s.HOST_IPS),
            u"macs": client.Get(s.MAC_ADDRESS),
            u"users": "\n".join(client.Get(s.USERNAMES, [])),
            u"last_seen": client.Get(s.PING),
        })

    with io.open(filename, "w") as csv_out:
        csv_out.write(writer.Content())
Ejemplo n.º 2
0
    def testRaisesOnMissingColumn(self):
        writer = csv.DictWriter(["foo", "bar", "baz"])

        with self.assertRaises(ValueError):
            writer.WriteRow({"foo": "quux", "bar": "norf"})
Ejemplo n.º 3
0
    def testEmpty(self):
        writer = csv.DictWriter(["foo", "bar", "baz"])

        self.assertEqual(writer.Content(), "")
Ejemplo n.º 4
0
    def testWriteHeader(self):
        writer = csv.DictWriter(["A", "B", "C"])
        writer.WriteHeader()
        writer.WriteRow({"A": "foo", "B": "bar", "C": "baz"})

        self.assertEqual(writer.Content(), "A,B,C\nfoo,bar,baz\n")
Ejemplo n.º 5
0
    def testIrrelevantOrder(self):
        writer = csv.DictWriter(["1", "2", "3"])
        writer.WriteRow({"1": "a", "2": "b", "3": "c"})
        writer.WriteRow({"3": "d", "2": "e", "1": "f"})

        self.assertEqual(writer.Content(), "a,b,c\nf,e,d\n")
Ejemplo n.º 6
0
    def testCustomDelimiter(self):
        writer = csv.DictWriter(["1", "2", "3"], delimiter=" ")
        writer.WriteRow({"1": "a", "2": "b", "3": "c"})

        self.assertEqual(writer.Content(), "a b c\n")
Ejemplo n.º 7
0
    def testMultipleRows(self):
        writer = csv.DictWriter(["x", "y", "z"])
        writer.WriteRow({"x": "foo", "y": "bar", "z": "baz"})
        writer.WriteRow({"x": "quux", "y": "norf", "z": "blargh"})

        self.assertEqual(writer.Content(), "foo,bar,baz\nquux,norf,blargh\n")
Ejemplo n.º 8
0
    def testSingleRow(self):
        writer = csv.DictWriter(["foo", "bar", "baz"])
        writer.WriteRow({"foo": "quux", "bar": "norf", "baz": "blargh"})

        self.assertEqual(writer.Content(), "quux,norf,blargh\n")