summaryrefslogtreecommitdiff
path: root/src/op_mode/anyconnect-control.py
blob: 6382016b71e9fc3c8369bb6b6ab1cc357890b4c4 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
#
# Copyright (C) 2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import argparse
import json

from vyos.config import Config
from vyos.util import popen, run, DEVNULL
from tabulate import tabulate

occtl        = '/usr/bin/occtl'
occtl_socket = '/run/ocserv/occtl.socket'

def show_sessions():
    out, code = popen("sudo {0} -j -s {1} show users".format(occtl, occtl_socket),stderr=DEVNULL)
    if code:
        sys.exit('Cannot get anyconnect users information')
    else:
        headers = ["interface", "username", "ip", "remote IP", "RX", "TX", "state", "uptime"]
        sessions = json.loads(out)
        ses_list = []
        for ses in sessions:
            ses_list.append([ses["Device"], ses["Username"], ses["IPv4"], ses["Remote IP"], ses["_RX"], ses["_TX"], ses["State"], ses["_Connected at"]])
        if len(ses_list) > 0:
            print(tabulate(ses_list, headers))
        else:
            print("No active anyconnect sessions")

def is_ocserv_configured():
    if not Config().exists_effective('vpn anyconnect'):
        print("vpn anyconnect server is not configured")
        sys.exit(1)

def main():
    #parese args
    parser = argparse.ArgumentParser()
    parser.add_argument('--action', help='Control action', required=True)
    parser.add_argument('--selector', help='Selector username|ifname|sid', required=False)
    parser.add_argument('--target', help='Target must contain username|ifname|sid', required=False)
    args = parser.parse_args()


    # Check is IPoE configured
    is_ocserv_configured()

    if args.action == "restart":
        run("systemctl restart ocserv")
        sys.exit(0)
    elif args.action == "show_sessions":
        show_sessions()

if __name__ == '__main__':
    main()