Linux里控制USB口的供电

原文链接http://scateu.me/2016/05/30/linux-toggle-usb-power.html 保存一下

方法一

偶然间获得一个USB LED灯. 正好家里有OpenWrt的路由器. 插上去可以亮. 于是想看看能不能通过shell来控制它的亮灭.

居然还真行.

以我的TP-Link WR720N为例:

echo 1 > /sys/class/gpio/gpio8/value #亮
echo 0 > /sys/class/gpio/gpio8/value #灭

以后说不定可以在make或其它什么比较耗时的命令结束之后, blink一下灯. 或者把消息用Morse Code闪出来. 感觉会很好玩的样子.

看来要买一批这样的小灯啦.
Linux里控制USB口的供电_第1张图片

HTTP 访问

把以下几行放到/www/cgi-bin/led:
#!/bin/sh

echo "Content-type: text/html"
echo ""

if [ $(cat /sys/class/gpio/gpio8/value) -eq 0 ]
then
echo 1 > /sys/class/gpio/gpio8/value
echo " ">  
">">

Light is

ON

"
else echo 0 > /sys/class/gpio/gpio8/value echo " ">
">">

Light is

OFF

"
fi

这样, 你就可以通过http://example.com/cgi-bin/led来控制灯了.

还可以很方便的用bash来控:

alias blink='for i in 1 2 3 4;do curl -s -o /dev/null http://192.168.11.1/cgi-bin/led;sleep 0.3;done'
alias led='curl -s -o /dev/null http://192.168.11.1/cgi-bin/led'

这样, 就可以优雅地:

wget http://example.com/somemovie.mp4 && blink
make -j12 && blink

最棒的是, 你可以装一个我厂的阿里钱盾, 使用钱盾快启功能, 加一个自定义URL到iOS的全局下拉框中. 非常方便.
(以前用京东的WIFI插座, 拖了一个充电头, 再拖了一个USB LED小灯, 作为夜灯. 要打开手机, 解锁, 打开APP, 等广告, 找到开关, 点. 特别费劲, 而且还费电. )

Linux里控制USB口的供电_第2张图片
与Siri HomeKit集成
链接

sudo npm install -g homebridge
sudo npm install -g homebridge-http

~/.homebridge/config.json里加入

{
     
    "bridge": {
     
        "name": "HomebridgePI",
        "username": "CD:22:3D:E3:CE:30",
        "port": 51826,
        "pin": "031-45-156"
    },
    
    "description": "The Onion!",

    "platforms": [],

    "accessories": [ 
    {
     
        "accessory": "Http",
        "name": "康神的灯的灯灯等等灯",
        "switchHandling": "realtime",
        "http_method": "GET",
        "on_url":      "http://192.168.1.117/cgi-bin/led-on",
        "off_url":     "http://192.168.1.117/cgi-bin/led-off",
        "service": "Light",
        "brightnessHandling": "no",
        "brightness_url":     "http://localhost/controller/1707/%b",
        "brightnesslvl_url":  "http://localhost/status/100054",
        "sendimmediately": "",
        "username" : "",
        "password" : ""					    
          } 
       ]
}

就可以大喊一声”Hey Siri, 开灯”

闪Caps Lock

Linux 下可以:

#turn on
xset led named "Scroll Lock"

#turn off
xset -led named "Scroll Lock"

Mac OS X:

  • https://github.com/busyloop/maclight
    貌似还有闪Morse Code:
  • http://www.psychicorigami.com/2009/03/01/5k-morse-code-app-using-capslock-led/
    以及还有Pidgin新消息到来, 闪Thinkpad的键盘灯(好感人…):
  • pidgin-blinklight
    Thinkpad的键盘灯可以直接被操作:
  • http://www.thinkwiki.org/wiki/ThinkLight
# cd /sys/devices/platform/thinkpad_acpi/leds
# ls
tpacpi::power  tpacpi::standby  tpacpi::thinklight  tpacpi::thinkvantage
# cd "tpacpi::power"
# while true;do echo 0 > brightness;sleep 0.1;echo 1 >brightness;sleep 0.1;done
  • power对应的是电源按钮的灯
  • standby对应的是机器后背的月亮待机灯
  • thinklight对应的是Thinkpad的键盘灯, 可惜在x230上支持有问题, 只能关不能开
  • thinkvantage未知, 应该是在其它Thinkpad机型上有
    以上的支持是由thinkpad_acpi这个Kernel module来支持.

参考

可以与IFTTT相联的开源USB LED灯: http://blink1.thingm.com/

方法二

使用uhubctl

github链接

支持的设备

Manufacturer Product Ports USB VID:PID Release EOL
AmazonBasics HU3641V1 (RPi issue) 4 3.0 2109:2811 2013
AmazonBasics HU3770V1 (RPi issue) 7 3.0 2109:2811 2013
AmazonBasics HU9003V1EBL 7 3.1 2109:2817 2018
AmazonBasics HU9002V1SBL, HU9002V1ESL, HUC9002V1SBL, HUC9002V1EBL 10 3.1 2109:2817 2018
Apple Thunderbolt Display 27" (internal USB hub) 6 2.0 2011 2016
Apple USB Keyboard With Numeric Pad (internal USB hub) 3 2.0 2011
Asus Z87-PLUS Motherboard (onboard USB hub) 4 3.0 2013 2016
B+B SmartWorx UHR204 4 2.0 0856:DB00 2013
B+B SmartWorx USH304 4 3.0 04B4:6506 2017
Basler 2000036234 4 3.0 0451:8046 2016
Belkin F5U101 4 2.0 0451:2046 2005 2010
Buffalo BSH4A05U3BK 4 3.0 05E3:0610 2015
Bytecc BT-UH340 4 3.0 2109:8110 2010
Circuitco Beagleboard-xM (internal USB hub) 4 2.0 0424:9514 2010
Club3D CSV-3242HD Dual Display Docking Station 4 3.0 2109:2811 2015
CyberPower CP-H420P 4 2.0 0409:0059 2004
Cypress CY4608 HX2VL development kit 4 2.0 04B4:6570 2012
D-Link DUB-H4 rev B (silver) 4 2.0 05E3:0605 2005 2010
D-Link DUB-H4 rev D,E (black). Note: rev A,C,F not supported 4 2.0 05E3:0608 2012
D-Link DUB-H7 rev A (silver) 7 2.0 2001:F103 2005 2010
D-Link DUB-H7 rev D,E (black). Note: rev B,C not supported 7 2.0 05E3:0608 2012
Dell P2416D 24" QHD Monitor 4 2.0 2017
Dell UltraSharp 1704FPT 17" LCD Monitor 4 2.0 0424:A700 2005 2015
Dell UltraSharp U2415 24" LCD Monitor 5 3.0 2014
Elecom U2H-G4S 4 2.0 2006 2011
GlobalScale ESPRESSObin SBUD102 V5 1 3.0 1D6B:0003 2017
Hawking Technology UH214 4 2.0 2003 2008
IOI U3H415E1 4 3.0 2012
j5create JUH470 (works only in USB2 mode) 3 3.0 05E3:0610 2014
Juiced Systems 6HUB-01 7 3.0 0BDA:0411 2014 2018
LG Electronics 38WK95C-W monitor 4 3.0 0451:8142 2018
Lenovo ThinkPad Ultra Docking Station (40A20090EU) 6 2.0 17EF:100F 2015
Lenovo ThinkPad Ultra Docking Station (40AJ0135EU) 7 3.1 17EF:3070 2018
Lenovo ThinkPad X200 Ultrabase 42X4963 3 2.0 17EF:1005 2008 2011
Lenovo ThinkPad X6 Ultrabase 42W3107 4 2.0 17EF:1000 2006 2009
Lindy USB serial converter 4 port 4 1.1 058F:9254 2008
Linksys USB2HUB4 4 2.0 2004 2010
Maplin A08CQ 7 2.0 0409:0059 2008 2011
Microchip EVB-USB2517 7 2.0 2008
Moxa Uport-407 7 2.0 110A:0407 2009
NVidia Jetson Nano B01 (details) 4 3.0 2019
Phidgets HUB0003_0 7 2.0 1A40:0201 2017
Plugable USB3-HUB7BC 7 3.0 2109:0813 2015
Plugable USB3-HUB7C 7 3.0 2109:0813 2015
Plugable USB3-HUB7-81X 7 3.0 2109:0813 2012
Raspberry Pi B+, 2B, 3B (see below) 4 2.0 2011
Raspberry Pi 3B+ (see below) 4 2.0 0424:2514 2018
Raspberry Pi 4B (see below) 4 3.0 2109:3431 2019
Renesas uPD720202 PCIe USB 3.0 host controller 2 3.0 2013
Rosewill RHUB-210 4 2.0 0409:005A 2011 2014
Sanwa Supply USB-HUB14GPH 4 1.1 2001 2003
Seagate Backup Plus Hub STEL8000100 2 3.0 0BC2:AB44 2016
Sunix SHB4200MA 4 2.0 0409:0058 2006 2009
Targus PAUH212U 7 2.0 2004 2009
Texas Instruments TUSB4041PAPEVM 4 2.1 0451:8142 2015
#先安装libusb
sudo apt-get install libusb-1.0-0-dev
#再安装uhubctl
sudo apt-get install unhubctl

用法

你可以像这样控制USB端口上的电源:

uhubctl -a off -p 235

这意味着在默认的智能集线器上操作,并关闭端口 2,3,5 ( -p 235 ) 上的( -a off,或者 -a 0 ) 电源。 支持的操作是 off/on/cycle ( 或者 0/1/2 )。 cycle 表示关闭电源,等待一些延迟( 使用 -d 配置),然后将它的打开。

在Linux上,你可能需要使用 sudo 运行它,或者配置 udev USB权限。

如果连接了多个智能USB集线器,则应选择特定的集线器以使用 -l ( 位置) 参数进行控制。

要查找集线器位置,只需运行不带任何参数的uhubctl即可。

你可能感兴趣的:(Linux里控制USB口的供电)