% E4 % b8 % 80 % E3 % 80 % 81 % E7 % 9b % AE % E7 % 9a % 84 % 0a % E3 % 83 % BC % E3 % 83 % 89 % E3 % 83 % 83 % 9e % 81 % ab % E3 % 83 % ad % E3 % 82 % B0 % E3 % 82 % a4 % E3 % 83 % B3 % E3 % 81 % E3 % 82 % 8b % E3 % 81 % AE % E3 % 81 % ad % E3 % 81 % 81 % ad 3 % 83 % BC % E3 % 83 % 90 % E3 % 83 % BC % E3 % 81 % ab % E3 % 83 % 83 % ad % E3 % 82 % B0 % E3 % 82 % a4 % E3 % 83 % B3 % E3 % 81 % E6 % 93 % 8d % E4 % BD % 9c % E3 % 81 % a7 % E3 % 80 % 80 % 81 % E6 % af % 8e % E5 % 9b % 9e % E6 % 89 % 8b % E5 % 95 % E3 % % A8 % e9 % 9d % 9e % E5 % 8a % B9 % E7 % 8e % 87 % E3 % 81 % a7 % E7 % 85 % a9 % e9 % 9b % 91 % E3 % 81 % a7 % E3 % 81 % 99 % 83 % ad % E3 % 82 % B0 % E3 % 82 % a4 % E3 % 83 % B3 % E3 % 81 % 9f % 97 % E3 % E5 % be % 8c % E3 % 80 % 81 % E8 % 87 % ab 5 % AE % 9f % E8 % a1 % 8c % E3 % 81 % 97 % E3 % 81 % a6 % E3 % 80 % 81 % E8 % 87 % aa % E5 % 88 % 86 % E3 % 81 % 8c % E3 % 83 % 83 % E3 % 81 % a6 % E3 % 81 % 84 % E3 % 82 % 8b % E3 % 82 % B7 % E3 % 83 % 86 % B9 % E3 % 83 % A0 % E3 % 82 % 92 % E8 % 92 % e9 % 81 % b8 % E6 % 8a % 9e % E3 % 81 % 99 % E3 % 82 % 8b % E3 % 81 % A8 % E3 % 80 % 81 % E3 % 81 % 9d % E3 % 81 % AE % AE % 81 % ee % 81 % AE 1 % AE % E3 % 83 % 83 % E3 % 83 % E3 % 89ip % E3 % 81 % 8c % E3 % 83 % aa % E3 % 82 % B9 % E3 % 83 % 88 % E3 % 81 % 8 % E8 % 87 % aa % E5 % 8b % 95 % E7 % 9a % 84 % E3 % 81 % ab % E5 % af % E5 % BF % E3 % 81 % 99 % E3 % 82 % 8b % E3 % 99 % E3 % 82 % 8b % E3 % 81 % AE % E3 % 81 % 8c % E4 % be % E5 % 88 % a9 % E3 % 81 % E3 % 81 % 99 % E3 % 80 % 82 % E3 % 81 % 93 % E3 % 81 % AE % E8 % A8 % 98 % E4 % ba % 8b % E3 % 81 % AE python % E3 % 82 % B9 % E3 % 82 % af % E3 % 83 % aa % 3 % 81 % 9f % E3 % 80 % 82 % 0a % E4 % ba % 8c % E3 % 80 % 81 % E3 % 83 % BC % E3 % 83 % 89 % E5 % AE % 9f % E6 % E3 % 82 % a4 % E3 % 83 % ab % E5 % 90 % 8d % 3a smart _ ssh % E3 % 80 % 81 % E5 % AE % E8 % a1 % 8c % E7 % 92 % B0 % E5 aa % E5 % a4 % 9c % 2f python % 0a from % 20 optparse % 20 import % 20 option parser % 0a import % 20 p expect % 0a import % 20 getpass % _ server % ef % BC % 88ip % ef % BC % 89: % 0a if % ef % BC % 88ip % 3d % 3d none % ef % BC % 89: % 0a return % 20 none % ef % BC % 89 % ef % BC % 89 % E3 % 80 % 82 % 0a child % 3dp expect.spawn % ef % BC % 88 ssh _ cmd % ef % BC % 89 % ef % BC % 88 BC % 88 username % ef % BC % 8cp art _ app _ name % ef % BC % 89: % 0a try : % 0a URL % 3d % 27http://localhost :8080 % 27 % 20 % 25 % 20 % 28用户名称% ef % BC %8CP art _ app _ name % 29 % 0a if % 20 part _ app _ name % 20 % 3d % 20 none 3330 % 20 username % 3d % 25s % 27 % 20 % 25 % 20 % 28 username % 29 % 0a resp % 20 % 3d % 20 requests.get % 28 URL % 29 % 0a if % ef int % ef % BC % 88 requestapplistfromcmdbfail % ef % BC % 81 % 27 % 20 % ef % BC % 89 % 0a返回% 20 none % 0a resp _ content % 27 success % 27 % 5d % 3d % 3d % 27 true % 27 % ef % BC % 89 % ef % BC % 89: % 0a result % 20 % 3d % 20 resp _ content % 5b % 5b estapplistfromcmdbfail % ef % BC % 81 % 27 % 20 % ef % BC % 89 % 0a return % 20 none % 0a except : % 0a打印% ef % BC % 88 cmdd % 20 none %0ade flist _ authorized _ IPS % ef % BC % 88 username % ef % BC %8CAPP _ name % ef % BC % 893: % 0a if % 20 app 3d % 27http://localhost :8080 %2FCM db % 2f list-IP % ef % BC % 9f % 20 username % 3d % 25s artifact _ id % 3d % 25s % 25s % 27 % 27 . get % 28 URL % 29 % 0a if % ef % BC % 88 resp.status _ code % ef % BC % 81 % 20 % 3d % 20200 % ef % BC % 89: % 0a print % 0a return % 20 none % 0a resp _ content % 3d JSON.loads % 28 resp.text % 29 % 0a if % ef % BC % 88 resp _ content % 5b % 27 succcc % 20 % 3d % 20 resp _ content % 5b % 27 data % 27 % 5d % 0a return % 20 result % 0a else : % 0a print % ef % BC % 88 requestiplistiplist a except : % 0a print % ef % BC % 88 cmdbserviceisnotavailable % ef % BC % 81 % 22 % 20 % ef % BC % 89 % 0a return % 20 none %0none % 0 _ list % ef % BC % 89: % 0a if % ef % BC % 88 app _ name _ list % 3d % 3d none % ef % BC % 893360 % 0a return % 20 none % 0000 rt _ app _ name % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % 89 % ef % BC % 89 % ef % BC % 89 % 89 % ef % BC c % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % 89 % ef % BC % 89
ase select app (0...%d) : '%(index-1)try:
app_index = input(show_msg)
if(app_index < 0 or app_index > index):
print("you input wrong number ! ")
return None
selected_app = app_name_list[app_index]
return selected_app
except:
print("you input wrong number ! ")
return None
def show_ip_list(app_name, app_ip_list):
if(app_ip_list == None):
return None
print("all ips of the application %s list below" %(app_name))
index = 0
for app_ip in app_ip_list:
index = index + 1
print(" %d : %s " %(index ,app_ip))
show_msg = 'please select ip (1...%d) : '%index
try:
id_index = input(show_msg)
if(id_index <= 0 or id_index > index):
print("you input wrong number ! ")
return None
selected_ip = app_ip_list[id_index-1]
return selected_ip
except:
print("you input wrong number ! ")
return None
def main():
parser = OptionParser()
parser.add_option('-a', '--app', dest='app', action='store', type='string', help='the application id in the auv')
(options, args) = parser.parse_args()
username = getpass.getuser()
app_name_list = list_authorized_apps(username, options.app)
if app_name_list == None:
exit()
elif (len(app_name_list) == 0 and options.app == None):
print("you are not authorized to any app ,use jumper directly")
exit()
elif len(app_name_list) == 0 :
print("no app name like %s, use jumper directly" %(options.app))
exit()
elif (len(app_name_list) == 1 and app_name_list[0] == options.app):
app_ip_list = list_authorized_ips(username, options.app)
selected_ip = show_ip_list(options.app, app_ip_list)
connect_to_server(selected_ip)
else :
jumper_name = 'no thanks ,I will use jumper'
app_name_list.insert(0,jumper_name)
selected_app = show_app_list(options.app, app_name_list)
if(selected_app == jumper_name) :
exit()
app_ip_list = list_authorized_ips(username, selected_app)
selected_ip = show_ip_list(selected_app, app_ip_list)
connect_to_server(selected_ip)
exit()
if __name__== '__main__':
main()
2.2、mock的cmdb接口代码
@RestController
@RequestMapping("cmdb")
public class CmdbController {
@GetMapping("list-app")
public String listApp(@RequestParam(value = "username") String username, @RequestParam(value = "app", required = false) String app) {
Result result = new Result("true");
List partApps = new ArrayList<>();
partApps.add("axx-app");
partApps.add("bxx-app");
partApps.add("cxx-app");
result.setData(partApps);
return new Gson().toJson(result);
}
@GetMapping("list-ip")
public String listIp(@RequestParam(value = "username") String username, @RequestParam(value = "artifact_id", required = false) String artifact_id) {
Result result = new Result("true");
List ips = new ArrayList<>();
ips.add("192.168.0.101");
ips.add("192.168.0.102");
ips.add("192.168.0.103");
result.setData(ips);
return new Gson().toJson(result);
}
}
2.3、原理
程序会和cmdb交互两次,第一次交互去获取有登录权限的应用列表或者部分应用列表;第二次会去获取指定应用的ip列表,当用户选择了指定ip后,就会使用pexpect连接到对应的机器。
2.3、系统命令制作
将文件smart_ssh放到/usr/local/聪慧的黑夜下,然后suorce一下/etc/profile,在命令行就可以运行smart_ssh了
2.4、用法
2.4.1、不带参数
直接使用smart_ssh时,会列举出当前登录用户所有有登录权限的应用列表,用户选择一个应用后,会列举出该应用的所有ip,用户选择一个ip后,程序就会自动登录到对应的机器。
2.4.2、带参数,模糊匹配
使用smart_ssh -a part_app_name,会列举出当前登录用户所有有登录权限且应用名模糊匹配part_app_name的应用列表,用户选择一个应用后,会列举出该应用的所有ip,用户选择一个ip后,程序就会自动登录到对应的机器。
2.4.3、完全匹配
使用smart_ssh -a app_name,会列举出当前登录用户所有有登录权限且应用名匹配app_name的应用列表进行校验,然后会直接列举出应用的所有ip,用户选择一个ip后,程序就会自动登录到对应的机器。
2.4、运行效果
三、感想
1、工作中,有很多可以挺高工作效率的方法,其中完备的工具就是其中之一。
2、很多事情,不是看到了希望才去做,而是做了才有希望。