[Python] using the Pexpect module for batch SCP

Source: Internet
Author: User

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.