Skip to content

cdsl-research/SwitchbotControl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwitchbotControl

初めに

こんにちは最近研究室内で運用している物理サーバを学生が使っていない深夜は止めるためにソフトウェアを作りました. 物理サーバ上のケーブルとプラグの間にはSwitchbotが繋がっておりこれを使って物理サーバを上げたり落としたりします!

環境物理

電源をOFFする対象の物理サーバ

  • 物理サーバ→ESXiをインストールしたサーバ IMG_3894 電源をOFFする際に使用するSwitchBot
  • SwitchBot プラグミニ→ESXiを配置した物理サーバのケーブルとプラグの間に配置 IMG_3892

電源を落とす処理をするサーバ

  • Ubuntu 24.04.1 LTS
  • Python 3.10.12

Python上で動くライブラリ

  • requests
  • yaml
  • paramiko
  • time

構成要素

SwitchbotControl
├── deviceslist.yaml #Switchbotの電源をON/OFFする対象の指定箇所
├── esxilist.yaml #物理サーバ上にいるVMの電源をON/OFFする対象の指定箇所
├── README.md #手順書
├── start.py #deviceslist.yamlで指定した対象に繋がっているSwitchbotの電源をONするソフトウェア
├── stop.py #deviceslist.yamlで指定した対象に繋がっているSwitchbotの電源をOFFするソフトウェア
└── stop_vms.py #esxilist.yamlで指定した対象のESXiの仮想マシンの電源を落とすためのソフトウェア

使い方

  1. 対象ディレクトリへ移動
hirao@autofil:~$ cd SwitchbotControl/
hirao@autofil:~/SwitchbotControl$
  1. 仮想環境の準備

仮想環境を作成します.

hirao@autofil:~/SwitchbotControl$ ls
deviceslist.yaml  esxilist.yaml  README.md  start.py  stop.py  stop_vms.py  venv
hirao@autofil:~/SwitchbotControl$ 

仮想環境の有効化

hirao@autofil:~/SwitchbotControl$ source venv/bin/activate
(venv) hirao@autofil:~/SwitchbotControl$

Pythonで必要なパッケージのインストール

(venv) hirao@autofil:~/SwitchbotControl$ pip install requests pyyaml paramiko
Collecting requests
  Using cached requests-2.32.5-py3-none-any.whl (64 kB)
Collecting pyyaml
  Using cached pyyaml-6.0.3-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (770 kB)
Collecting paramiko
  Using cached paramiko-4.0.0-py3-none-any.whl (223 kB)
Collecting urllib3<3,>=1.21.1
  Using cached urllib3-2.5.0-py3-none-any.whl (129 kB)
Collecting charset_normalizer<4,>=2
  Using cached charset_normalizer-3.4.4-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (153 kB)
Collecting idna<4,>=2.5
  Using cached idna-3.11-py3-none-any.whl (71 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2025.10.5-py3-none-any.whl (163 kB)
Collecting pynacl>=1.5
  Downloading pynacl-1.6.0-cp38-abi3-manylinux_2_34_x86_64.whl (1.4 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.4/1.4 MB 19.7 MB/s eta 0:00:00
Collecting cryptography>=3.3
  Downloading cryptography-46.0.3-cp38-abi3-manylinux_2_34_x86_64.whl (4.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.5/4.5 MB 37.7 MB/s eta 0:00:00
Collecting bcrypt>=3.2
  Downloading bcrypt-5.0.0-cp39-abi3-manylinux_2_34_x86_64.whl (278 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 278.2/278.2 KB 63.3 MB/s eta 0:00:00
Collecting invoke>=2.0
  Downloading invoke-2.2.1-py3-none-any.whl (160 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 160.3/160.3 KB 34.7 MB/s eta 0:00:00
Collecting typing-extensions>=4.13.2
  Downloading typing_extensions-4.15.0-py3-none-any.whl (44 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.6/44.6 KB 14.7 MB/s eta 0:00:00
Collecting cffi>=2.0.0
  Downloading cffi-2.0.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (216 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 216.5/216.5 KB 43.1 MB/s eta 0:00:00
Collecting pycparser
  Downloading pycparser-2.23-py3-none-any.whl (118 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.1/118.1 KB 37.0 MB/s eta 0:00:00
Installing collected packages: urllib3, typing-extensions, pyyaml, pycparser, invoke, idna, charset_normalizer, certifi, bcrypt, requests, cffi, pynacl, cryptography, paramiko
Successfully installed bcrypt-5.0.0 certifi-2025.10.5 cffi-2.0.0 charset_normalizer-3.4.4 cryptography-46.0.3 idna-3.11 invoke-2.2.1 paramiko-4.0.0 pycparser-2.23 pynacl-1.6.0 pyyaml-6.0.3 requests-2.32.5 typing-extensions-4.15.0 urllib3-2.5.0
(venv) hirao@autofil:~/SwitchbotControl$ 
  1. 物理サーバ上のVMの電源をOFFにする.

esxilist.yamlの編集します.

esxi_hosts:
  - host: "*物理サーバのホスト名 OR IPアドレス*"
    user: "*対象のユーザ名*"
    password: "*パスワード*"

hostの部分には,物理サーバのホスト名 OR IPアドレスを入れます.

userの部分には,ESXiにログインする際のユーザ名を入れます.

passwordにはESXiにログインする際のパスワードを入れます.

こんな感じでESXiにログインした際にVMの電源がONになっています. スクリーンショット 2025-11-05 22 33 04

次にstop_vms.pyを実行します.以下のように動けは大丈夫です.

(venv) hirao@autofil:~/SwitchbotControl$ python3 stop_vms.py 

=== violet: 通常シャットダウン → 5分待ち → 強制停止 → メンテナンスモード ===
VMIDs: 333 341 349 350
shutdown: VM 333
shutdown: VM 341
Waiting... still on: 333 341
All VMs powered off (graceful).
enter maintenance mode
Disabled
(venv) hirao@autofil:~/SwitchbotControl$ 

実際に確認すると以下のようにVMの電源が落ちています.

スクリーンショット 2025-11-05 22 37 53
  1. 物理サーバの電源をOFFにする.

物理サーバの電源を落とすためにdeviceslist.yamlを編集します.

devices:
  Taget_SB: "*DeviceのID*"

token: "*Switchbotに接続する際のtoken*"

Taget_SBには,SwitchBotのDeviceのIDを入れます.

tokenにはSwitchbotに接続する際のtokenを入れます.

次にstop.pyを実行します.以下のように動けは大丈夫です.

(venv) hirao@autofil:~/SwitchbotControl$ python3 stop.py 
電源OFF 成功: {'statusCode': 100, 'body': {}, 'message': 'success'}
(venv) hirao@autofil:~/SwitchbotControl$ 

こんな感じで電源もOFFされていますね! IMG_3896

  1. 物理サーバの電源をONにする. 今度は逆に電源をONにします. 電源のONはすごく簡単で,以下のようにstart.py を実行します.以下のようになればOKです.
(venv) hirao@autofil:~/SwitchbotControl$ python3 start.py 
電源ON 成功: {'statusCode': 100, 'body': {}, 'message': 'success'}
(venv) hirao@autofil:~/SwitchbotControl$ 

電源上がってますね! IMG_3897

最後に

私たちの環境では,このシステムをタイマーを使って定期的に落としたりしています.皆さんもぜひ電力の削減に使ってみてください.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages