![如何检测 USB 调制解调器的工作 ttyUSB](https://linux22.com/image/40680/%E5%A6%82%E4%BD%95%E6%A3%80%E6%B5%8B%20USB%20%E8%B0%83%E5%88%B6%E8%A7%A3%E8%B0%83%E5%99%A8%E7%9A%84%E5%B7%A5%E4%BD%9C%20ttyUSB.png)
我使用 OpenWRT 并需要设置 USB 3G 调制解调器。当我连接 USB 调制解调器时,我在日志中看到系统创建了四个 ttyUSB。为什么是四个?
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.220000] usbserial_generic 1-1:1.0: The "generic" usb-serial driver is only for testing and one-off prototypes.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.230000] usbserial_generic 1-1:1.0: Tell [email protected] to add your device to a proper driver.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.240000] usbserial_generic 1-1:1.0: generic converter detected
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.280000] usb 1-1: generic converter now attached to ttyUSB0
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.280000] usbserial_generic 1-1:1.1: The "generic" usb-serial driver is only for testing and one-off prototypes.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.290000] usbserial_generic 1-1:1.1: Tell [email protected] to add your device to a proper driver.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.300000] usbserial_generic 1-1:1.1: generic converter detected
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.340000] usb 1-1: generic converter now attached to ttyUSB1
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.340000] usbserial_generic 1-1:1.2: The "generic" usb-serial driver is only for testing and one-off prototypes.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.350000] usbserial_generic 1-1:1.2: Tell [email protected] to add your device to a proper driver.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.360000] usbserial_generic 1-1:1.2: generic converter detected
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.440000] usb 1-1: generic converter now attached to ttyUSB2
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.450000] usbserial_generic 1-1:1.3: The "generic" usb-serial driver is only for testing and one-off prototypes.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.450000] usbserial_generic 1-1:1.3: Tell [email protected] to add your device to a proper driver.
Tue Jul 29 07:45:23 2014 kern.info kernel: [ 580.460000] usbserial_generic 1-1:1.3: generic converter detected
Tue Jul 29 07:45:24 2014 kern.info kernel: [ 580.500000] usb 1-1: generic converter now attached to ttyUSB3
之后我知道调制解调器正在工作。因此,我设置网络接口,必须选择一个可用的 ttyUSB。但我怎么知道哪个ttyUSB 正在工作呢?对于我运行的每个端口
gcom -d /dev/ttyUSB0 info
gcom -d /dev/ttyUSB1 info
etc...
用于查找工作端口。但这种方法不好,因为速度慢且难以解析。我应该编写一个 bash 脚本来检测调制解调器的工作 ttyUSB 并放入网络配置。我该怎么做?
我的路由器只有一个 USB 端口。每个 USB 3G 调制解调器使用不同的 ttyUSB 端口。
附言。我如何想象我可以向每个端口发送一些数据并检查结果。如果结果良好,则使用此ttyUSB。
答案1
我没有找到更好的方法来解析 gcom 命令结果。
## Search for a working USB port (/dev/ttyUSB)
work=false
for port in /dev/ttyUSB*
do
[[ -e $port ]] || continue
gcom -d $port info &> /tmp/testusb
testUSB=`cat /tmp/testusb | grep "Error\|Can't"`
if [ -z "$testUSB" ]; then
work=$port
break
fi
done
rm -rf /tmp/testusb
if [ $work != false ]; then
echo "$work is working!"
else
echo 'Not found a working USB port'
fi