0lever-so 1.9.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 0lever_so-1.9.1/0lever_so.egg-info/PKG-INFO +117 -0
- 0lever_so-1.9.1/0lever_so.egg-info/SOURCES.txt +10 -0
- 0lever_so-1.9.1/0lever_so.egg-info/dependency_links.txt +1 -0
- 0lever_so-1.9.1/0lever_so.egg-info/entry_points.txt +3 -0
- 0lever_so-1.9.1/0lever_so.egg-info/requires.txt +2 -0
- 0lever_so-1.9.1/0lever_so.egg-info/top_level.txt +1 -0
- 0lever_so-1.9.1/PKG-INFO +117 -0
- 0lever_so-1.9.1/README.rst +101 -0
- 0lever_so-1.9.1/setup.cfg +4 -0
- 0lever_so-1.9.1/setup.py +41 -0
- 0lever_so-1.9.1/so/__init__.py +0 -0
- 0lever_so-1.9.1/so/so.py +195 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: 0lever-so
|
|
3
|
+
Version: 1.9.1
|
|
4
|
+
Summary: 服务器登录工具
|
|
5
|
+
Home-page: https://github.com/0lever/so
|
|
6
|
+
Author: fqiyou
|
|
7
|
+
Author-email: yc.fqiyou@gmail.com
|
|
8
|
+
Requires-Dist: pyyaml
|
|
9
|
+
Requires-Dist: pexpect
|
|
10
|
+
Dynamic: author
|
|
11
|
+
Dynamic: author-email
|
|
12
|
+
Dynamic: description
|
|
13
|
+
Dynamic: home-page
|
|
14
|
+
Dynamic: requires-dist
|
|
15
|
+
Dynamic: summary
|
|
16
|
+
|
|
17
|
+
======
|
|
18
|
+
so
|
|
19
|
+
======
|
|
20
|
+
|
|
21
|
+
This is a SSH login tool
|
|
22
|
+
|
|
23
|
+
Installation
|
|
24
|
+
============
|
|
25
|
+
|
|
26
|
+
::
|
|
27
|
+
|
|
28
|
+
pip install --upgrade 0lever-so
|
|
29
|
+
or
|
|
30
|
+
pip install --upgrade 0lever-so -i https://pypi.org/simple/
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
Usage
|
|
34
|
+
=====
|
|
35
|
+
|
|
36
|
+
::
|
|
37
|
+
|
|
38
|
+
# 初始化配置文件,升级无需初始化,chmod 400 ~/.so/keys/*
|
|
39
|
+
➜ ~ so_install
|
|
40
|
+
➜ ~ cd .so
|
|
41
|
+
➜ .so tree
|
|
42
|
+
.
|
|
43
|
+
├── keys
|
|
44
|
+
│ └── demo.pem
|
|
45
|
+
└── password.yaml
|
|
46
|
+
|
|
47
|
+
1 directory, 2 files
|
|
48
|
+
➜ .so
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
::
|
|
52
|
+
|
|
53
|
+
# 配置文件
|
|
54
|
+
ssh:
|
|
55
|
+
- id: 1
|
|
56
|
+
name: demo1
|
|
57
|
+
user: fqiyou
|
|
58
|
+
password: xxx
|
|
59
|
+
host: 1.1.1.1
|
|
60
|
+
port: 20755
|
|
61
|
+
- id: 2
|
|
62
|
+
name: demo2
|
|
63
|
+
user: fqiyou
|
|
64
|
+
password: xxx
|
|
65
|
+
host: 1.1.1.1
|
|
66
|
+
port: 39986
|
|
67
|
+
- id: 3
|
|
68
|
+
name: demo3
|
|
69
|
+
user: root
|
|
70
|
+
password: demo.pem
|
|
71
|
+
host: 1.1.1.1
|
|
72
|
+
port: 22
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
Other-shell
|
|
76
|
+
|
|
77
|
+
=====
|
|
78
|
+
|
|
79
|
+
::
|
|
80
|
+
|
|
81
|
+
#!/usr/bin/expect
|
|
82
|
+
set USER "xxx"
|
|
83
|
+
set PASSWD "xxx"
|
|
84
|
+
set timeout 10
|
|
85
|
+
|
|
86
|
+
trap {
|
|
87
|
+
set rows [stty rows]
|
|
88
|
+
set cols [stty columns]
|
|
89
|
+
stty rows $rows columns $cols < $spawn_out(slave,name)
|
|
90
|
+
} WINCH
|
|
91
|
+
spawn su - $USER
|
|
92
|
+
expect "Password: "
|
|
93
|
+
send "$PASSWD\n"
|
|
94
|
+
interact
|
|
95
|
+
|
|
96
|
+
::
|
|
97
|
+
|
|
98
|
+
#!/usr/bin/expect -f
|
|
99
|
+
set HOST [lindex $argv 0]
|
|
100
|
+
set USER [lindex $argv 1]
|
|
101
|
+
set PASSWD [lindex $argv 2]
|
|
102
|
+
set PORT [lindex $argv 3]
|
|
103
|
+
set timeout 10
|
|
104
|
+
|
|
105
|
+
trap {
|
|
106
|
+
set rows [stty rows]
|
|
107
|
+
set cols [stty columns]
|
|
108
|
+
stty rows $rows columns $cols < $spawn_out(slave,name)
|
|
109
|
+
} WINCH
|
|
110
|
+
|
|
111
|
+
spawn ssh $USER@HOST -p $PORT
|
|
112
|
+
expect {
|
|
113
|
+
"*yes/no" {send "yes\r"; exp_continue}
|
|
114
|
+
"*password:" {send "$PASSWD\r"}
|
|
115
|
+
}
|
|
116
|
+
interact
|
|
117
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.rst
|
|
2
|
+
setup.py
|
|
3
|
+
0lever_so.egg-info/PKG-INFO
|
|
4
|
+
0lever_so.egg-info/SOURCES.txt
|
|
5
|
+
0lever_so.egg-info/dependency_links.txt
|
|
6
|
+
0lever_so.egg-info/entry_points.txt
|
|
7
|
+
0lever_so.egg-info/requires.txt
|
|
8
|
+
0lever_so.egg-info/top_level.txt
|
|
9
|
+
so/__init__.py
|
|
10
|
+
so/so.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
so
|
0lever_so-1.9.1/PKG-INFO
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: 0lever-so
|
|
3
|
+
Version: 1.9.1
|
|
4
|
+
Summary: 服务器登录工具
|
|
5
|
+
Home-page: https://github.com/0lever/so
|
|
6
|
+
Author: fqiyou
|
|
7
|
+
Author-email: yc.fqiyou@gmail.com
|
|
8
|
+
Requires-Dist: pyyaml
|
|
9
|
+
Requires-Dist: pexpect
|
|
10
|
+
Dynamic: author
|
|
11
|
+
Dynamic: author-email
|
|
12
|
+
Dynamic: description
|
|
13
|
+
Dynamic: home-page
|
|
14
|
+
Dynamic: requires-dist
|
|
15
|
+
Dynamic: summary
|
|
16
|
+
|
|
17
|
+
======
|
|
18
|
+
so
|
|
19
|
+
======
|
|
20
|
+
|
|
21
|
+
This is a SSH login tool
|
|
22
|
+
|
|
23
|
+
Installation
|
|
24
|
+
============
|
|
25
|
+
|
|
26
|
+
::
|
|
27
|
+
|
|
28
|
+
pip install --upgrade 0lever-so
|
|
29
|
+
or
|
|
30
|
+
pip install --upgrade 0lever-so -i https://pypi.org/simple/
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
Usage
|
|
34
|
+
=====
|
|
35
|
+
|
|
36
|
+
::
|
|
37
|
+
|
|
38
|
+
# 初始化配置文件,升级无需初始化,chmod 400 ~/.so/keys/*
|
|
39
|
+
➜ ~ so_install
|
|
40
|
+
➜ ~ cd .so
|
|
41
|
+
➜ .so tree
|
|
42
|
+
.
|
|
43
|
+
├── keys
|
|
44
|
+
│ └── demo.pem
|
|
45
|
+
└── password.yaml
|
|
46
|
+
|
|
47
|
+
1 directory, 2 files
|
|
48
|
+
➜ .so
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
::
|
|
52
|
+
|
|
53
|
+
# 配置文件
|
|
54
|
+
ssh:
|
|
55
|
+
- id: 1
|
|
56
|
+
name: demo1
|
|
57
|
+
user: fqiyou
|
|
58
|
+
password: xxx
|
|
59
|
+
host: 1.1.1.1
|
|
60
|
+
port: 20755
|
|
61
|
+
- id: 2
|
|
62
|
+
name: demo2
|
|
63
|
+
user: fqiyou
|
|
64
|
+
password: xxx
|
|
65
|
+
host: 1.1.1.1
|
|
66
|
+
port: 39986
|
|
67
|
+
- id: 3
|
|
68
|
+
name: demo3
|
|
69
|
+
user: root
|
|
70
|
+
password: demo.pem
|
|
71
|
+
host: 1.1.1.1
|
|
72
|
+
port: 22
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
Other-shell
|
|
76
|
+
|
|
77
|
+
=====
|
|
78
|
+
|
|
79
|
+
::
|
|
80
|
+
|
|
81
|
+
#!/usr/bin/expect
|
|
82
|
+
set USER "xxx"
|
|
83
|
+
set PASSWD "xxx"
|
|
84
|
+
set timeout 10
|
|
85
|
+
|
|
86
|
+
trap {
|
|
87
|
+
set rows [stty rows]
|
|
88
|
+
set cols [stty columns]
|
|
89
|
+
stty rows $rows columns $cols < $spawn_out(slave,name)
|
|
90
|
+
} WINCH
|
|
91
|
+
spawn su - $USER
|
|
92
|
+
expect "Password: "
|
|
93
|
+
send "$PASSWD\n"
|
|
94
|
+
interact
|
|
95
|
+
|
|
96
|
+
::
|
|
97
|
+
|
|
98
|
+
#!/usr/bin/expect -f
|
|
99
|
+
set HOST [lindex $argv 0]
|
|
100
|
+
set USER [lindex $argv 1]
|
|
101
|
+
set PASSWD [lindex $argv 2]
|
|
102
|
+
set PORT [lindex $argv 3]
|
|
103
|
+
set timeout 10
|
|
104
|
+
|
|
105
|
+
trap {
|
|
106
|
+
set rows [stty rows]
|
|
107
|
+
set cols [stty columns]
|
|
108
|
+
stty rows $rows columns $cols < $spawn_out(slave,name)
|
|
109
|
+
} WINCH
|
|
110
|
+
|
|
111
|
+
spawn ssh $USER@HOST -p $PORT
|
|
112
|
+
expect {
|
|
113
|
+
"*yes/no" {send "yes\r"; exp_continue}
|
|
114
|
+
"*password:" {send "$PASSWD\r"}
|
|
115
|
+
}
|
|
116
|
+
interact
|
|
117
|
+
```
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
======
|
|
2
|
+
so
|
|
3
|
+
======
|
|
4
|
+
|
|
5
|
+
This is a SSH login tool
|
|
6
|
+
|
|
7
|
+
Installation
|
|
8
|
+
============
|
|
9
|
+
|
|
10
|
+
::
|
|
11
|
+
|
|
12
|
+
pip install --upgrade 0lever-so
|
|
13
|
+
or
|
|
14
|
+
pip install --upgrade 0lever-so -i https://pypi.org/simple/
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
Usage
|
|
18
|
+
=====
|
|
19
|
+
|
|
20
|
+
::
|
|
21
|
+
|
|
22
|
+
# 初始化配置文件,升级无需初始化,chmod 400 ~/.so/keys/*
|
|
23
|
+
➜ ~ so_install
|
|
24
|
+
➜ ~ cd .so
|
|
25
|
+
➜ .so tree
|
|
26
|
+
.
|
|
27
|
+
├── keys
|
|
28
|
+
│ └── demo.pem
|
|
29
|
+
└── password.yaml
|
|
30
|
+
|
|
31
|
+
1 directory, 2 files
|
|
32
|
+
➜ .so
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
::
|
|
36
|
+
|
|
37
|
+
# 配置文件
|
|
38
|
+
ssh:
|
|
39
|
+
- id: 1
|
|
40
|
+
name: demo1
|
|
41
|
+
user: fqiyou
|
|
42
|
+
password: xxx
|
|
43
|
+
host: 1.1.1.1
|
|
44
|
+
port: 20755
|
|
45
|
+
- id: 2
|
|
46
|
+
name: demo2
|
|
47
|
+
user: fqiyou
|
|
48
|
+
password: xxx
|
|
49
|
+
host: 1.1.1.1
|
|
50
|
+
port: 39986
|
|
51
|
+
- id: 3
|
|
52
|
+
name: demo3
|
|
53
|
+
user: root
|
|
54
|
+
password: demo.pem
|
|
55
|
+
host: 1.1.1.1
|
|
56
|
+
port: 22
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
Other-shell
|
|
60
|
+
|
|
61
|
+
=====
|
|
62
|
+
|
|
63
|
+
::
|
|
64
|
+
|
|
65
|
+
#!/usr/bin/expect
|
|
66
|
+
set USER "xxx"
|
|
67
|
+
set PASSWD "xxx"
|
|
68
|
+
set timeout 10
|
|
69
|
+
|
|
70
|
+
trap {
|
|
71
|
+
set rows [stty rows]
|
|
72
|
+
set cols [stty columns]
|
|
73
|
+
stty rows $rows columns $cols < $spawn_out(slave,name)
|
|
74
|
+
} WINCH
|
|
75
|
+
spawn su - $USER
|
|
76
|
+
expect "Password: "
|
|
77
|
+
send "$PASSWD\n"
|
|
78
|
+
interact
|
|
79
|
+
|
|
80
|
+
::
|
|
81
|
+
|
|
82
|
+
#!/usr/bin/expect -f
|
|
83
|
+
set HOST [lindex $argv 0]
|
|
84
|
+
set USER [lindex $argv 1]
|
|
85
|
+
set PASSWD [lindex $argv 2]
|
|
86
|
+
set PORT [lindex $argv 3]
|
|
87
|
+
set timeout 10
|
|
88
|
+
|
|
89
|
+
trap {
|
|
90
|
+
set rows [stty rows]
|
|
91
|
+
set cols [stty columns]
|
|
92
|
+
stty rows $rows columns $cols < $spawn_out(slave,name)
|
|
93
|
+
} WINCH
|
|
94
|
+
|
|
95
|
+
spawn ssh $USER@HOST -p $PORT
|
|
96
|
+
expect {
|
|
97
|
+
"*yes/no" {send "yes\r"; exp_continue}
|
|
98
|
+
"*password:" {send "$PASSWD\r"}
|
|
99
|
+
}
|
|
100
|
+
interact
|
|
101
|
+
```
|
0lever_so-1.9.1/setup.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from setuptools import setup, find_packages
|
|
4
|
+
|
|
5
|
+
setup(name='0lever-so',
|
|
6
|
+
|
|
7
|
+
version="1.9.1",
|
|
8
|
+
|
|
9
|
+
url='https://github.com/0lever/so',
|
|
10
|
+
|
|
11
|
+
author='fqiyou',
|
|
12
|
+
|
|
13
|
+
author_email='yc.fqiyou@gmail.com',
|
|
14
|
+
|
|
15
|
+
description=u'服务器登录工具',
|
|
16
|
+
|
|
17
|
+
install_requires=["pyyaml", "pexpect"],
|
|
18
|
+
|
|
19
|
+
packages=find_packages(),
|
|
20
|
+
|
|
21
|
+
long_description=open('README.rst').read(),
|
|
22
|
+
|
|
23
|
+
package_data={
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
entry_points={
|
|
27
|
+
'console_scripts': [
|
|
28
|
+
'so = so.so:run',
|
|
29
|
+
'so_install = so.so:run_install',
|
|
30
|
+
],
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# pip install build twine
|
|
36
|
+
# python -m build # 生成 dist/ 下的 sdist 和 wheel
|
|
37
|
+
# twine upload dist/* -r coohua
|
|
38
|
+
# twine upload dist/* -r pypi
|
|
39
|
+
# pip install --upgrade 0lever-so -i https://pypi.org/simple/
|
|
40
|
+
|
|
41
|
+
# python -m build && twine upload dist/* -r pypi
|
|
File without changes
|
0lever_so-1.9.1/so/so.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import sys
|
|
3
|
+
import pexpect
|
|
4
|
+
import signal
|
|
5
|
+
import yaml
|
|
6
|
+
import os
|
|
7
|
+
import shutil
|
|
8
|
+
import time
|
|
9
|
+
import termios
|
|
10
|
+
import struct
|
|
11
|
+
import fcntl
|
|
12
|
+
|
|
13
|
+
password_yaml = """ssh:
|
|
14
|
+
- id: 1
|
|
15
|
+
name: demo1
|
|
16
|
+
user: fqiyou
|
|
17
|
+
password: xxx
|
|
18
|
+
host: 1.1.1.1
|
|
19
|
+
port: 20755
|
|
20
|
+
- id: 2
|
|
21
|
+
name: demo2
|
|
22
|
+
user: fqiyou
|
|
23
|
+
password: xxx
|
|
24
|
+
host: 1.1.1.1
|
|
25
|
+
port: 39986
|
|
26
|
+
- id: 3
|
|
27
|
+
name: demo3
|
|
28
|
+
user: root
|
|
29
|
+
password: demo.pem
|
|
30
|
+
host: 1.1.1.1
|
|
31
|
+
port: 22
|
|
32
|
+
"""
|
|
33
|
+
keys_demo_pem = """-----BEGIN RSA PRIVATE KEY-----
|
|
34
|
+
xxxxx
|
|
35
|
+
-----END RSA PRIVATE KEY-----
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
child = None
|
|
39
|
+
termios_size = None
|
|
40
|
+
def _sigwinch_passthrough(sig, data):
|
|
41
|
+
global child,termios_size
|
|
42
|
+
if 'TIOCGWINSZ' in dir(termios):
|
|
43
|
+
TIOCGWINSZ = termios.TIOCGWINSZ
|
|
44
|
+
else:
|
|
45
|
+
TIOCGWINSZ = 1074295912
|
|
46
|
+
s = struct.pack("HHHH", 0, 0, 0, 0)
|
|
47
|
+
a = struct.unpack('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s))
|
|
48
|
+
termios_size = a
|
|
49
|
+
if child is not None:
|
|
50
|
+
child.setwinsize(a[0], a[1])
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _exit(*args, **kwargs):
|
|
54
|
+
print('You choose to stop so.')
|
|
55
|
+
if child is not None:
|
|
56
|
+
child.close()
|
|
57
|
+
sys.exit(1)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _get_hosts_by_config(name=''):
|
|
61
|
+
try:
|
|
62
|
+
so_dir = os.path.join(os.path.expanduser('~'), ".so")
|
|
63
|
+
password_yaml_file = os.path.join(so_dir, "password%s.yaml" % ('-' + name if name else ''))
|
|
64
|
+
keys_dir = os.path.join(so_dir, "keys")
|
|
65
|
+
return yaml.load(open(password_yaml_file, 'r').read(), Loader=yaml.Loader)["ssh"], keys_dir
|
|
66
|
+
except Exception as e:
|
|
67
|
+
print(u"获取配置文件错误", e)
|
|
68
|
+
exit(1)
|
|
69
|
+
return
|
|
70
|
+
|
|
71
|
+
def _login_ssh(user, password, host, port, oauth=False):
|
|
72
|
+
global child
|
|
73
|
+
if password.endswith('.pem'):
|
|
74
|
+
child = pexpect.spawn('ssh %s@%s -p %s -i %s' % (user, host, port, password))
|
|
75
|
+
elif password == '':
|
|
76
|
+
# 免密登陆,仅使用公钥认证
|
|
77
|
+
child = pexpect.spawn('ssh %s@%s -p %s -o BatchMode=yes -o PreferredAuthentications=publickey' % (user, host, port))
|
|
78
|
+
else:
|
|
79
|
+
child = pexpect.spawn('ssh %s@%s -p %s' % (user, host, port))
|
|
80
|
+
patterns = ['nodename nor servname provided', 'Connection refused'
|
|
81
|
+
, pexpect.TIMEOUT
|
|
82
|
+
, '[Pp]assword:'
|
|
83
|
+
, 'continue connecting (yes/no)?'
|
|
84
|
+
, '#', '~', 'Welcome']
|
|
85
|
+
if oauth:
|
|
86
|
+
patterns.append('请输入 OAuth 二次验证码')
|
|
87
|
+
i = child.expect(patterns)
|
|
88
|
+
if i <= 2:
|
|
89
|
+
print(child.before, child.after)
|
|
90
|
+
return
|
|
91
|
+
elif i == 3:
|
|
92
|
+
child.sendline(password)
|
|
93
|
+
elif i == 4:
|
|
94
|
+
child.sendline('yes')
|
|
95
|
+
if oauth:
|
|
96
|
+
import pyotp
|
|
97
|
+
key = str(pyotp.TOTP(password).now())
|
|
98
|
+
child.sendline(key)
|
|
99
|
+
else:
|
|
100
|
+
child.expect('[Pp]assword:')
|
|
101
|
+
child.sendline(password)
|
|
102
|
+
elif i in (5, 6, 7):
|
|
103
|
+
pass
|
|
104
|
+
elif oauth and i == 8:
|
|
105
|
+
import pyotp
|
|
106
|
+
key = str(pyotp.TOTP(password).now())
|
|
107
|
+
child.sendline(key)
|
|
108
|
+
else:
|
|
109
|
+
print(i)
|
|
110
|
+
print(child.before, child.after)
|
|
111
|
+
return
|
|
112
|
+
print('Login Success!')
|
|
113
|
+
child.sendline('')
|
|
114
|
+
_sigwinch_passthrough(None, None)
|
|
115
|
+
if termios_size is not None:
|
|
116
|
+
child.setwinsize(termios_size[0], termios_size[1])
|
|
117
|
+
child.interact()
|
|
118
|
+
|
|
119
|
+
def _print_head():
|
|
120
|
+
print("##############################################")
|
|
121
|
+
print("\033[0;36m SU Login Platform \033[0m")
|
|
122
|
+
print("##############################################")
|
|
123
|
+
|
|
124
|
+
def _print_underline():
|
|
125
|
+
print("----------------------------------------------")
|
|
126
|
+
|
|
127
|
+
def _print_remind():
|
|
128
|
+
print("[*] 选择主机:")
|
|
129
|
+
|
|
130
|
+
def _get_cmd_args():
|
|
131
|
+
remind = "[*] 选择主机:"
|
|
132
|
+
args = ""
|
|
133
|
+
if 2 == sys.version_info[0]:
|
|
134
|
+
args = raw_input(remind)
|
|
135
|
+
elif 3 == sys.version_info[0]:
|
|
136
|
+
args = input(remind)
|
|
137
|
+
else:
|
|
138
|
+
_print_remind()
|
|
139
|
+
args = sys.stdin.readline()
|
|
140
|
+
return str(args).replace("\n", "")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _print_host_list(host_list):
|
|
144
|
+
_print_underline()
|
|
145
|
+
print(" 序号 | 主机 | 说明 " )
|
|
146
|
+
_print_underline()
|
|
147
|
+
for i in host_list:
|
|
148
|
+
print("\033[0;31m%4s\033[0m\t| %15s | %s\t" % (i['id'], i['host'], i['name']))
|
|
149
|
+
_print_underline()
|
|
150
|
+
|
|
151
|
+
def _login(info, keys_dir):
|
|
152
|
+
user = info["user"]
|
|
153
|
+
password = info["password"]
|
|
154
|
+
host = info["host"]
|
|
155
|
+
port = info["port"]
|
|
156
|
+
if "key_type" in info.keys() and info["key_type"] == "OAuth":
|
|
157
|
+
_login_ssh(user=user, password=password, host=host, port=port, oauth=True)
|
|
158
|
+
else:
|
|
159
|
+
if password.endswith('.pem'):
|
|
160
|
+
password = os.path.join(keys_dir, password)
|
|
161
|
+
_login_ssh(user=user, password=password, host=host, port=port)
|
|
162
|
+
|
|
163
|
+
def run():
|
|
164
|
+
name = sys.argv[1] if len(sys.argv) > 1 else ''
|
|
165
|
+
config_host_list, key_dir = _get_hosts_by_config(name)
|
|
166
|
+
config_host_map = dict(zip([str(i["id"]) for i in config_host_list], config_host_list))
|
|
167
|
+
# 信号
|
|
168
|
+
signal.signal(signal.SIGINT, _exit)
|
|
169
|
+
signal.signal(signal.SIGTERM, _exit)
|
|
170
|
+
signal.signal(signal.SIGWINCH, _sigwinch_passthrough)
|
|
171
|
+
_print_head()
|
|
172
|
+
while True:
|
|
173
|
+
_print_host_list(config_host_list)
|
|
174
|
+
# _print_remind()
|
|
175
|
+
# cmd_args = sys.stdin.readline().replace("\n", "")
|
|
176
|
+
cmd_args = _get_cmd_args()
|
|
177
|
+
if cmd_args in config_host_map:
|
|
178
|
+
_login(config_host_map[cmd_args], key_dir)
|
|
179
|
+
elif cmd_args == "q" or cmd_args == "exit" or cmd_args == "quit":
|
|
180
|
+
_exit()
|
|
181
|
+
return
|
|
182
|
+
else:
|
|
183
|
+
print("未知参数:%s" % cmd_args)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def run_install():
|
|
187
|
+
so_dir = os.path.join(os.path.expanduser('~'), ".so")
|
|
188
|
+
if os.path.exists(so_dir):
|
|
189
|
+
shutil.move(so_dir, so_dir+"_" + str(int(time.time() * 1000)))
|
|
190
|
+
os.mkdir(so_dir)
|
|
191
|
+
os.mkdir(os.path.join(so_dir, "keys"))
|
|
192
|
+
with open(os.path.join(so_dir, 'password.yaml'), 'w') as f:
|
|
193
|
+
f.write(password_yaml)
|
|
194
|
+
with open(os.path.join(so_dir, "keys", 'demo.pem'), 'w') as f:
|
|
195
|
+
f.write(password_yaml)
|