#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
python--做远程scp的进度条
其中threading为多线程函数,multiprocessing为多进程函数(本处使用进程)
scp通过pexpect与本机进行交互输入密码(如果配置无密登陆此处可以省略)
通过paramiko获取远程服务器的du -s的值
http://zhengbin.blog.51cto.com/2989505/1189439
"""
import sys
import os
import threading,multiprocessing
import pexpect
import time,paramiko
def scopy():
ld=pexpect.spawn('scp -r /home/evan/dovo/iso/ubuntu-12.04.2-desktop-amd64.iso root@192.168.23.41:/root/')
ld.expect('password:',timeout=None)
ld.sendline('2240')
ld.expect(pexpect.EOF,timeout=None)
#ld.read()
def view_bar(num=1, sum=100, bar_word=":"):
rate = float(num) / float(sum)
rate_num = int(rate * 100)
print 'r%d%% :' %(rate_num),
for i in range(int(num)/10):
os.write(1, bar_word)
sys.stdout.flush()
def huoqu():
host='192.168.23.41'
user='root'
passwd='2240'
port=22
try:
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(host,port,user,passwd)
#stdin,stdout,stderr=ssh.exec_command('ls /home')
stdin,stdout,stderr=ssh.exec_command('du -s /root/')
abc=stdout.readlines()[0].split()[0]
return abc
except Exception,e:
return 0
if __name__=='__main__':
global count,mutex
count=1
t=multiprocessing.Process(target=scopy,args=())
t.start()
while (1):
time.sleep(1)
x=huoqu()
bili=float(x)/float(os.popen('du -s /home/evan/dovo/iso/ubuntu-12.04.2-desktop-amd64.iso ').readlines()[0].split()[0])*100
view_bar(bili,100,bar_word="#")
if int(bili) == 100:
break
print "COPY IS FINISH!!!!"
t.join()