File: util.py

package info (click to toggle)
zeekctl 2.2.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,544 kB
  • sloc: python: 5,639; sh: 1,374; makefile: 71; awk: 24
file content (53 lines) | stat: -rw-r--r-- 1,584 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import errno

from ZeekControl import config


# 'src' is the file to which the link will point, and 'dst' is the link to make
def force_symlink(src, dst):
    try:
        os.symlink(src, dst)
    except OSError as e:
        if e.errno == errno.EEXIST:
            os.remove(dst)
            os.symlink(src, dst)
        else:
            raise

# Returns an IP address string suitable for embedding in a Zeek script,
# for IPv6 colon-hexadecimal address strings, that means surrounding it
# with square brackets.
def format_zeek_addr(addr):
    if ":" not in addr:
        return addr
    else:
        return "[%s]" % addr

# Returns an IP prefix string suitable for embedding in a Zeek script,
# for IPv6 colon-hexadecimal prefix strings, that means surrounding the
# IP address part with square brackets.
def format_zeek_prefix(prefix):
    if ":" not in prefix:
        return prefix
    else:
        parts = prefix.split("/")
        return "[%s]/%s" % (parts[0], parts[1])

# Returns an IP address string suitable for use with rsync, which requires
# encasing IPv6 addresses in square brackets, and some shells may require
# quoting the brackets.
def format_rsync_addr(addr):
    if ":" not in addr:
        return addr
    else:
        return "'[%s]'" % addr

# Convert a number into a string with a unit (e.g., 1024 into "1K").
def number_unit_str(num):
    units = (("G", 1024*1024*1024), ("M", 1024*1024), ("K", 1024))
    for (unit, factor) in units:
        if num >= factor:
            return "%3.0f%s" % (num / factor, unit)
    return " %3.0f" % (num)