◎软件环境:
◎功能描述:
工作中有时候需要同时登陆某个项目下多台服务器,同时查看多台机器状态,服务日志,批量操作等,往往要先打开终端一台一台登陆上去然后多窗口操作。通过此 workflow 只要选中多个服务器名称或IP,一键即可登录到多服务器。
◎实现思路:
选中多个服务器名称或IP,通过快捷键将服务器信息导入剪贴板,然后存到中转文件,Alfred workflow 会读取文件,通过 iterm 创建多个tab登陆到多个服务器。服务器信息会自动创建放在中转文件 ~/.hosts 里,可以修改 osascript 脚本实现个人需求。
on alfred_script(q)
-- 将剪贴板内的服务器地址写入中转文件~/.hosts
try
tell application "Finder"
set hostFile to POSIX path of (path to home folder as text) & ".hosts"
if not exists file hostFile then
make new file at Home with properties {name: ".hosts"}
end if
end tell
on error
-- ~/.hosts exists
-- display dialog "~/.hosts exists"
end try
do shell script "pbpaste > ~/.hosts"
-- 读取文件,创建多个tab登陆服务器
tell application "System Events"
-- some versions might identify as "iTerm2" instead of "iTerm"
set isRunning to (exists (processes where name is "iTerm")) or (exists (processes where name is "iTerm2"))
end tell
-- Get hosts
set listOfShows to {}
set Shows to paragraphs of (read POSIX file hostFile)
repeat with nextLine in Shows
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfShows
end if
end repeat
set num_hosts to count listOfShows
tell application "iTerm2"
activate
set hasNoWindows to ((count of windows) is 0)
if isRunning and hasNoWindows then
create window with default profile
end if
select first window
-- Create new tab
tell the first window
if isRunning and hasNoWindows is false then
create tab with default profile
end if
end tell
-- Prepare vertical panes
repeat with i from 1 to num_hosts
tell session i of current tab of current window
if i < num_hosts then
split vertically with profile "Default"
end if
end tell
end repeat
-- Login server, use write text can execute all command you want
repeat with n from 1 to num_hosts
tell session n of current tab of current window
-- 这里con脚本里实现登陆服务器的流程
write text "/usr/local/bin/con " & (item n of listOfShows)
end tell
end repeat
end tell
end alfred_scriptcon脚本:
#!/usr/bin/expect
#!/bin/bash
# 解决终端显示异常的问题
#trap sigwinch spawned
trap {
set rows [stty rows]
set cols [stty columns]
stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
# 记录执行日志
#log_file /tmp/login.log
# 0-关闭日志/1-打开日志
#log_user 1
# 开启执行时调试
#exp_internal 1
#### 用户信息 ####
set user "anzhihe"
set pass "passwd"
# 选择中控机器
set relay "relay.chegva.com"
set machine [lindex $argv 0]
# 启动连接进程
spawn ssh -o StrictHostKeyChecking=no -o LogLevel=quiet "$user@$relay"
set timeout 10
expect {
# 内部并行匹配
# "Googlekey shield code*" {
# # 输入googlekey
# expect_user -timeout -1 -re "(.*)\n"
# set code $expect_out(1,string)
# send "$code\n";
# exp_continue;
# }
# "Password*" {
# # 输入密码
# send "$pass\n";
# exp_continue;
# }
# "Option*" {
# # 选择中控
# send "$relay\r";
# exp_continue;
# }
"*$user\>" {
# 连接线上机器
send "$machine\r";
# exp_continue;
}
# "*bash*" {
# # 切换成root用户
# sleep 0.3
# send "sudo -iu root\r";
# }
}
# 串行匹配,解决可能程序没准备好,却已经返回的情况
expect {
"*bash*" {
# 切换成root用户
send "sudo -iu root\r";
}
}
interact使用方法:
Use HotKey: Select the servers name or IP,Press
Command + gUse keyword: First copy the name(or ip) of the servers,Then use alfred to enter the keyword
agAfter that, you can use
Command + Option + iorCommand + Shift + ito operate multiple servers
◎效果展示:
◎下载地址:https://github.com/anzhihe/Efficient-office/tree/master/login-multiple-servers
参考:

