forked from china-testing/python-api-tesing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlftp.py
More file actions
executable file
·51 lines (43 loc) · 1.51 KB
/
lftp.py
File metadata and controls
executable file
·51 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: xurongzhong#126.com wechat:pythontesting qq:37391319
# 技术支持 钉钉群:21745728(可以加钉钉pythontesting邀请加入)
# qq群:144081101 591302926 567351477
# CreateDate: 2018-07-27
import pexpect
class Lftp(object):
client = None
@classmethod
def connect(cls, ip, username="andrew", password="654321_", prompt='~>',
silent=False):
child = pexpect.spawn('lftp {}:{}@{}'.format(username, password, ip),
maxread=5000)
i = 1
# Enter password
while i != 0:
i = child.expect([prompt, pexpect.TIMEOUT])
if not silent:
print(child.before.decode('utf-8'), child.after.decode('utf-8'))
if i == 0: # find prompt
pass
else:
raise Exception('ERROR TIMEOUT! LFTP could not login. ')
Lftp.client = child
@classmethod
def command(cls, cmd, prompt='~>', silent=False):
Lftp.client.buffer = b''
Lftp.client.send(cmd + "\r")
# Lftp.client.setwinsize(400,400)
Lftp.client.expect([prompt,])
if not silent:
print(Lftp.client.before.decode('utf-8'),
Lftp.client.after.decode('utf-8'))
return Lftp.client.before, Lftp.client.after
@classmethod
def close(cls):
Lftp.client.close()
c = Lftp()
c.connect('172.20.17.200')
c.command("ls -l")
c.command("mirror projects")
c.close()