在Windows下如何能控制Linux,大概的想法肯定是SSH,的确,Python也提供了对应的库,以下先提供一个例子,通过Python获得XenServer的uuid
import paramiko
hostname = '10.10.10.100'
port = 22
username = 'root'
password = 'xxxxxx'
command_for_uuid = "xe sr-list type=ext|grep uuid|awk -F'[:]' '{print $2}'|cut -d\" \" -f2"
paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.load_system_host_keys()
s.connect(hostname, port, username, password)
stdin, stdout, stderr = s.exec_command(command_for_uuid)
uuid = stdout.read().strip()
这个例子在Win7下面运行通过,可以顺利获得XenServer的SR uuid,
两点注意
1. 装paramiko库的时候,系统必须有C编译器,要么你装VS,要么你装MinGW,我系统中安装了VS2010
2. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())这句代码在Windows平台下必须加