答案1
实际上,您可以在服务器端的配置文件中使用:
# server vpn interface is up
up "/script/server_up.sh"
# server vpn interface is going down
down "/script/server_down.sh"
# client connected to VPN server
client-connect "script/client_connect.sh"
# client disconnected from VPN server
client-disconnect "script/client_disconnect.sh"
在客户端你将使用:
# Client connected to VPN server
up "script/connected.sh"
# Client disconnected from VPN server
down "script/disconnected.sh"
OpenVPN 会传递很多环境变量到你的 shell 脚本中,你可以使用它来做任何你想做的事情。
我之前有一个名为server_up.sh
“Hurricane Electric”的脚本,用于设置 IPv6 隧道。
假设 IP 地址 2001:db8::1 是 Hurricane Electric IPv6 标准网关,子网 2001:db8:cafe::/48 是路由到我的 IPv6 子网。
那么的内容script/server_up.dh
就会有点像这样:
#!/bin/bash
ip tunnel add he-ipv6 mode sit remote TUNNELBROKER.IPV4.IP.ADDRESS local MY.IPV4.IP.ADDRESS ttl 255
ip link set he-ipv6 up
ip -6 route add default via 2001:db8::1 dev he-ipv6 table openvpn
# Reset ALL ipv6 routes
ip -6 rule flush
# Reinitialise the main IPv6 routing table (inbound traffic) because of reset above
ip -6 rule add priority 32766 from all table main
# Reset OpenVPN routing table (outbound traffic)
ip -6 route flush table openvpn
# Add default unreachable route for any ipv6 subnet not in use.
ip -6 route add unreachable 2001:db8:cafe::/48 table main
ip -6 route add unreachable 2001:db8:cafe::/48 table openvpn
# Add rule to lookup openvpn table if traffic originates from our subnet
ip -6 route add priority 32000 from 2001:db8:cafe::/48 table openvpn
的内容script/server_down.sh
会以相反的顺序再次拆除一切。
答案2
OpenVPN 中的连接/断开脚本内容由配置文件中的“up/down”操作符控制(加上“script-security 2”,因此 OpenVPN 守护进程允许您执行第三方脚本)。以下是示例(p2p 连接):
remote 1.2.3.4
dev tun123
ifconfig 1.1.1.2 1.1.1.1
secret /etc/openvpn/test.key
proto udp
port 1234
comp-lzo
nobind
keepalive 10 60
script-security 2
up "/script/location/up.sh"
down "/script/location/down.sh"
此外,如果从 cli 使用 openvpn 可执行文件,则可以使用 --up 和 --down 选项。
答案3
这是关于 --client-connect 示例脚本的问题吗?如果是的话,任何遵循 bash 的东西
#!/bin/bash
message="$(echo -e "${common_name} connected to: ${HOSTNAME} \\nRemote:
${untrusted_ip} \\nVirtual: ${ifconfig_pool_remote_ip}")"
/usr/bin/create_notification -s news "${message}"
/usr/bin/notifier
exit 0