-
Notifications
You must be signed in to change notification settings - Fork 0
/
pessh.py
57 lines (47 loc) · 1.24 KB
/
pessh.py
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
52
53
54
55
56
57
#!/usr/bin/env python
import pexpect
import os
import signal
import sys
import subprocess
class PeSSH:
'''
simple usage:
c = PeSSH('example.com', encoding = 'GB18030')
c.login('foo')
c.prompt()
c.sendline('date')
c.prompt()
c.logout()
'''
def __init__(self, host, encoding = None, logfile = None):
self.host = host
self.encoding = encoding
self.logfile = logfile
def login(self, username, password = None):
cmd = 'ssh -l %s %s' % (username, self.host)
if self.encoding:
cmd = ('luit -encoding %s ' % self.encoding) + cmd
self.process = pexpect.spawn(cmd)
self.process.logfile = self.logfile
if password:
self.process.expect('.*[pP]assword:.*')
self.process.sendline(password)
def prompt(self):
self.process.expect('.*\$.*')
def expect(self, pattern):
self.process.expect(pattern)
def sendline(self, line):
self.process.sendline(line)
def interact(self):
self.process.interact()
def readline(self):
return self.process.readline()
def logout(self):
ps = subprocess.Popen('ps -o pid --noheaders --ppid %d' % self.process.pid, \
shell = True, stdout = subprocess.PIPE)
ps.wait()
pids = ps.stdout.read()
self.process.close()
for p in pids.splitlines():
os.kill(int(p), signal.SIGKILL)