使用pxssh

pull/2/head
guanghongwei 2014-10-31 14:35:33 +08:00
parent de2837229c
commit 63065f5c33
3 changed files with 17 additions and 33 deletions

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 2.7.8 (C:\Python27\python.exe)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 2.7.6 (C:/Python27/python.exe)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -16,6 +16,7 @@ from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex from binascii import b2a_hex, a2b_hex
import ConfigParser import ConfigParser
import paramiko import paramiko
import pxssh
base_dir = "/opt/jumpserver/" base_dir = "/opt/jumpserver/"
cf = ConfigParser.ConfigParser() cf = ConfigParser.ConfigParser()
@ -110,38 +111,20 @@ def connect(host, port, user, password):
os.mkdir(log_date_dir) os.mkdir(log_date_dir)
logfile = open("%s/%s_%s" % (log_date_dir, host, user), 'a') logfile = open("%s/%s_%s" % (log_date_dir, host, user), 'a')
logfile.write('\n\n%s\n\n' % time.strftime('%Y%m%d_%H%M%S')) logfile.write('\n\n%s\n\n' % time.strftime('%Y%m%d_%H%M%S'))
cmd = 'ssh -q -p %s %s@%s' % (port, user, host) try:
global foo global foo
foo = pexpect.spawn('/bin/bash', ['-c', cmd]) foo = pxssh.pxssh()
foo.logfile = logfile foo.login(host, user, password, port=port, auto_prompt_reset=False)
while True: foo.logfile = logfile
index = foo.expect(['continue', foo.sendline('')
'assword', signal.signal(signal.SIGWINCH, sigwinch_passthrough)
pexpect.EOF, size = getwinsize()
pexpect.TIMEOUT], timeout=3) foo.setwinsize(size[0], size[1])
if index == 0: foo.interact()
foo.sendline('yes') except pxssh.ExceptionPxssh as e:
continue print('密码错误: %s' % e)
elif index == 1: except KeyboardInterrupt:
foo.sendline(password) foo.logout()
index = foo.expect(['assword',
'.*',
pexpect.EOF,
pexpect.TIMEOUT], timeout=3)
if index == 1:
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
size = getwinsize()
foo.setwinsize(size[0], size[1])
foo.interact()
break
elif index == 0:
print "Password error."
break
else:
print "Login failed, please contact system administrator!"
break
foo.terminate(force=True)
def ip_all_select(username): def ip_all_select(username):

1
pip-requirement.txt Normal file
View File

@ -0,0 +1 @@
pexpect == 3.3