JAvaはlinux,windows,3階層スイッチをリモートで操作します.シャットダウンと再起動
8260 ワード
最近、linux、windowsサーバ、3階層スイッチの再起動とシャットダウンがいくつか必要です.
Linux:
まず原理を言います:linuxに対してsshプロトコルを通じて、アカウントのパスワードは登録してからコマンドを使います.直接コードをつける.jsch-0.1.48.jarパッケージの使用
3階層スイッチ:思科を例に挙げます.
原理:三層スイッチはjavaシミュレーションtelnetを使用してログインし、コマンドを発行します.使用するパッケージcommons-net-3.3.jar
Windows:
Windowsの操作にはwmiを使用します.JAvaはvbsスクリプトを作成します.もちろん,パラメータはプログラムを介してスクリプトに伝達される.(リモートはまだテストに成功していません.後で追加します)
Linux:
まず原理を言います:linuxに対してsshプロトコルを通じて、アカウントのパスワードは登録してからコマンドを使います.直接コードをつける.jsch-0.1.48.jarパッケージの使用
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class ControllerLinux {
/**
* 2017-12-25 10:52:35
* cwy
*
*/
private static ChannelExec channelExec;
private static Session session = null;
private static int timeout = 60000;
//
public static void main(String[] args){
try{
versouSshUtil("192.168.10.234","root","38921561",22);
runCmd("init 1","UTF-8");
}catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param host ip
* @param userName
* @param password
* @param port
* @throws Exception
*/
public static void versouSshUtil(String host,String userName,String password,int port) throws Exception{
//log.info(" ....host:" + host + ",username:" + userName + ",password:" + password + ",port:"
//+ port);
JSch jsch = new JSch(); // JSch
session = jsch.getSession(userName, host, port); // , ip, Session
session.setPassword(password); //
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config); // Session properties
session.setTimeout(timeout); // timeout
session.connect(); // Session
}
/**
*
* @param cmd
* @param charset
* @throws Exception
*/
public static void runCmd(String cmd,String charset) throws Exception{
channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(cmd);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.connect();
InputStream in = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));
String buf = null;
while ((buf = reader.readLine()) != null){
System.out.println(buf);
}
reader.close();
channelExec.disconnect();
}
}
3階層スイッチ:思科を例に挙げます.
原理:三層スイッチはjavaシミュレーションtelnetを使用してログインし、コマンドを発行します.使用するパッケージcommons-net-3.3.jar
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;
public class TelnetConnection {
/**
* 2017-12-25 5:45:38
* cwy
*
*/
private TelnetClient telnet = null;
private String prompt = "#$>]";
private String loginPrompt = "login";
private String usernamePrompt = "User Name:";
private String passwordPrompt = "Password:";
private InputStream in;
private PrintStream out;
public TelnetConnection(String host, int port) {
if(telnet == null) {
telnet = new TelnetClient("vt200");
try {
telnet.setDefaultTimeout(5000);
telnet.connect("192.168.10.150",23); // , 23
this.in = telnet.getInputStream();
this.out = new PrintStream(telnet.getOutputStream());
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* : , , Username:, , setUsernamePrompt(prompt);
* , , Password:, , setPasswordPrompt(prompt);
* , : :%Apr 17 04:26:32:256 2000 Quidway SHELL/5/LOGIN:- 1 - admin(191.168.2.227) in unit1 login
* login, , setLoginPrompt(prompt)。
, setLoginPrompt(null);
* , , #、$、>、] , , setPrompt(prompt).
*/
public void login(String username, String password, String prompt) {
//
if(prompt != null && !"".equals(prompt)) {
this.prompt = prompt;
}
readUntil(this.usernamePrompt);
write(username);
readUntil(this.passwordPrompt);
write(password);
readUntil(this.prompt);
if(this.loginPrompt != null)
readUntil(this.loginPrompt);
}
/** * * * @param pattern * @return */
public String readUntil(String pattern) {
StringBuffer sb = new StringBuffer();
try {
int len = 0;
while((len = this.in.read()) != -1) {
sb.append((char)len);
if(pattern.indexOf((char)len) != -1 || sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
} catch (IOException e) {
}
return "";
}
/** * * * @param value */
public void write(String value) {
try {
out.println(value);
out.flush();
} catch (Exception e) {
}
}
/** * * * @param command * @return */
public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + "");
} catch (Exception e) {
}
return "";
}
/** * */
public void disconnect() {
try {
telnet.disconnect();
} catch (Exception e) {
}
}
/**
* @return the prompt
*/
public String getPrompt() {
return prompt;
}
/**
* @param prompt the prompt to set
*/
public void setPrompt(String prompt) {
this.prompt = prompt;
}
/**
* @return the usernamePrompt
*/
public String getUsernamePrompt() {
return usernamePrompt;
}
/**
* @param usernamePrompt the usernamePrompt to set
*/
public void setUsernamePrompt(String usernamePrompt) {
this.usernamePrompt = usernamePrompt;
}
/**
* @return the passwordPrompt
*/
public String getPasswordPrompt() {
return passwordPrompt;
}
/**
* @param passwordPrompt the passwordPrompt to set
*/
public void setPasswordPrompt(String passwordPrompt) {
this.passwordPrompt = passwordPrompt;
}
/**
* @return the loginPrompt
*/
public String getLoginPrompt() {
return loginPrompt;
}
/**
* @param loginPrompt the loginPrompt to set
*/
public void setLoginPrompt(String loginPrompt) {
this.loginPrompt = loginPrompt;
}
/**
*
* @param telnet
*/
public void close(TelnetClient telnet) {
if(telnet != null) {
try {
telnet.disconnect();
} catch (IOException e) {
}
}
if(this.telnet != null) {
try {
this.telnet.disconnect();
} catch (IOException e) {
}
}
}
public static void main(String[] args) {
try {
System.out.println(" Telnet...");
String ip = "191.168.10.150";
int port = 23;
String user = "gzyingling";
String password = "gzyingling@2017";
TelnetConnection telnet = new TelnetConnection(ip, port);
telnet.login(user, password, "");
String r1 = telnet.sendCommand("reload");//display snmp-agent local-engineid
String r2 = telnet.sendCommand("Y");
System.out.println(r1);
System.out.println(r2);
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Windows:
Windowsの操作にはwmiを使用します.JAvaはvbsスクリプトを作成します.もちろん,パラメータはプログラムを介してスクリプトに伝達される.(リモートはまだテストに成功していません.後で追加します)