一、实现功能:获取docker_id
#docker数据源:
[root@docker scripts]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fddc6ae6484b fedora:25 "/bin/bash" 32 minutes ago Exited (0) 32 minutes ago competent_bardeen b8a8c0f47a8b fedora:25 "/bin/bash" 32 minutes ago Exited (0) 32 minutes ago practical_joliot 0cc87047545c fedora:25 "/bin/bash" 32 minutes ago Exited (0) 32 minutes ago mystifying_austin d7d77375e35e fedora:25 "/bin/bash" About an hour ago Exited (0) About an hour ago wizardly_yalow
#获取docker_id
[root@docker scripts]# cat dc_mgmt.py
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: nulige import os import re DC_list = [] p=os.popen('docker ps -a').read() dock_list=re.findall(r'sw{12}s',p) for i in dock_list: DC_list.append(i.strip()) print(DC_list)
#执行结果
[root@docker scripts]# python3 dc_mgmt.py ['fddc6ae6484b', 'b8a8c0f47a8b', '0cc87047545c', 'd7d77375e35e']
#获取bus号 (备注:用到pyudev模块,去获取bus号)
pyudev模块用法
参考:http://pyudev.readthedocs.io/en/latest/guide.html
#先安装pyudev模块
pip3 install pyudev
#获取脚本