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
ad16d8b5
Commit
ad16d8b5
authored
9 years ago
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化connect.py 和 api.py
parent
9f0620f9
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
30 deletions
+45
-30
connect.py
connect.py
+40
-28
models.py
jlog/models.py
+1
-1
api.py
jumpserver/api.py
+0
-0
models.py
juser/models.py
+4
-1
No files found.
connect.py
View file @
ad16d8b5
...
@@ -26,8 +26,9 @@ os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
...
@@ -26,8 +26,9 @@ os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
if
django
.
get_version
()
!=
'1.6'
:
if
django
.
get_version
()
!=
'1.6'
:
django
.
setup
()
django
.
setup
()
from
jlog.models
import
Log
from
jlog.models
import
Log
from
jumpserver.api
import
CONF
,
BASE_DIR
,
ServerError
,
User
,
UserGroup
,
Asset
,
BisGroup
from
jumpserver.api
import
CONF
,
BASE_DIR
,
ServerError
,
User
,
UserGroup
,
Asset
,
get_object
from
jumpserver.api
import
CRYPTOR
,
logger
,
is_dir
from
jumpserver.api
import
CRYPTOR
,
logger
,
is_dir
from
jumpserver.api
import
BisGroup
as
AssetGroup
try
:
try
:
import
termios
import
termios
...
@@ -38,7 +39,7 @@ except ImportError:
...
@@ -38,7 +39,7 @@ except ImportError:
sys
.
exit
()
sys
.
exit
()
log_dir
=
os
.
path
.
join
(
BASE_DIR
,
'logs'
)
log_dir
=
os
.
path
.
join
(
BASE_DIR
,
'logs'
)
login_user
=
User
(
username
=
getpass
.
getuser
())
login_user
=
get_object
(
User
,
username
=
getpass
.
getuser
())
def
color_print
(
msg
,
color
=
'red'
,
exits
=
False
):
def
color_print
(
msg
,
color
=
'red'
,
exits
=
False
):
...
@@ -98,14 +99,14 @@ class Jtty(object):
...
@@ -98,14 +99,14 @@ class Jtty(object):
timestamp_start
=
int
(
time
.
time
())
timestamp_start
=
int
(
time
.
time
())
date_start
=
time
.
strftime
(
'
%
Y
%
m
%
d'
,
time
.
localtime
(
timestamp_start
))
date_start
=
time
.
strftime
(
'
%
Y
%
m
%
d'
,
time
.
localtime
(
timestamp_start
))
time_start
=
time
.
strftime
(
'
%
H
%
M
%
S'
,
time
.
localtime
(
timestamp_start
))
time_start
=
time
.
strftime
(
'
%
H
%
M
%
S'
,
time
.
localtime
(
timestamp_start
))
today_connect_log_dir
=
os
.
path
.
join
(
tty_log_dir
,
date_start
)
log_filename
=
'
%
s_
%
s_
%
s.log'
%
(
self
.
username
,
self
.
ip
,
time_start
)
log_filename
=
'
%
s_
%
s_
%
s.log'
%
(
self
.
username
,
self
.
ip
,
time_start
)
today_connect_log_dir
=
os
.
path
.
join
(
tty_log_dir
,
date_start
)
log_file_path
=
os
.
path
.
join
(
today_connect_log_dir
,
log_filename
)
log_file_path
=
os
.
path
.
join
(
today_connect_log_dir
,
log_filename
)
dept_name
=
self
.
user
.
dept
.
name
dept_name
=
self
.
user
.
dept
.
name
pid
=
os
.
getpid
()
pid
=
os
.
getpid
()
pts
=
os
.
popen
(
"ps axu | grep
%
s | grep -v grep | awk '{ print $7 }'"
%
pid
)
.
read
()
.
strip
()
pts
=
os
.
popen
(
"ps axu | grep
%
s | grep -v grep | awk '{ print $7 }'"
%
pid
)
.
read
()
.
strip
()
remote_ip
=
os
.
popen
(
"who | grep
%
s | awk '{ print $5 }'"
%
pts
)
.
read
()
.
strip
(
'()
\n
'
)
ip_list
=
os
.
popen
(
"who | grep
%
s | awk '{ print $5 }'"
%
pts
)
.
read
()
.
strip
(
'()
\n
'
)
try
:
try
:
is_dir
(
today_connect_log_dir
)
is_dir
(
today_connect_log_dir
)
...
@@ -117,7 +118,7 @@ class Jtty(object):
...
@@ -117,7 +118,7 @@ class Jtty(object):
except
IOError
:
except
IOError
:
raise
ServerError
(
'Create logfile failed, Please modify
%
s permission.'
%
today_connect_log_dir
)
raise
ServerError
(
'Create logfile failed, Please modify
%
s permission.'
%
today_connect_log_dir
)
log
=
Log
(
user
=
self
.
username
,
host
=
self
.
ip
,
remote_ip
=
remote_ip
,
dept_name
=
dept_name
,
log
=
Log
(
user
=
self
.
username
,
host
=
self
.
ip
,
remote_ip
=
ip_list
,
dept_name
=
dept_name
,
log_path
=
log_file_path
,
start_time
=
datetime
.
datetime
.
now
(),
pid
=
pid
)
log_path
=
log_file_path
,
start_time
=
datetime
.
datetime
.
now
(),
pid
=
pid
)
log_file
.
write
(
'Start time is
%
s
\n
'
%
datetime
.
datetime
.
now
())
log_file
.
write
(
'Start time is
%
s
\n
'
%
datetime
.
datetime
.
now
())
log
.
save
()
log
.
save
()
...
@@ -164,18 +165,17 @@ class Jtty(object):
...
@@ -164,18 +165,17 @@ class Jtty(object):
log_file
.
write
(
'End time is
%
s'
%
datetime
.
datetime
.
now
())
log_file
.
write
(
'End time is
%
s'
%
datetime
.
datetime
.
now
())
log_file
.
close
()
log_file
.
close
()
log
.
is_finished
=
True
log
.
is_finished
=
True
log
.
log
_finished
=
False
log
.
handle
_finished
=
False
log
.
end_time
=
datetime
.
datetime
.
now
()
log
.
end_time
=
datetime
.
datetime
.
now
()
log
.
save
()
log
.
save
()
def
get_connect_item
(
self
):
def
get_connect_item
(
self
):
port
=
int
(
self
.
asset
.
port
)
"""获取连接需要的参数,也就是服务ip, 端口, 用户账号和密码"""
if
not
self
.
asset
.
is_active
:
if
not
self
.
asset
.
is_active
:
raise
ServerError
(
'Host
%
s is not active.'
%
self
.
ip
)
raise
ServerError
(
'
该主机被禁用
Host
%
s is not active.'
%
self
.
ip
)
if
not
self
.
user
.
is_active
:
if
not
self
.
user
.
is_active
:
raise
ServerError
(
'User
%
s is not active.'
%
self
.
username
)
raise
ServerError
(
'
该用户被禁用
User
%
s is not active.'
%
self
.
username
)
login_type_dict
=
{
login_type_dict
=
{
'L'
:
self
.
user
.
ldap_pwd
,
'L'
:
self
.
user
.
ldap_pwd
,
...
@@ -183,37 +183,38 @@ class Jtty(object):
...
@@ -183,37 +183,38 @@ class Jtty(object):
if
self
.
asset
.
login_type
in
login_type_dict
:
if
self
.
asset
.
login_type
in
login_type_dict
:
password
=
CRYPTOR
.
decrypt
(
login_type_dict
[
self
.
asset
.
login_type
])
password
=
CRYPTOR
.
decrypt
(
login_type_dict
[
self
.
asset
.
login_type
])
return
self
.
username
,
password
,
self
.
ip
,
port
return
self
.
username
,
password
,
self
.
ip
,
int
(
self
.
asset
.
port
)
elif
self
.
asset
.
login_type
==
'M'
:
elif
self
.
asset
.
login_type
==
'M'
:
username
=
self
.
asset
.
username
username
=
self
.
asset
.
username
password
=
CRYPTOR
.
decrypt
(
self
.
asset
.
password
)
password
=
CRYPTOR
.
decrypt
(
self
.
asset
.
password
)
return
username
,
password
,
self
.
ip
,
port
return
username
,
password
,
self
.
ip
,
int
(
self
.
asset
.
port
)
else
:
else
:
raise
ServerError
(
'Login type is not in ["L", "M"]'
)
raise
ServerError
(
'
不支持的服务器登录方式
Login type is not in ["L", "M"]'
)
def
connect
(
self
):
def
connect
(
self
):
"""
"""
Connect server.
Connect server.
连接服务器
"""
"""
username
,
password
,
ip
,
port
=
self
.
get_connect_item
()
username
,
password
,
ip
,
port
=
self
.
get_connect_item
()
logger
.
debug
(
"username:
%
s, password:
%
s, ip:
%
s, port:
%
s"
%
(
username
,
password
,
ip
,
port
))
logger
.
debug
(
"username:
%
s, password:
%
s, ip:
%
s, port:
%
s"
%
(
username
,
password
,
ip
,
port
))
ps1
=
"PS1='[
\
u@
%
s
\
W]
\
$ '
\n
"
%
self
.
ip
ps1
=
"PS1='[
\
u@
%
s
\
W]
\
$ '
\n
"
%
self
.
ip
login_msg
=
"clear;echo -e '
\\
033[32mLogin
%
s done. Enjoy it.
\\
033[0m'
\n
"
%
self
.
ip
login_msg
=
"clear;echo -e '
\\
033[32mLogin
%
s done. Enjoy it.
\\
033[0m'
\n
"
%
ip
# Make a ssh connection
#
发起ssh连接请求
Make a ssh connection
ssh
=
paramiko
.
SSHClient
()
ssh
=
paramiko
.
SSHClient
()
ssh
.
load_system_host_keys
()
ssh
.
load_system_host_keys
()
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
try
:
try
:
ssh
.
connect
(
ip
,
port
=
port
,
username
=
username
,
password
=
password
,
compress
=
True
)
ssh
.
connect
(
ip
,
port
=
port
,
username
=
username
,
password
=
password
,
compress
=
True
)
except
paramiko
.
ssh_exception
.
AuthenticationException
,
paramiko
.
ssh_exception
.
SSHException
:
except
paramiko
.
ssh_exception
.
AuthenticationException
,
paramiko
.
ssh_exception
.
SSHException
:
raise
ServerError
(
'Authentication Error.'
)
raise
ServerError
(
'
认证错误
Authentication Error.'
)
except
socket
.
error
:
except
socket
.
error
:
raise
ServerError
(
'Connect SSH Socket Port Error, Please Correct it.'
)
raise
ServerError
(
'
端口可能不对
Connect SSH Socket Port Error, Please Correct it.'
)
# Make a channel and set windows size
#
获取连接的隧道并设置窗口大小
Make a channel and set windows size
global
channel
global
channel
win_size
=
self
.
get_win_size
()
win_size
=
self
.
get_win_size
()
self
.
chan
=
channel
=
ssh
.
invoke_shell
(
height
=
win_size
[
0
],
width
=
win_size
[
1
])
self
.
chan
=
channel
=
ssh
.
invoke_shell
(
height
=
win_size
[
0
],
width
=
win_size
[
1
])
...
@@ -222,7 +223,7 @@ class Jtty(object):
...
@@ -222,7 +223,7 @@ class Jtty(object):
except
:
except
:
pass
pass
# Set PS1 and msg it
#
设置PS1并提示
Set PS1 and msg it
channel
.
send
(
ps1
)
channel
.
send
(
ps1
)
channel
.
send
(
login_msg
)
channel
.
send
(
login_msg
)
...
@@ -235,6 +236,7 @@ class Jtty(object):
...
@@ -235,6 +236,7 @@ class Jtty(object):
def
verify_connect
(
user
,
option
):
def
verify_connect
(
user
,
option
):
"""鉴定用户是否有该主机权限 或 匹配到的ip是否唯一"""
ip_matched
=
[]
ip_matched
=
[]
try
:
try
:
assets_info
=
login_user
.
get_asset_info
()
assets_info
=
login_user
.
get_asset_info
()
...
@@ -255,16 +257,20 @@ def verify_connect(user, option):
...
@@ -255,16 +257,20 @@ def verify_connect(user, option):
ip_matched
=
list
(
set
(
ip_matched
))
ip_matched
=
list
(
set
(
ip_matched
))
if
len
(
ip_matched
)
>
1
:
if
len
(
ip_matched
)
>
1
:
ip_comment
=
{}
for
ip
in
ip_matched
:
for
ip
in
ip_matched
:
if
assets_info
[
ip
][
2
]:
ip_comment
[
ip
]
=
assets_info
[
ip
][
2
]
print
'
%-15
s --
%
s'
%
(
ip
,
assets_info
[
ip
][
2
])
for
ip
in
sorted
(
ip_comment
):
if
ip_comment
[
ip
]:
print
'
%-15
s --
%
s'
%
(
ip
,
ip_comment
[
ip
])
else
:
else
:
print
'
%-15
s'
%
ip
print
'
%-15
s'
%
ip
print
''
print
''
elif
len
(
ip_matched
)
<
1
:
elif
len
(
ip_matched
)
<
1
:
color_print
(
'No Permission or No host.'
,
'red'
)
color_print
(
'
没有该主机,或者您没有该主机的权限
No Permission or No host.'
,
'red'
)
else
:
else
:
asset
=
Asset
(
ip
=
ip_matched
[
0
])
.
asset
asset
=
get_object
(
Asset
,
ip
=
ip_matched
[
0
])
jtty
=
Jtty
(
user
,
asset
)
jtty
=
Jtty
(
user
,
asset
)
jtty
.
connect
()
jtty
.
connect
()
...
@@ -351,12 +357,13 @@ def print_prompt():
...
@@ -351,12 +357,13 @@ def print_prompt():
# multi_remote_exec_cmd(hosts, username, cmd)
# multi_remote_exec_cmd(hosts, username, cmd)
if
__name__
==
'__main__'
:
def
main
()
:
if
not
login_user
.
validate
():
if
not
login_user
:
# 判断用户是否存在
color_print
(
u'没有该用户 No that user.'
,
exits
=
True
)
color_print
(
u'没有该用户
,或许你是以root运行的
No that user.'
,
exits
=
True
)
print_prompt
()
print_prompt
()
gid_pattern
=
re
.
compile
(
r'^g\d+$'
)
gid_pattern
=
re
.
compile
(
r'^g\d+$'
)
try
:
try
:
while
True
:
while
True
:
try
:
try
:
...
@@ -374,8 +381,8 @@ if __name__ == '__main__':
...
@@ -374,8 +381,8 @@ if __name__ == '__main__':
continue
continue
elif
gid_pattern
.
match
(
option
):
elif
gid_pattern
.
match
(
option
):
gid
=
option
[
1
:]
.
strip
()
gid
=
option
[
1
:]
.
strip
()
asset_group
=
JassetGroup
(
id
=
gid
)
asset_group
=
get_object
(
AssetGroup
,
id
=
gid
)
if
asset_group
.
validate
()
and
asset_group
.
is_permed
(
user
=
login_user
.
user
):
if
asset_group
and
asset_group
.
is_permed
(
user
=
login_
user
):
asset_group
.
get_asset_info
(
printable
=
True
)
asset_group
.
get_asset_info
(
printable
=
True
)
continue
continue
elif
option
in
[
'E'
,
'e'
]:
elif
option
in
[
'E'
,
'e'
]:
...
@@ -390,3 +397,8 @@ if __name__ == '__main__':
...
@@ -390,3 +397,8 @@ if __name__ == '__main__':
color_print
(
e
,
'red'
)
color_print
(
e
,
'red'
)
except
IndexError
:
except
IndexError
:
pass
pass
if
__name__
==
'__main__'
:
main
()
This diff is collapsed.
Click to expand it.
jlog/models.py
View file @
ad16d8b5
...
@@ -10,7 +10,7 @@ class Log(models.Model):
...
@@ -10,7 +10,7 @@ class Log(models.Model):
start_time
=
models
.
DateTimeField
(
null
=
True
)
start_time
=
models
.
DateTimeField
(
null
=
True
)
pid
=
models
.
IntegerField
(
max_length
=
10
)
pid
=
models
.
IntegerField
(
max_length
=
10
)
is_finished
=
models
.
BooleanField
(
default
=
False
)
is_finished
=
models
.
BooleanField
(
default
=
False
)
log
_finished
=
models
.
BooleanField
(
default
=
False
)
handle
_finished
=
models
.
BooleanField
(
default
=
False
)
end_time
=
models
.
DateTimeField
(
null
=
True
)
end_time
=
models
.
DateTimeField
(
null
=
True
)
def
__unicode__
(
self
):
def
__unicode__
(
self
):
...
...
This diff is collapsed.
Click to expand it.
jumpserver/api.py
View file @
ad16d8b5
This diff is collapsed.
Click to expand it.
juser/models.py
View file @
ad16d8b5
...
@@ -19,6 +19,9 @@ class UserGroup(models.Model):
...
@@ -19,6 +19,9 @@ class UserGroup(models.Model):
def
__unicode__
(
self
):
def
__unicode__
(
self
):
return
self
.
name
return
self
.
name
def
get_user
(
self
):
return
self
.
user_set
.
all
()
class
User
(
models
.
Model
):
class
User
(
models
.
Model
):
USER_ROLE_CHOICES
=
(
USER_ROLE_CHOICES
=
(
...
@@ -104,7 +107,7 @@ class User(models.Model):
...
@@ -104,7 +107,7 @@ class User(models.Model):
assets
=
self
.
get_asset
()
assets
=
self
.
get_asset
()
for
asset
in
assets
:
for
asset
in
assets
:
asset_alias
=
AssetAlias
.
objects
.
filter
(
user
=
self
.
user
,
asset
=
asset
)
asset_alias
=
AssetAlias
.
objects
.
filter
(
user
=
self
,
asset
=
asset
)
if
asset_alias
and
asset_alias
[
0
]
.
alias
!=
''
:
if
asset_alias
and
asset_alias
[
0
]
.
alias
!=
''
:
assets_info
[
asset
.
ip
]
=
[
asset
.
id
,
asset
.
ip
,
str
(
asset_alias
[
0
]
.
alias
)]
assets_info
[
asset
.
ip
]
=
[
asset
.
id
,
asset
.
ip
,
str
(
asset_alias
[
0
]
.
alias
)]
else
:
else
:
...
...
This diff is collapsed.
Click to expand it.
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