-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathser2bt_bridge
executable file
·123 lines (111 loc) · 4.06 KB
/
ser2bt_bridge
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
#color and other formatting defenitions:
source /usr/local/lib/format.sh
#constants:
bt_port="/dev/rfcomm0"
usb_port="/dev/ttyUSB"
acm_port="/dev/ttyACM"
#variables set with default values
switch_name=("Switch_1" "Switch_2" "Switch_3")
installed_app_count=0
app_count=0
baud="9600"
terminal="xterm"
screen_config="/etc/ser2bt_bridge.screenrc"
max_ports=3
declare -a cmds=(stty screen minicom tio)
#Functions:
function exit_script () {
if [[ ${exit_status} == 0 ]]; then
printf "\n${nor}Exit Status is: ${drk_grn}${exit_status}${end}\n"
else
printf "\n${nor}Exit Status is: ${red}${exit_status}${end}\n"
fi
printf "${nor}Exit Resason is: ${exit_status_reason}\n"
printf "\n${nor}Exiting to shell...\n${end}"
}
#Script starts here:
#Check to determine if the user is logging in through a bluetooth serial port (rfcomm[0-9]), if not, then state the reason, and exit.
if [[ $(tty) != ${bt_port} ]]; then
printf "\n\n${yel}${0}${nor} is only designed to work from a ${blu}bluetooth${nor} vty. Please\nlogin to ${mag}${HOSTNAME}${nor}"
printf "through a ${blu}bluetooth${nor} vty, to activate the serial passthrough\nfunction.\n"
exit_status=2
exit_status_reason="${bro}Can only work if being accessed from ${blu}bluetooth${bro} serial."
exit_script $exit_status ${exit_status_reason}
exit ${exit_status} # Exit - can only work if being accessed from bluetooth serial.
fi
resize > /dev/null 2>&1
#Check dependencies:
for cmd in ${cmds[@]} ; do
let app_count++
if command -v ${cmd} > /dev/null 2>&1 ; then
let installed_app_count++
else
printf "${red}${cmd}${nor} not found, please use: \"${yel}sudo apt install ${cmd}${nor}\"\n"
fi
done
if [[ ${app_count} != ${installed_app_count} ]]; then
exit_status=3
exit_status_reason="${bro}Dependancies need to be met.${nor}"
exit_script ${exit_status} ${exit_status_reason}
exit $exit_status # Exit as dependancies need to be met.
fi
#set the bluetooth port, and assign the usb and/or acm ports.
printf "\n${nor}Checking to make sure we are connected to at least one serial device:\n"
#Check for USB ports:
z=${max_ports}
printf " ${nor}Checking to see what ${cyn}${usb_port}${nor}'s are available:"
for (( a=0; a<=${max_ports}-1; a++ )); do
port="${usb_port}${a}"
if [[ ! -e ${port} ]]; then
let z--
else
printf " \n${cyn}${port}${nor} is ${drk_grn}available${nor}...\n"
wire_port+=(${port})
fi
done
printf " ${drk_grn}Done\n"
#Check for ACM ports:
z=${max_ports}
printf " ${nor}Checking to see what ${cyn}${acm_port}${nor}'s are available:"
for (( a=0; a<=${max_ports}-1; a++ )); do
port="${acm_port}${a}"
if [[ ! -e ${port} ]]; then
let z--
else
printf " \n${cyn}${port}${nor} is ${drk_grn}available${nor}...\n"
wire_port+=(${port})
fi
done
printf " ${drk_grn}Done\n"
#Create screen sessions based on the USB and ACM Ports found from above:
count=0
wire_count=1
for serial_port in ${wire_port[@]}; do
printf "Both sides are connected, assigning screen ${cyn}${wire_count}${nor} with ${cyn}${serial_port}${nor}..."
eval $(screen -c ${screen_config} -S ${switch_name[count]} -d -m -t "${serial_port}${space}${switch_name[count]}" ${serial_port} -L ${baud} -T ${terminal}) && printf "${drk_grn}Done${nor}.\n\n" || printf "${drk_red}Failed${nor}!\n\n"
let count++
let wire_count++
done
session_count=$(screen -r | grep Detached | cut -d "." -f 2 | cut -f 1 | wc -w)
#resize the terminal for better vewing
resize > /dev/null 2>&1
#Determine what to do on exit - if we are connected to more than one serial device, connected to 1 serial device, or none at all:
if [[ ${session_count} > 1 ]]; then
screen -r | grep Detached | cut -d "." -f 2 | cut -f 1
printf "\nUse ${yel}screen -r${mag} switch_name${nor} (listed above).\n"
elif [[ ${session_count} = 1 ]]; then
screen -r
else
exit_status=4
exit_status_reason="${bro}There is no device connected via serial.${nor}"
exit_script ${exit_status} ${exit_status_reason}
exit ${exit_status} # Exit as there is no device connected via serial.
fi
wait
#clear
#exit routine.
exit_status=0
exit_status_reason="Successful completion."
exit_script ${exit_status} ${exit_status_reason}
exit ${exit_status}