-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathswitch_script.sh
executable file
·72 lines (56 loc) · 2.33 KB
/
switch_script.sh
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
#!/bin/bash
# find Cisco Port Discovery (CDP) from a Mac
# version .4 | 12/2017 | Marnin RU ITS
# check for root privileges
if [ $USER != root ]; then
echo "Please run script with root privileges, exiting"
exit 1
fi
ComputerName=`/bin/hostname`
echo ""
echo "If the Ethernet interface is active, this script can take up to 30 seconds to run."
echo "Still beats using a Fluke in the field. Please be patient."
echo ""
# grab the active port. source: https://www.jamf.com/jamf-nation/discussions/18174/ea-to-determine-the-current-network-negotiation-speed
allPorts=$(/usr/sbin/networksetup -listallhardwareports | awk -F' ' '/Device:/{print $NF}')
while read Port; do
if [[ $(ifconfig "$Port" 2>/dev/null | awk '/status:/{print $NF}') == "active" ]]; then
ActivePort="$Port"
ActivePortName=$(/usr/sbin/networksetup -listallhardwareports | grep -B1 "$port" | awk -F': ' '/Hardware Port/{print $NF}')
break
fi
done < <(printf '%s\n' "$allPorts")
if [[ "$ActivePortName" =~ "Ethernet" ]]; then
LinkSpeed=$(/sbin/ifconfig $ActivePort | awk -F': ' '/media:/{print $NF}' | grep -o "[0-9]\{1,5\}baseT")
HardwareAddress=`/sbin/ifconfig $ActivePort | awk '/ether/{print $2}' | head -1`
MacIP=`/usr/sbin/ipconfig getifaddr $ActivePort 2>&1`
elif [[ "$ActivePortName" =~ "Wi-Fi" ]]; then
echo "Wi-Fi is the active port, exiting"
exit 1
else
echo "Not sure what the active network port is, exiting"
exit 1
fi
# query the switch
/usr/sbin/tcpdump -nn -v -i $ActivePort -s 1500 -c 1 'ether[20:2] == 0x2000' > /tmp/network.txt
Switch_Port=`cat /tmp/network.txt | grep Port-ID | awk '{print $7}' | tr -d "'"`
Vlan=`cat /tmp/network.txt | grep "Native VLAN ID" | awk '{print $9}'`
Physical_location_switch=`cat /tmp/network.txt | grep "Physical Location" | awk '{print $8}'`
Switch_name=`cat /tmp/network.txt | grep "Device-ID" | awk '{print $7}' | tr -d "'"`
Switch_ip=`cat /tmp/network.txt | grep "Management Addresses" | awk '{print $10}'`
echo ""
echo "Mac Results:"
echo "Computer Name: $ComputerName"
echo "Mac IP ($ActivePort) = $MacIP"
echo "Mac, MAC Address = $HardwareAddress"
echo "Link Speed: $LinkSpeed"
echo ""
echo "Switch Results:"
echo "Switch Port = $Switch_Port"
echo "Vlan = $Vlan"
echo "Switch Location = $Physical_location_switch"
echo "Switch Name = $Switch_name"
echo "Switch IP = $Switch_ip"
echo ""
echo ""
exit 0