#_*_ coding:utf8 _*_
from pysphere import VIServer
import ssl
import re
import sys
import os
import yaml
config_name = 'config.yaml'
config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), config_name)
config = yaml.load(file(config_file))
print config['vmware']['host'] , config['vmware']['user'], config['vmware']['pwd']
ssl._create_default_https_context = ssl._create_unverified_context
s = VIServer()
s.connect(config['vmware']['host'], config['vmware']['user'], config['vmware']['pwd'])
hosts = s.get_registered_vms()
try :
if sys.argv[1] is not None:
arg = sys.argv[1]
except:
print("""
\t[Notes]: script have option,the option is vm‘s name.
""")
#print hosts
def filter(obj,v):
hosts = s.get_registered_vms()
if v is not None:
for host in hosts:
res = re.findall(r'\[\S*\] ' + v + '\/' + v +'\.vmx',host)
# res = re.findall(r''+'\[\w*\] '+ v + '\/\w*.vmx',host)
if res:
return res
v = filter(s, arg)
vm = s.get_vm_by_path(v[0])
print vm
vm.login_in_guest('root','cditv#028') #登录虚拟机
vm.send_file('/root/pysphere-master/VMmission/init.sh','/tmp/init.sh') #传送本地脚本到虚拟机
vm.start_process('/bin/bash',args=["init.sh"], env='',cwd='/tmp') #执行脚本