Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
fdcaa358
Commit
fdcaa358
authored
Jun 12, 2015
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify some api.py bug
parent
5d16836b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
118 additions
and
60 deletions
+118
-60
connect.py
connect.py
+33
-27
api.py
jumpserver/api.py
+85
-33
No files found.
connect.py
View file @
fdcaa358
...
...
@@ -244,32 +244,37 @@ def posix_shell(chan, username, host):
def
verify_connect
(
username
,
part_ip
):
pass
# ip_matched = []
# try:
# assets = get_asset(username=username)
# except ServerError, e:
# color_print(e, 'red')
# return False
#
# assets_info =
# for ip_info in hosts:
# if part_ip in ip_info[1:] and part_ip:
# ip_matched = [ip_info[1]]
# break
# for info in ip_info[1:]:
# if part_ip in info:
# ip_matched.append(ip_info[1])
#
# ip_matched = list(set(ip_matched))
# if len(ip_matched) > 1:
# for ip in ip_matched:
# print '%-15s -- %s' % (ip, hosts_attr[ip][2])
# elif len(ip_matched) < 1:
# color_print('No Permission or No host.', 'red')
# else:
# username, password, host, port = get_connect_item(username, ip_matched[0])
# connect(username, password, host, port, login_name)
ip_matched
=
[]
try
:
assets_info
=
user
.
get_asset_info
()
except
ServerError
,
e
:
color_print
(
e
,
'red'
)
return
False
for
ip
,
asset_info
in
assets_info
.
items
():
if
part_ip
in
asset_info
[
1
:]
and
part_ip
:
ip_matched
=
[
asset_info
[
1
]]
break
for
info
in
asset_info
[
1
:]:
if
part_ip
in
info
:
ip_matched
.
append
(
ip
)
logger
.
debug
(
'
%
s matched input
%
s:
%
s'
%
(
user
.
username
,
part_ip
,
ip_matched
))
ip_matched
=
list
(
set
(
ip_matched
))
if
len
(
ip_matched
)
>
1
:
for
ip
in
ip_matched
:
if
assets_info
[
ip
][
2
]:
print
'
%-15
s --
%
s'
%
(
ip
,
assets_info
[
ip
][
2
])
else
:
print
'
%-15
s'
%
ip
print
''
elif
len
(
ip_matched
)
<
1
:
color_print
(
'No Permission or No host.'
,
'red'
)
else
:
username
,
password
,
host
,
port
=
get_connect_item
(
username
,
ip_matched
[
0
])
connect
(
username
,
password
,
host
,
port
,
login_name
)
def
print_prompt
():
...
...
@@ -437,7 +442,8 @@ if __name__ == '__main__':
elif
gid_pattern
.
match
(
option
):
gid
=
option
[
1
:]
.
strip
()
asset_group
=
JassetGroup
(
id
=
gid
)
asset_group
.
get_asset_info
(
printable
=
True
)
if
asset_group
.
validate
():
asset_group
.
get_asset_info
(
printable
=
True
)
continue
elif
option
in
[
'E'
,
'e'
]:
exec_cmd_servers
(
login_name
)
...
...
jumpserver/api.py
View file @
fdcaa358
...
...
@@ -301,6 +301,7 @@ class Juser(object):
Jumpserver user class
用户类
"""
def
__init__
(
self
,
username
=
None
,
uid
=
None
):
if
username
:
user
=
User
.
objects
.
filter
(
username
=
username
)
...
...
@@ -313,16 +314,31 @@ class Juser(object):
user
=
user
[
0
]
self
.
user
=
user
self
.
id
=
user
.
id
self
.
username
=
user
.
username
self
.
name
=
user
.
name
# self.id = user.id
# self.username = user.username
# self.name = user.name
self
.
group
=
user
.
group
.
all
()
else
:
self
.
id
=
None
def
__repr__
(
self
):
if
self
.
id
:
return
'<
%
s Juser instance>'
%
getattr
(
self
.
user
,
'username'
)
else
:
return
'None'
def
__getattr__
(
self
,
item
):
if
self
.
id
:
return
getattr
(
self
.
user
,
item
)
else
:
return
None
def
validate
(
self
):
"""
Validate is or not a true user
鉴定用户
"""
if
self
.
user
:
if
self
.
id
:
return
True
else
:
return
False
...
...
@@ -362,21 +378,22 @@ class Juser(object):
asset_groups_info
[
group_id
][
1
])
else
:
print
"[
%3
s]
%
s"
%
(
group_id
,
asset_groups_info
[
group_id
][
0
])
print
''
else
:
return
asset_groups_info
def
get_asset
(
self
):
"""
Get the
hos
ts of under the user control.
Get the
asse
ts of under the user control.
获取主机列表
"""
hos
ts
=
[]
hos
t_groups
=
self
.
get_asset_group
()
asse
ts
=
[]
asse
t_groups
=
self
.
get_asset_group
()
for
host_group
in
hos
t_groups
:
hosts
.
extend
(
get_asset_group_member
(
host_group
.
id
))
for
asset_group
in
asse
t_groups
:
assets
.
extend
(
asset_group
.
asset_set
.
all
(
))
return
hos
ts
return
asse
ts
def
get_asset_info
(
self
,
printable
=
False
):
"""
...
...
@@ -389,9 +406,9 @@ class Juser(object):
for
asset
in
assets
:
asset_alias
=
AssetAlias
.
objects
.
filter
(
user
=
self
.
user
,
asset
=
asset
)
if
asset_alias
and
asset_alias
[
0
]
.
alias
!=
''
:
assets_info
[
asset
.
ip
]
=
[
asset
.
id
,
asset
.
ip
,
asset_alias
[
0
]
.
alias
]
assets_info
[
asset
.
ip
]
=
[
asset
.
id
,
asset
.
ip
,
str
(
asset_alias
[
0
]
.
alias
)
]
else
:
assets_info
[
asset
.
ip
]
=
[
asset
.
id
,
asset
.
ip
,
asset
.
comment
]
assets_info
[
asset
.
ip
]
=
[
asset
.
id
,
asset
.
ip
,
str
(
asset
.
comment
)
]
if
printable
:
ips
=
assets_info
.
keys
()
...
...
@@ -407,6 +424,7 @@ class Juser(object):
class
Jasset
(
object
):
def
__init__
(
self
,
ip
=
None
,
id
=
None
):
if
ip
:
asset
=
Asset
.
objects
.
filter
(
ip
=
ip
)
...
...
@@ -418,20 +436,40 @@ class Jasset(object):
if
asset
:
asset
=
asset
[
0
]
self
.
asset
=
asset
self
.
ip
=
asset
.
ip
self
.
id
=
asset
.
id
self
.
port
=
asset
.
port
self
.
comment
=
asset
.
comment
# self.ip = asset.ip
# self.id = asset.id
# self.port = asset.port
# self.comment = asset.comment
else
:
self
.
id
=
None
def
__repr__
(
self
):
if
self
.
id
:
return
'<
%
s Jasset instance>'
%
self
.
asset
.
ip
else
:
return
'None'
def
__getattr__
(
self
,
item
):
if
self
.
id
:
return
getattr
(
self
.
asset
,
item
)
else
:
return
None
def
validate
(
self
):
if
self
.
asset
:
"""
Validate is or not a true asset
判断是否存在
"""
if
self
.
id
:
return
True
else
:
return
False
class
JassetGroup
(
object
):
def
__init__
(
self
,
id
=
None
,
name
=
None
):
def
__init__
(
self
,
name
=
None
,
id
=
None
):
if
id
:
asset_group
=
BisGroup
.
objects
.
filter
(
id
=
int
(
id
))
elif
name
:
...
...
@@ -442,11 +480,23 @@ class JassetGroup(object):
if
asset_group
:
asset_group
=
asset_group
[
0
]
self
.
asset_group
=
asset_group
self
.
name
=
asset_group
.
name
#
self.name = asset_group.name
self
.
id
=
asset_group
.
id
else
:
self
.
id
=
None
def
__repr__
(
self
):
if
self
.
id
:
return
'<
%
s JassetGroup instance>'
%
self
.
name
else
:
return
'None'
def
validate
(
self
):
if
self
.
asset_group
:
"""
Validate it is a true asset group or not
鉴定是否为真是存在的组
"""
if
self
.
id
:
return
True
else
:
return
False
...
...
@@ -457,14 +507,16 @@ class JassetGroup(object):
def
get_asset_info
(
self
,
printable
=
False
):
assets
=
self
.
get_asset
()
for
asset
in
assets
:
print
'
%-15
s --
%
s'
%
(
asset
.
ip
,
asset
.
comment
)
if
asset
.
comment
:
print
'
%-15
s --
%
s'
%
(
asset
.
ip
,
asset
.
comment
)
else
:
print
'
%-15
s'
%
asset
.
ip
print
''
def
get_asset_num
(
self
):
return
len
(
self
.
get_asset
())
# def get_asset_group(user=None):
# """
# Get user host_groups.
...
...
@@ -484,18 +536,18 @@ class JassetGroup(object):
# return host_group_list
def
get_asset_group_member
(
gid
):
"""
Get host_group's member host
获取主机组下的主机
"""
hosts
=
[]
if
gid
:
host_group
=
BisGroup
.
objects
.
filter
(
id
=
gid
)
if
host_group
:
host_group
=
host_group
[
0
]
hosts
=
host_group
.
asset_set
.
all
()
return
hosts
#
def get_asset_group_member(gid):
#
"""
#
Get host_group's member host
#
获取主机组下的主机
#
"""
#
hosts = []
#
if gid:
#
host_group = BisGroup.objects.filter(id=gid)
#
if host_group:
#
host_group = host_group[0]
#
hosts = host_group.asset_set.all()
#
return hosts
# def get_asset(user=None):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment