#!/usr/bin/env python
#-*-Coding:utf-8-*-
#wangxiaofei
#awcloud自动化测试
Import Time,os
Import threading
Import Sys
Threads = []
Username = ' Root '
passwd = ' www.awcloud.com '
SSH_IP = [' 10.11.11.4 ',
' 10.11.11.5 ',
' 10.11.11.6 ',
' 10.11.11.7 '
]
Try:
import pexpect
except Importerror:
print ' Can not found Pexpect Module,now install it ... '
os.system (' yum install pexpect-y ')
Time.sleep (3)
check_pexpect=os.popen (' rpm-qa | grep pexpect ')
if ' Pexpect ' in Check_pexpect.read ():
print ' Check pexpect module was exists,going on ... ' br> else:
print ' Check pexpect module is not exists,exit ... '
&N Bsp sys.exit ()
Else:
Import pexpect
Def scp_file ():
I=0
For I in range (len (SSH_IP)):
Try
Scp_command=pexpect.spawn (' SCP ' +scp_filename+ ' [email protected] ' +ssh_ip[i]+ ':/root/')
Expect_result = Scp_command.expect ([R ' Assword: ', R ' yes/no '],timeout=30)
If Expect_result = = 0:
Scp_command.sendline (passwd)
#这句话真的很神奇, if you do not add this sentence, the program will be executed, but do not execute copy, please explain the road God
Scp_command.read ()
elif Expect_result = = 1:
Scp_command.sendline (' yes ')
Scp_command.expect (' Assword: ', timeout=30)
Scp_command.sendline (passwd)
#don ' t Delete this code,if your does then,the program would be a faill.
Scp_command.read ()
#important
Else
print ' Unknow Result ... '
Except Pexpect. Eof:
print ' uploading Fail .... '
Print Pexpect. Eof
Except Pexpect. Timeour:
print ' Uploading Time out ... '
Time.sleep (2)
If ' __name__ ' = = ' __main__ ':
Scp_file ()
[Python] using the Pexpect module for batch SCP