mirror of https://github.com/jumpserver/jumpserver
基本完成
parent
ab87420c9d
commit
bc78331d5b
71
connect.py
71
connect.py
|
@ -15,6 +15,7 @@ import getpass
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from Crypto.Cipher import AES
|
from Crypto.Cipher import AES
|
||||||
from binascii import b2a_hex, a2b_hex
|
from binascii import b2a_hex, a2b_hex
|
||||||
|
import re
|
||||||
|
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
|
||||||
django.setup()
|
django.setup()
|
||||||
|
@ -152,34 +153,62 @@ def posix_shell(chan, user, host):
|
||||||
|
|
||||||
|
|
||||||
def get_user_host(username):
|
def get_user_host(username):
|
||||||
hosts = {}
|
hosts_comments = {}
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(username=username)
|
user = User.objects.get(username=username)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
return {'Error': 'username %s is not exist.' % username}
|
return {'Error': 'username %s is not exist.' % username}, ['Error']
|
||||||
else:
|
else:
|
||||||
perm_all = user.permission_set.all()
|
perm_all = user.permission_set.all()
|
||||||
for perm in perm_all:
|
for perm in perm_all:
|
||||||
hosts[perm.asset.ip] = perm.asset.comment
|
hosts_comments[perm.asset.ip] = perm.asset.comment
|
||||||
return hosts
|
hosts = hosts_comments.keys()
|
||||||
|
hosts.sort()
|
||||||
|
return hosts_comments, hosts
|
||||||
|
|
||||||
|
|
||||||
def get_port(ip):
|
def get_connect_item(username, ip):
|
||||||
pass
|
try:
|
||||||
|
asset = Asset.objects.get(ip=ip)
|
||||||
|
port = asset.port
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
red_print("Host %s isn't exist." % ip)
|
||||||
|
return
|
||||||
|
|
||||||
|
if not asset.ldap_enable:
|
||||||
|
user = User.objects.get(username=username)
|
||||||
|
ldap_pwd = user.ldap_pwd
|
||||||
|
return username, ldap_pwd, ip, port
|
||||||
|
|
||||||
|
else:
|
||||||
|
perms = asset.permission_set.all()
|
||||||
|
perm = perms[0]
|
||||||
|
|
||||||
|
if perm.perm_user_type == 'S':
|
||||||
|
return asset.username_super, asset.password_super, ip, port
|
||||||
|
else:
|
||||||
|
return asset.username_common, asset.password_common, ip, port
|
||||||
|
|
||||||
|
|
||||||
def get_ldap_pwd(username):
|
def verify_connect(username, part_ip):
|
||||||
pass
|
ip_matched = []
|
||||||
|
hosts_comments, hosts = get_user_host(username)
|
||||||
|
for ip in hosts:
|
||||||
|
if part_ip in ip:
|
||||||
|
ip_matched.append(ip)
|
||||||
|
|
||||||
|
if len(ip_matched) > 1:
|
||||||
def connect_one(username, segment):
|
for ip in ip_matched:
|
||||||
assets = Asset.objects.filter(ip__icontains=segment)
|
print '%s -- %s' % (ip, hosts_comments[ip])
|
||||||
if len(assets) > 1:
|
elif len(ip_matched) < 1:
|
||||||
for asset in assets:
|
red_print('No Permission or No host.')
|
||||||
print '%s -- %s' % (asset.ip, asset.comment)
|
else:
|
||||||
elif len(assets) == 1:
|
try:
|
||||||
asset = assets[0]
|
username, password, host, port = get_connect_item(username, ip_matched[0])
|
||||||
permission = asset.permission_set.all()
|
except (ObjectDoesNotExist, IndexError):
|
||||||
|
red_print('Get get_connect_item Error.')
|
||||||
|
else:
|
||||||
|
connect(username, password, host, port)
|
||||||
|
|
||||||
|
|
||||||
def print_prompt():
|
def print_prompt():
|
||||||
|
@ -193,9 +222,9 @@ def print_prompt():
|
||||||
|
|
||||||
|
|
||||||
def print_user_host(username):
|
def print_user_host(username):
|
||||||
host_all = get_user_host(username)
|
hosts_comments, hosts = get_user_host(username)
|
||||||
for ip, comment in host_all.items():
|
for ip in hosts:
|
||||||
print '%s -- %s' % (ip, comment)
|
print '%s -- %s' % (ip, hosts_comments[ip])
|
||||||
|
|
||||||
|
|
||||||
def connect(username, password, host, port):
|
def connect(username, password, host, port):
|
||||||
|
@ -258,7 +287,7 @@ if __name__ == '__main__':
|
||||||
elif option in ['Q', 'q']:
|
elif option in ['Q', 'q']:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
else:
|
else:
|
||||||
pass
|
verify_connect(login_name, option)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue