公司裡頭有「禁用root登入」政策,須先以個人帳號登入,才能su到特定帳號。不過,對於sa而言,能夠從root開始su到各帳號,是最方便的,理由很簡單,這樣就不必去記各帳號的密碼了。
不過,流程就會變得相當繁瑣,首先得登入自己的帳號,敲密碼;緊接著su成root,再敲密碼,最後su成特定user。
而通常sa不會只管一部機器,所以,上述流程得重複數次,端看較常管理的機器有幾部。
這很麻煩,尤其遇上緊急事件時,還得浪費時間在敲這一連串密碼,常常暗嘆豈無良方?所以,趁興頭上,改寫了Poderosa內附的telnet.js,加上迴圈以及額外步驟,並且設定在開啟Poderosa時直接執行這個macro。
測試後,哇靠,數秒鐘內就完成連線到10部機器,並且都依序登入personal->root->specific,唉,早這麼做的話,以前就不會這麼累啦。
程式碼附在下面。
import Poderosa;
import Poderosa.Macro;
import Poderosa.ConnectionParam;
import Poderosa.Terminal;
import System.Drawing;
var env = new Environment();
/*
Please modify the following values before you run this macro!
*/
var host = ["host1", "host2", "host3", "host4", "host5"];
var user1 = ["apache","apache","apache","apachelin","apache"];
var user2 = ["account1","account2","account3","account4","account5"];
var user1_pass = "i_love_you";
var root_pass = "god_love_you";
var i : int = 0;
for (i = 0; i < host.length; i++) {
telnet_as_normal_user(host[i], user1[i]);
su_to_root();
su_to_specific(user2[i]);
}
function telnet_as_normal_user(host_Name, login_user) {
var param = new TelnetTerminalParam(host_Name);
var prof = new RenderProfile();
prof.FontSize = 10;
prof.SetBackColor(Color.Black);
prof.SetForeColor(Color.White);
param.RenderProfile = prof;
var c = env.Connections.Open(param);
var r = c.ReceiveData();
while(r.indexOf("login:")==-1) r =c.ReceiveData(); //waiting prompt for account
c.TransmitLn(login_user);
r = c.ReceiveData();
while(r.indexOf("Password:")==-1) r =c.ReceiveData(); //waiting prompt for password
c.TransmitLn(user1_pass);
}
function su_to_root() {
var con = env.Connections.ActiveConnection;
con.TransmitLn("su -")
var r = con.ReceiveData();
while(r.indexOf("Password:")==-1) r = con.ReceiveData(); //waiting prompt for account
con.TransmitLn(root_pass)
}
function su_to_specific(target_user) {
var con = env.Connections.ActiveConnection;
con.TransmitLn("su - " + target_user)
}
沒有留言:
張貼留言