Page Menu
Home
VyOS Platform
Search
Configure Global Search
Log In
Files
F119895025
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
9 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/data/op-mode-standardized.json b/data/op-mode-standardized.json
index b5e9308c5..5f49be781 100644
--- a/data/op-mode-standardized.json
+++ b/data/op-mode-standardized.json
@@ -1,12 +1,13 @@
[
"bridge.py",
"conntrack.py",
"container.py",
"cpu.py",
"memory.py",
"nat.py",
"neighbor.py",
+"openconnect.py",
"route.py",
"version.py",
"vrf.py"
]
diff --git a/op-mode-definitions/openconnect.xml.in b/op-mode-definitions/openconnect.xml.in
index 9343637c0..88e1f9f15 100644
--- a/op-mode-definitions/openconnect.xml.in
+++ b/op-mode-definitions/openconnect.xml.in
@@ -1,77 +1,77 @@
<?xml version="1.0"?>
<interfaceDefinition>
<node name="show">
<children>
<node name="openconnect-server">
<properties>
<help>Show OpenConnect server information</help>
</properties>
<children>
<leafNode name="sessions">
<properties>
<help>Show active OpenConnect server sessions</help>
</properties>
- <command>${vyos_op_scripts_dir}/openconnect-control.py --action="show_sessions"</command>
+ <command>${vyos_op_scripts_dir}/openconnect.py show_sessions</command>
</leafNode>
<tagNode name="user">
<properties>
<help>Show OpenConnect configured user settings</help>
<completionHelp>
<script>sudo ${vyos_completion_dir}/list_openconnect_users.py</script>
</completionHelp>
</properties>
<children>
<node name="otp">
<properties>
<help>Show OTP key information</help>
</properties>
<children>
<leafNode name="full">
<properties>
<help>Show full settings, including QR code and commands for VyOS</help>
</properties>
<command>${vyos_op_scripts_dir}/show_openconnect_otp.py --user="$4" --info="full"</command>
</leafNode>
<leafNode name="key-hex">
<properties>
<help>Show OTP authentication secret in Hex (used in VyOS config)</help>
</properties>
<command>${vyos_op_scripts_dir}/show_openconnect_otp.py --user="$4" --info="key-hex"</command>
</leafNode>
<leafNode name="key-b32">
<properties>
<help>Show OTP authentication secret in Base32 (used in mobile apps)</help>
</properties>
<command>${vyos_op_scripts_dir}/show_openconnect_otp.py --user="$4" --info="key-b32"</command>
</leafNode>
<leafNode name="qrcode">
<properties>
<help>Show OTP authentication QR code</help>
</properties>
<command>${vyos_op_scripts_dir}/show_openconnect_otp.py --user="$4" --info="qrcode"</command>
</leafNode>
<leafNode name="uri">
<properties>
<help>Show OTP authentication otpauth URI</help>
</properties>
<command>${vyos_op_scripts_dir}/show_openconnect_otp.py --user="$4" --info="uri"</command>
</leafNode>
</children>
</node>
</children>
</tagNode>
</children>
</node>
</children>
</node>
<node name="restart">
<children>
<leafNode name="openconnect-server">
<properties>
<help>Restart openconnect server process</help>
</properties>
<command>${vyos_op_scripts_dir}/openconnect-control.py --action="restart"</command>
</leafNode>
</children>
</node>
</interfaceDefinition>
diff --git a/src/op_mode/openconnect-control.py b/src/op_mode/openconnect-control.py
index a128cc011..20c50e779 100755
--- a/src/op_mode/openconnect-control.py
+++ b/src/op_mode/openconnect-control.py
@@ -1,74 +1,69 @@
#!/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 commit_in_progress
from vyos.util import popen
from vyos.util import run
from vyos.util import 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 openconnect 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 openconnect sessions")
def is_ocserv_configured():
if not Config().exists_effective('vpn openconnect'):
print("vpn openconnect 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 Openconnect server configured
is_ocserv_configured()
- if commit_in_progress():
- print('Cannot restart openconnect while a commit is in progress')
- exit(1)
-
if args.action == "restart":
run("sudo systemctl restart ocserv.service")
sys.exit(0)
elif args.action == "show_sessions":
show_sessions()
if __name__ == '__main__':
main()
diff --git a/src/op_mode/openconnect.py b/src/op_mode/openconnect.py
new file mode 100755
index 000000000..00992c66a
--- /dev/null
+++ b/src/op_mode/openconnect.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2022 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 json
+
+from tabulate import tabulate
+from vyos.configquery import ConfigTreeQuery
+from vyos.util import rc_cmd
+
+import vyos.opmode
+
+
+occtl = '/usr/bin/occtl'
+occtl_socket = '/run/ocserv/occtl.socket'
+
+
+def _get_raw_data_sessions():
+ rc, out = rc_cmd(f'sudo {occtl} --json --socket-file {occtl_socket} show users')
+ if rc != 0:
+ output = {'openconnect':
+ {
+ 'configured': False,
+ 'return_code': rc,
+ 'reason': out
+ }
+ }
+ return output
+
+ sessions = json.loads(out)
+ return sessions
+
+
+def _get_formatted_sessions(data):
+ headers = ["Interface", "Username", "IP", "Remote IP", "RX", "TX", "State", "Uptime"]
+ ses_list = []
+ for ses in data:
+ 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:
+ output = tabulate(ses_list, headers)
+ else:
+ output = 'No active openconnect sessions'
+ return output
+
+
+def show_sessions(raw: bool):
+ config = ConfigTreeQuery()
+ if not config.exists('vpn openconnect') and not raw:
+ print('Openconnect is not configured')
+ exit(0)
+
+ openconnect_data = _get_raw_data_sessions()
+ if raw:
+ return openconnect_data
+ return _get_formatted_sessions(openconnect_data)
+
+
+if __name__ == '__main__':
+ try:
+ res = vyos.opmode.run(sys.modules[__name__])
+ if res:
+ print(res)
+ except (ValueError, vyos.opmode.Error) as e:
+ print(e)
+ sys.exit(1)
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Nov 26, 7:41 PM (17 h, 20 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4979280
Default Alt Text
(9 KB)
Attached To
Mode
rVYOSONEX vyos-1x
Attached
Detach File
Event Timeline
Log In to Comment