依賴public
abstract
class
shell
implements
autocloseable
class ssh2shell extends shell;
public
static string errormsg = new string ;
/*** 利用jsch包實現遠端主機shell命令執行, 鏈結遠端主機
* *@param ip 主機ip
*@param user 主機登陸使用者名稱
*@param psw 主機登陸密碼
*@param port 主機ssh2登陸埠,如果取預設值,傳-1
*@param privatekey 金鑰檔案路徑
*@param passphrase 金鑰的密碼
*/ssh2shell(string ip, string user, string psw, int port, string privatekey, string passphrase)
/*** 利用jsch包實現遠端主機shell命令執行, 鏈結遠端主機 獲得expect4j物件,該對用可以往ssh傳送命令請求
* *@param ip 主機ip
*@param user 主機登陸使用者名稱
*@param psw 主機登陸密碼
*@param port 主機ssh2登陸埠,如果取預設值,傳-1
*@param privatekey 金鑰檔案路徑
*@param passphrase 金鑰的密碼
*/void connect(string ip, string user, string psw, int port, string privatekey, string passphrase) catch (exception ex)
}/**
* 執行配置命令
* *@param commands 要執行的命令,為字元陣列
*@return 執行是否成功
*/boolean executecommands(string... commands)
if (log.isdebugenabled())
try
// 防止最後乙個命令執行不了
issuccess = !checkresult(expect.expect(lstpattern));
// 找不到錯誤資訊標示成功
string response = buffer.tostring().tolowercase();
for (string msg : errormsg)
}return issuccess;
} catch (exception ex)
}/**
* 關閉ssh遠端連線
*/protected
void
disconnect()
if (channel != null)
if (session != null)
log.info("---------- disconnect ssh ----------");
}@override
public
void
close()
}/**
* 獲取伺服器返回的資訊
* *@return 服務端的執行結果
*/public string getresponse()
private
void
expect() throws jschexception, ioexception
private
void
session(string ip, string user, string psw, int port) throws jschexception else
if (session == null)
session.setpassword(psw);
hashtableconfig = new hashtable();
config.put("stricthostkeychecking", "no");
session.setconfig(config);
localuserinfo ui = new localuserinfo();
session.setuserinfo(ui);
session.connect();
}/**
* 設定ssh 免密登陸
* *@param jsch
*@param privatekey
*@param passphrase
*@throws jschexception
*/private
void
addidentity(string privatekey, string passphrase) throws jschexception
if (passphrase != null && "".equals(passphrase)) else
}/**
*@return
*@throws malformedpatternexception
*/private listlstpattern() throws malformedpatternexception );
lstpattern.add(mat);
}lstpattern.add(new eofmatch(x -> ));
lstpattern.add(new timeoutmatch(default_time_out, x -> ));}}
return lstpattern;
}// 檢查執行是否成功
private
boolean
executecommand(listobjpattern, string strcommandpattern)
return
false;
} catch (malformedpatternexception ex) catch (exception ex)
}// 檢查執行返回的狀態
private
boolean
checkresult(int intretval)
return
false;
}// 登入ssh時的控制資訊
// 設定不提示輸入密碼、不顯示登入資訊等
private
static
class
localuserinfo
implements
userinfo
public
boolean
promptyesno(string str)
public string getpassphrase()
public
boolean
promptpassphrase(string message)
public
boolean
promptpassword(string message)
public
void
showmessage(string message)
}}
com.github.cverges.expect4jgroupid>
expect4jartifactid>
1.6version>
dependency>
com.jcraftgroupid>
jschartifactid>
0.1.54version>
dependency>
通過CGI實現在Web頁面上執行shell命令
使用命令安裝 yum install httpd命令安裝 安裝成功我們可以看到在 var 目錄下會產生乙個www的目錄,該目錄下還包含 cgi bin html 連個目錄 cgi bin目錄下主要存放cgi檔案 html目錄下主要存放html網頁檔案 此時可啟動httpd服務,檢視啟動後的狀態 伺服...
Android initrc中執行shell指令碼
由於initrc中支援的命令有限 不能支援system bin下所有命令 而且不適合寫比較複雜的邏輯 如傳參 函式塊 通常把這些功能放在shell指令碼裡來執行,但是shell指令碼又需要放在initrc中來呼叫執行。因為隨著android版本更新,selinux的限制越來越多,在initrc中執行...
Golang通過SSH執行交換機操作
golang通過ssh執行交換機操作 簡單實現通過輸入指令,兩步執行交換機命令。存在問題 不過我的目的已經達到,我主要是了解ssh的使用。package main import bufio fmt golang.org x crypto ssh log os strings sync 獲取賬號和密碼...