-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVNCinUse.sh
More file actions
executable file
·80 lines (68 loc) · 2.83 KB
/
Copy pathVNCinUse.sh
File metadata and controls
executable file
·80 lines (68 loc) · 2.83 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
#########################################################################################################################
#
# VNC Checker written by Austin Leath and KClose.
# Checks for VNC connection and triggers cleanup scripts if VNC was only recently disconnected.
# This script should be run on a timed cycle with a launch daemon.
#
# Updated 01/24/2022: Check *only* connecttions from subnet 129.120.207.*
# This is to ignore any and any management connections that may be made (ARD, ScreenSharing, etc.)
# Also added IP reporting to the switchfile to check for unexpected connections.
#
# ToDo: move subnet to a variable for future, wider use applications.
#
#########################################################################################################################
### NECESSARY FUNCTIONS ###
# Function to see if VNC is currently in use. This simply reports a true/false response.
# If using this script on another subnet, be sure to change the the last grep option accordingly.
function checkvncuse() {
inuse=$(netstat -vanp tcp | grep 5900 | grep ESTABLISHED | awk '{print $5}' | grep 129.120.207);
if [ -n "$inuse" ];
then
#VNC in use. return 1
return 1
else
#VNC not in use. return 0
return 0
fi
}
# Function to write out a switch file to record current state.
switchfile() {
# Collect variables for switch.
vncstate=$1
timestamp=$(date)
# Write the result to the switch file.
echo "writing output to switch file."
echo $timestamp " - " $vncstate >> /tmp/VNCSwitch.txt
}
### MAIN SCRIPT ###
# Create Switch File if it doesn't already exist. Otherwise the tail check will fail.
if [ ! -f /tmp/VNCSwitch.txt ]; then
echo "switch file not found. creating switch file."
touch /tmp/VNCSwitch.txt
switchfile "New Switchfile."
fi
# Run function "checkvncuse" and use the boolean return to determine actions.
# We are *only* looking for changes in state.
# If VNC is "not in use", then we only care if it was previously "in use";
# in which case we register that as a change and perform the approrpriate cleanup.
#
# If VNC is "in use", then we only care if it was previously "not in use";
# in which case we register that as a change, but do nothing else.
if checkvncuse "$notinuse";
then
if (tail -n 1 /tmp/VNCSwitch.txt | grep 'VNC in use.');
then
echo "vnc recently in use. running disconnect scripts and writing to switch file."
/usr/local/jamf/bin/jamf policy -event cvad.guacdisconnect
switchfile "VNC not in use."
fi
else
if (tail -n 1 /tmp/VNCSwitch.txt | grep 'VNC not in use.') || (tail -n 1 /tmp/VNCSwitch.txt | grep 'New Switchfile.');
then
connectedip=$(netstat -vanp tcp | grep 5900 | grep ESTABLISHED | awk '{print $5}')
echo "vnc in use from writing to switch file."
switchfile "VNC in use. Connected IP is $connectedip."
fi
fi
exit 0