最近由于测试CU分离,涉及到多个虚机环境的文件的拷贝、程序脚本的执行,所以写了如下python脚本来自动化执行;
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys, time, getopt
import pexpect
import commands
import shlex
import subprocess
import datetime
import socket
import io
DEBUG_FLAG = True
######################################################################
# return value
# 0 login and setup success
# -2 login failture
# -3 setup failture
######################################################################
LOGIN_FAILTURE = -2
RUN_CMD_TIMEOUT = -3
RUN_CMD_SUCCESS = 0
def ssh_remote_exec_cmd(address, user, password, cmd_list):
ret = RUN_CMD_SUCCESS
cmd_prompt = '\[?%s@.+\]?#' % user
local_cmd = 'ssh %s@%s' % (user, address)
if DEBUG_FLAG:
print '[%s] %s' % (local_cmd, cmd_list)
ssh = pexpect.spawn(local_cmd)
ssh.logfile_read = sys.stdout
expect = ssh.expect(['(?i)password:', '(?i)continue connecting (yes/no)?', cmd_prompt, pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if DEBUG_FLAG:
print 'ssh expect is %d' % expect
# Login
while True:
# ssh mutual trust mode, not need password
if 2 == expect:
break
# expect '(?i)continue connecting (yes/no)?'
elif 1 == expect:
ssh.sendline('yes')
expect = ssh.expect(['(?i)password:', pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if 0 == expect:
ssh.sendline(password)
expect = ssh.expect([cmd_prompt, '(?i)Permission denied', pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if DEBUG_FLAG:
print 'login expect is ' + str(expect)
if expect != 0:
ret = LOGIN_FAILTURE # login failture
print 'Can\'t ssh to %s ! ' % address
break
# Login success, run ssh command
if RUN_CMD_SUCCESS == ret:
for cmd in cmd_list:
# Ignore blank lines
while (ssh.expect([cmd_prompt, pexpect.EOF, pexpect.TIMEOUT], timeout = 1) == 0):
pass
ssh.sendline('%s' % cmd)
expect = ssh.expect([cmd_prompt, pexpect.EOF, pexpect.TIMEOUT], timeout = 180)
if 2 == expect:
ret = RUN_CMD_TIMEOUT # failture
print 'Exec timeout: %s ! ' % address
break
ssh.close()
return ret
def scp_file_to_remote(address, user, password, local_file, remote_file):
ret = RUN_CMD_SUCCESS
cmd_prompt = '\[?%s@.+\]?#' % user
local_cmd = 'scp -r %s %s@%s:%s' % (local_file, user, address, remote_file)
if DEBUG_FLAG:
print '[%s]' % (local_cmd)
ssh = pexpect.spawn(local_cmd)
ssh.logfile_read = sys.stdout
expect = ssh.expect(['(?i)password:', '(?i)continue connecting (yes/no)?', cmd_prompt, pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if DEBUG_FLAG:
print 'ssh expect is %d' % expect
# Login
while True:
# ssh mutual trust mode, not need password
if 2 == expect:
break
# expect '(?i)continue connecting (yes/no)?'
elif 1 == expect:
ssh.sendline('yes')
expect = ssh.expect(['(?i)password:', pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if 0 == expect:
ssh.sendline(password)
expect = ssh.expect([cmd_prompt, '(?i)Permission denied', pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if DEBUG_FLAG:
print 'login expect is ' + str(expect)
if expect == 0:
ret = LOGIN_FAILTURE # login failture
print 'Can\'t ssh to %s ! ' % address
break
ssh.close()
return ret
def scp_remote_file_to_local(address, user, password, local_path, remote_file):
ret = RUN_CMD_SUCCESS
cmd_prompt = '\[?%s@.+\]?#' % user
local_cmd = 'scp -r %s@%s:%s %s' % (user, address, remote_file, local_path)
if DEBUG_FLAG:
print '[%s] %s' % (local_cmd, cmd_prompt)
ssh = pexpect.spawn(local_cmd)
ssh.logfile_read = sys.stdout
expect = ssh.expect(['(?i)password:', '(?i)continue connecting (yes/no)?', cmd_prompt, pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if DEBUG_FLAG:
print 'ssh expect is %d' % expect
# Login
while True:
# ssh mutual trust mode, not need password
if 2 == expect:
break
# expect '(?i)continue connecting (yes/no)?'
elif 1 == expect:
ssh.sendline('yes')
expect = ssh.expect(['(?i)password:', pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if 0 == expect:
ssh.sendline(password)
expect = ssh.expect([cmd_prompt, '(?i)Permission denied', pexpect.EOF, pexpect.TIMEOUT], timeout = 20)
if DEBUG_FLAG:
print 'login expect is ' + str(expect)
if expect == 0:
ret = LOGIN_FAILTURE # login failture
print 'Can\'t ssh to %s ! ' % address
break
# Login success, run ssh command
ssh.close()
return ret
def fetch(startStr,endStr,filePath):
a = ""
with open(filePath,'r') as f :
for line in f:
line = line.strip()
line = line.lstrip(startStr)
line = line.rstrip(endStr)
a = line
return a
def remote_install_symbol():
servers = ['12.1.1.104', '12.1.1.105', '12.1.1.106', '12.1.1.107', '12.1.1.108', '12.1.1.109', '12.1.1.110', '12.1.1.111', '12.1.1.112', '12.1.1.113', '12.1.1.114', '12.1.1.115', '12.1.1.116', '12.1.1.117', '12.1.1.118','12.1.1.119', '12.1.1.120']
scp_remote_file_to_local('12.1.1.120', 'root', '123456', '/usr/local/certus/version/', '/usr/local/certus/version/.version')
version = fetch("version: ", "-release", "/usr/local/certus/version/.version")
print version
symbol_path = '/root/%s-debug-symbol.tar.gz' % version
print symbol_path
tar_cmd = 'tar zxvf /usr/local/certus/version/%s-debug-symbol.tar.gz -C /usr/local/certus/version' % version
print tar_cmd
install_cmd = '/usr/local/certus/version/%s-debug-symbol/install-symbol' % version
print install_cmd
for m in servers:
print '---------------------[%s]---------------------' % m
scp_file_to_remote(m, 'root', '123456', symbol_path, '/usr/local/certus/version')
ssh_remote_exec_cmd(m, 'root', '123456', [tar_cmd, install_cmd])
print '\n\n'
def remote_install_process(process):
servers = ['12.1.1.104', '12.1.1.105', '12.1.1.106', '12.1.1.107', '12.1.1.108', '12.1.1.109', '12.1.1.110', '12.1.1.111', '12.1.1.112', '12.1.1.113', '12.1.1.114', '12.1.1.115', '12.1.1.116', '12.1.1.117', '12.1.1.118','12.1.1.119']
stop_cmd = 'flexbng -S'
start_cmd = 'flexbng -s'
for m in servers:
print '---------------------[%s]---------------------' % m
ssh_remote_exec_cmd(m, 'root', '123456', [stop_cmd])
scp_file_to_remote(m, 'root', '123456', process, '/usr/local/certus/vnf/bin/control-plane/')
ssh_remote_exec_cmd(m, 'root', '123456', [start_cmd])
print '\n\n'
def remote_install_lib(lib):
servers = ['12.1.1.104', '12.1.1.105', '12.1.1.106', '12.1.1.107', '12.1.1.108', '12.1.1.109', '12.1.1.110', '12.1.1.111', '12.1.1.112', '12.1.1.113', '12.1.1.114', '12.1.1.115', '12.1.1.116', '12.1.1.117', '12.1.1.118','12.1.1.119']
stop_cmd = 'flexbng -S'
start_cmd = 'flexbng -s'
for m in servers:
print '---------------------[%s]---------------------' % m
ssh_remote_exec_cmd(m, 'root', '123456', [stop_cmd])
scp_file_to_remote(m, 'root', '123456', lib, '/usr/local/certus/vnf/lib/')
ssh_remote_exec_cmd(m, 'root', '123456', [start_cmd])
print '\n\n'
def remote_onekey_switch():
servers = ['12.1.1.104', '12.1.1.105', '12.1.1.106', '12.1.1.107', '12.1.1.108', '12.1.1.109', '12.1.1.110', '12.1.1.111', '12.1.1.112', '12.1.1.113', '12.1.1.114', '12.1.1.115', '12.1.1.116', '12.1.1.117', '12.1.1.118','12.1.1.119', '12.1.1.120']
onekey_switch_cmd = '/root/onekey-switch'
for m in servers:
print '---------------------[%s]---------------------' % m
ssh_remote_exec_cmd(m, 'root', '123456', [onekey_switch_cmd])
print '\n\n'
def remote_install_release_process(process):
servers = ['12.1.1.104', '12.1.1.105', '12.1.1.106', '12.1.1.107', '12.1.1.108', '12.1.1.109', '12.1.1.110', '12.1.1.111', '12.1.1.112', '12.1.1.113', '12.1.1.114', '12.1.1.115', '12.1.1.116', '12.1.1.117', '12.1.1.118','12.1.1.119']
stop_cmd = 'flexbng -S'
start_cmd = 'flexbng -s'
debug_path = '/usr/local/certus/debug-symbol/'
process_symbol = '%s.dbg' % process
print process
print process_symbol
for m in servers:
print '---------------------[%s]---------------------' % m
ssh_remote_exec_cmd(m, 'root', '123456', [stop_cmd])
scp_file_to_remote(m, 'root', '123456', process, '/usr/local/certus/vnf/bin/control-plane/')
scp_file_to_remote(m, 'root', '123456', process_symbol, '/usr/local/certus/debug-symbol')
ssh_remote_exec_cmd(m, 'root', '123456', [start_cmd])
print '\n\n'
def remote_install_log():
print ''
def usage():
print '-s install debug symbol, no need follow symbol path'
print '-p install single process, followed by process path'
print '-r install release single process and symbol, followed by process path'
print '-l install single lib, followed by lib path'
print '-o execute onekey switch'
if len(sys.argv) == 1:
usage()
sys.exit()
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "h:sr:p:l:o", ["help", "symbol", "process", "release","lib", "trace", "onekey_switch"])
except getopt.GetoptError:
print("argv error,please input")
print 'opts=',opts
for cmd, value in opts:
if cmd in ("-s", "--symbol"):
print value
remote_install_symbol()
elif cmd in ["-r", "--release"]:
print value
remote_install_release_process(value)
elif cmd in ("-p", "--process"):
print value
remote_install_process(value)
elif cmd in ("-l", "--lib"):
remote_install_lib(value)
elif cmd in ("-t", "--trace"):
remote_install_log()
elif cmd in ["-o", "--onekey_switch"]:
remote_onekey_switch()
elif cmd in ("-h", "--help"):
usage()
sys.exit()