本文整理汇总了Java中ch.ethz.ssh2.Connection类的典型用法代码示例。如果您正苦于以下问题:Java Connection类的具体用法?Java Connection怎么用?Java Connection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Connection类属于ch.ethz.ssh2包,在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConn
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
/**
* 获取服务器链接
* @param hostname
* @param username
* @param password
* @return
*/
public static Connection getConn(String hostName,String userName,int sshPort,String passWord){
try {
Connection conn = new Connection(hostName,sshPort);
//连接到主机
conn.connect();
//使用用户名和密码校验
boolean isconn = conn.authenticateWithPassword(userName, passWord);
if(!isconn){
System.out.println("用户名称或者是密码不正确");
}else{
return conn;
}
} catch (IOException e) {
System.out.println("获取服务器链接出现异常:"+e.toString());
return null;
}
return null;
}
示例2: getConnection
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
public Connection getConnection(){
int round = 0;
while(round<maxRoundConnection){
try{
connection = new Connection(server);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed...");
}
break;
}catch (Exception e) {
e.printStackTrace();
connection.close();
connection = null;
System.err.println("================= connection is null in round "+round);
try {
Thread.sleep(sshReconnectWatingtime*1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
round++;
}
return connection;
}
示例3: getConnection
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
public Connection getConnection(){
int round = 0;
while(round<maxRoundConnection){
try{
connection = new Connection(server);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed...");
}
break;
}catch (Exception e) {
e.printStackTrace();
connection.close();
connection = null;
System.err.println("================= connection is null in round "+round);
try {
Thread.sleep(sshReconnectWatingtime*1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
round++;
}
return connection;
}
示例4: getProcessState
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
/**
* 获取进程使用状态
* @param conn
* @param pid 进程ID
* @return
*/
public static ProcessState getProcessState(Connection conn,String pid){
String processStr = CtrCommond.doCommond(conn, LinuxCmd.dd.replace("{pid}", pid));
processStr = conProcessStr(processStr,pid);
String[] appStateStr = null;
if(!StringUtils.isEmpty(processStr)){
appStateStr = processStr.split(StaticKeys.SPLIT_KG);
if(appStateStr.length>1){
ProcessState processState = new ProcessState();
processState.setCpuPer(appStateStr[0]);
processState.setMemPer(appStateStr[1]);
return processState;
}
}
return null;
}
示例5: main
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = CtrCommond.getConn("192.168.1.1", "root", 22, "123456");
System.out.println("CPU型号信息:"+HostUtils.getCpuModel(conn));
System.out.println("物理CPU个数:"+HostUtils.getCpuNum(conn));
System.out.println("每个CPU核数量:"+HostUtils.getCpuPerCores(conn));
System.out.println("系统运行天数:"+HostUtils.getSystemDays(conn));
System.out.println("系统版本信息:"+HostUtils.getSystemRelease(conn));
System.out.println("系统版本详细信息:"+HostUtils.getSystemUname(conn));
System.out.println("cpu Idle使用率:"+HostUtils.getCpuState(conn).getIdle());
System.out.println("磁盘已使用G:"+HostUtils.getDfInfo(conn).getUsed());
System.out.println("磁盘IO状态:"+HostUtils.getDiskIoState(conn).getRkBS());
System.out.println("内存已使用百分比:"+HostUtils.getMemState(conn).getUsePer());
System.out.println("网络吞吐率rxbyt:"+HostUtils.getNetIoState(conn).getRxbyt());
System.out.println("系统一分钟负载:"+HostUtils.getSysLoadState(conn).getOneLoad());
System.out.println("系统TCP active:"+HostUtils.getTcpState(conn).getActive());
//System.out.println("进程2696内存使用率:"+HostUtils.getProcessState(conn, "2696").getMemPer());
System.out.println("系统已加载内核模块:"+HostUtils.getLsmodInfo(conn));
System.out.println("系统密码文件修改时间:"+HostUtils.getPasswdFileInfo(conn));
System.out.println("系统rpc服务开放状态:"+HostUtils.getRpcinfo(conn));
System.out.println("当前系统任务计划:"+HostUtils.getCrontab(conn));
}
示例6: SSHGetStats
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
public SSHGetStats(Properties ini) {
this.hostname = ini.getProperty("sshhost");
this.username = ini.getProperty("sshuser");
this.password = ini.getProperty("sshpass");
try {
/* Create a connection instance */
conn = new Connection(hostname);
/* Now connect */
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username,
password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
} catch (Exception e) {
e.printStackTrace();
}
diskStatsOldValue = getDiskStats();
getPercentageDiskIOSinceLastCall();
getPercentageCPUSinceLastCall();
getDifferentialDiskStats();
}
示例7: RemoteUtility
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
public RemoteUtility(
String hostName,
String userName,
String userPassword,
MessageConsoleStream consoleStream) {
// Setting attributes
setHostName(hostName);
setUserName(userName);
setUserPassword(userPassword);
setConsoleStream(consoleStream);
setConnection(new Connection(getHostName()));
setVerboseMode(false);
}
示例8: SSHConnection
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
/**
* SSHConnection authenticate using a user and and a public key
* @param hostname The host to connect to
* @param username The user name
* @param keyfile The certificate public key file
* @throws IOException
*/
public SSHConnection(String hostname, String username, File keyfile) throws IOException {
String keyfilePass = ""; // will be ignored if not needed
/* Create a connection instance */
connection = new Connection(hostname);
/* Now connect */
connection.connect();
/* Authenticate */
boolean isAuthenticated = connection.authenticateWithPublicKey(username, keyfile, keyfilePass);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
}
示例9: getConnection
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
public Connection getConnection(){
try{
connection = new Connection(server);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed...");
}
}catch (IOException e) {
e.printStackTrace();
}
return connection;
}
示例10: getConnection
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
public Connection getConnection() {
try {
connection = new Connection(server);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(
username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed...");
}
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
示例11: getConnection
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
private Connection getConnection() {
try {
connection = new Connection(server);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(
username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed...");
}
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
示例12: getCpuPerCores
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
/**
* 获取每个CPU核数量
* @param conn
* @return
*/
public static String getCpuPerCores(Connection conn){
String str = CtrCommond.doCommond(conn, LinuxCmd.WULI_CPU_CORE_NUM);
if(!StringUtils.isEmpty(str)){
return str.substring(str.length()-1);
}
return "";
}
示例13: getCpuModel
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
/**
* 获取CPU型号信息
* @param conn
* @return
*/
public static String getCpuModel(Connection conn){
String result = CtrCommond.doCommond(conn, LinuxCmd.CPU_XINGHAO);
if(!StringUtils.isEmpty(result)){
return result.trim();
}else{
return "";
}
}
示例14: ClientServerHello
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
public ClientServerHello(InputStream bi, OutputStream bo) throws IOException {
client_line = "SSH-2.0-" + Connection.identification;
bo.write((client_line + "\r\n").getBytes());
bo.flush();
byte[] serverVersion = new byte[512];
for (int i = 0; i < 50; i++) {
int len = readLineRN(bi, serverVersion);
server_line = new String(serverVersion, 0, len);
if (server_line.startsWith("SSH-"))
break;
}
if (server_line.startsWith("SSH-") == false)
throw new IOException(
"Malformed server identification string. There was no line starting with 'SSH-' amongst the first 50 lines.");
if (server_line.startsWith("SSH-1.99-"))
server_versioncomment = server_line.substring(9);
else if (server_line.startsWith("SSH-2.0-"))
server_versioncomment = server_line.substring(8);
else
throw new IOException("Server uses incompatible protocol, it is not SSH-2 compatible.");
}
示例15: execute
import ch.ethz.ssh2.Connection; //导入依赖的package包/类
/**
* 通过回调执行命令
* @param ip
* @param port
* @param username
* @param password
* @param callback 可以使用Session执行多个命令
* @throws SSHException
*/
public Result execute(String ip, int port, String username, String password,
SSHCallback callback) throws SSHException{
Connection conn = null;
try {
conn = getConnection(ip, port, username, password);
return callback.call(new SSHSession(conn, ip+":"+port));
} catch (Exception e) {
throw new SSHException("SSH err: " + e.getMessage(), e);
} finally {
close(conn);
}
}