Skip to content

Commit ee7acde

Browse files
authored
Create ssh-generator
1 parent 8d48951 commit ee7acde

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

ssh-generator

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/python3
2+
#coding=utf-8
3+
4+
import sys, os, uuid
5+
6+
def help():
7+
print('\n1. 首先创建ssh自动连接expect脚本: ssh-generator [hostname] [ip] [username] [password] [port]\n')
8+
print('2. 然后创建打开连接shell: ssh-generator [hostname]\n')
9+
print('# 如果您想同时打开多个ssh连接,那么您可以使用相同的 [hostname] 创建连接脚本,当您运行连接脚本时,将同时打开相同 [hostname] 的远程连接\n')
10+
sys.exit()
11+
12+
if __name__ == '__main__':
13+
USER_DIR = os.environ['HOME']
14+
name = ''
15+
if (len(sys.argv) < 2):
16+
help()
17+
else:
18+
name = sys.argv[1]
19+
if name.startswith('-'):
20+
help()
21+
22+
if (os.path.isdir(USER_DIR + '/host/') is False): os.makedirs(USER_DIR + '/host/')
23+
24+
if (len(sys.argv) <= 2):
25+
show = '#!/bin/sh\n'
26+
try:
27+
files = os.listdir(USER_DIR + '/host/' + name)
28+
except:
29+
print('\n\033[1;31;40m没有找到目录 [%s] 请确认您是否已生成expect脚本!\033[0m\n' % (USER_DIR + '/host/' + name))
30+
sys.exit()
31+
for filename in files:
32+
show += 'gnome-terminal -x bash -c \"%s\"\n' % ('~/host/' + name + '/' + filename)
33+
with open(os.path.join(USER_DIR + '/host', name + '.show'), 'wt') as f:
34+
f.write(show)
35+
os.system('chmod a+x ' + os.path.join(USER_DIR + '/host', name + '.show'))
36+
print('\n\033[1;32;40m 脚本创建成功,执行命令 `~/host/%s.show` 自动ssh登录\033[0m\n' % name)
37+
sys.exit()
38+
39+
ip = sys.argv[2]
40+
user = sys.argv[3]
41+
pwd = sys.argv[4]
42+
port = sys.argv[5] if len(sys.argv )>= 6 else '22'
43+
44+
45+
if (os.path.isdir(USER_DIR + '/host/' + name) is False): os.makedirs(USER_DIR + '/host/' + name)
46+
47+
exp = '#!/usr/bin/expect\nset timeout 20\nspawn sudo ssh %s@%s -p %s\nexpect { \n\"*yes/no\" { send \"yes\\r\"; exp_continue}\n\"*password:\" { send \"%s\\r\"}\n}\ninteract' % (user, ip, port, pwd)
48+
filename = name + '.exp.' + str(uuid.uuid1())
49+
50+
with open(os.path.join(USER_DIR + '/host', name, filename), 'wt') as f:
51+
f.write(exp)
52+
os.system('chmod a+x ' + os.path.join(USER_DIR + '/host', name, filename))
53+
54+
print('\n\033[1;32;40m 创建expect脚本成功,请继续执行命令 `ssh-generator %s`\033[0m\n' % name)

0 commit comments

Comments
 (0)