python--做远程scp的进度条

2023-01-31 05:01:53 python

python写的远程做scp拷贝,并判断拷贝状态,自制进度条在屏幕输出

说明:

其中threading为多线程函数,multiprocessing为多进程函数(本处使用进程)

scp通过pexpect与本机进行交互输入密码(如果配置无密登陆此处可以省略)

通过paramiko获取远程服务器的du -s的值


 

import sys

import os
import threading,multiprocessing
import pexpect
import time,paramiko
def scopy():
ld=pexpect.spawn('scp -r /data/rhel-server-6.3-x86_64-dvd.iso root@192.168.2.3:/back/')
ld.expect('passWord:',timeout=None)
ld.sendline('zhengbin110')
        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.2.3'
 user='root'
 passwd='zhengbin110'
 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 /back')
  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 /data/rhel-server-6.3-x86_64-dvd.iso').readlines()[0].split()[0])*100
view_bar(bili,100,bar_word="#")
if int(bili) == 100:
break
print "COPY IS FINISH!!!!"
t.join()

相关文章