java访问远端Linux及执行远端Linux命令
来源:互联网 发布:男生文具盒淘宝 编辑:程序博客网 时间:2024/04/29 19:06
package util;
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
import java.util.List;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.sshd.ClientChannel;
import org.apache.sshd.ClientSession;
import org.apache.sshd.SshClient;
import org.apache.sshd.client.channel.ChannelShell;
import org.apache.sshd.common.util.NoCloseInputStream;
import org.apache.sshd.common.util.NoCloseOutputStream;
public class SshClient3 {
public static void main(String[] args) throws Exception {
int port = 22;
String host = "192.168.2.4";
String login = System.getProperty("user.name");
boolean agentForward = false;
List<String> command = null;
SshClient client = SshClient.setUpDefaultClient();
String str= "cat /usr/local/tomcat/webapps/studyApp/WEB-INF/db.properties;\r exit 0;\n";
ByteArrayInputStream in ;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
PrintStream out = new PrintStream( byteOut );
client.start();
try {
boolean hasKeys = false;
ClientSession session = client.connect(host, port).await().getSession();
int ret = ClientSession.WAIT_AUTH;
while ((ret & ClientSession.WAIT_AUTH) != 0) {
if (hasKeys) {
session.authAgent(login);
ret = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
} else {
String password ="loginpassword";
session.authPassword(login, password);
ret = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
}
}
if ((ret & ClientSession.CLOSED) != 0) {
System.err.println("error");
System.exit(-1);
}
ClientChannel channel = null;
if (command == null) {
channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
((ChannelShell) channel).setAgentForwarding(agentForward);
in= new ByteArrayInputStream( str.getBytes());
channel.setIn(new NoCloseInputStream( in ) );
}
channel.setOut(new NoCloseOutputStream( out));
channel.setErr(new NoCloseOutputStream(System.err));
channel.open().await();
channel.waitFor(ClientChannel.CLOSED, 0);
session.close(false);
} finally {
System.out.println( new String( byteOut.toByteArray()) );
client.stop();
}
}
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
import java.util.List;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.sshd.ClientChannel;
import org.apache.sshd.ClientSession;
import org.apache.sshd.SshClient;
import org.apache.sshd.client.channel.ChannelShell;
import org.apache.sshd.common.util.NoCloseInputStream;
import org.apache.sshd.common.util.NoCloseOutputStream;
public class SshClient3 {
public static void main(String[] args) throws Exception {
int port = 22;
String host = "192.168.2.4";
String login = System.getProperty("user.name");
boolean agentForward = false;
List<String> command = null;
SshClient client = SshClient.setUpDefaultClient();
String str= "cat /usr/local/tomcat/webapps/studyApp/WEB-INF/db.properties;\r exit 0;\n";
ByteArrayInputStream in ;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
PrintStream out = new PrintStream( byteOut );
client.start();
try {
boolean hasKeys = false;
ClientSession session = client.connect(host, port).await().getSession();
int ret = ClientSession.WAIT_AUTH;
while ((ret & ClientSession.WAIT_AUTH) != 0) {
if (hasKeys) {
session.authAgent(login);
ret = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
} else {
String password ="loginpassword";
session.authPassword(login, password);
ret = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
}
}
if ((ret & ClientSession.CLOSED) != 0) {
System.err.println("error");
System.exit(-1);
}
ClientChannel channel = null;
if (command == null) {
channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
((ChannelShell) channel).setAgentForwarding(agentForward);
in= new ByteArrayInputStream( str.getBytes());
channel.setIn(new NoCloseInputStream( in ) );
}
channel.setOut(new NoCloseOutputStream( out));
channel.setErr(new NoCloseOutputStream(System.err));
channel.open().await();
channel.waitFor(ClientChannel.CLOSED, 0);
session.close(false);
} finally {
System.out.println( new String( byteOut.toByteArray()) );
client.stop();
}
}
}
pom.xml
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.8.0</version>
</dependency>
- java访问远端Linux及执行远端Linux命令
- ssh到远端执行命令
- Linux下安装Redis以及配置远端访问
- FileZilla远端登录虚拟机Linux
- 通过ssh 执行远端服务器命令
- Embedded Linux下Java Socket迅速判断远端网络断开
- Shell下通过echo+telnet在远端执行命令
- java FTP 远端 分布式
- springmvc restTemplate访问远端工程
- 关于用java搭建FTP服务器和远端Linux系统传输文件
- 进入远端服务器的命令
- vc客户端连远端linux下的oracle重要步骤
- 通过 ssh proxy 代理连接远端 linux PC 图形界面 [图文]
- Linux下SVN远端安全改密配置脚本
- Linux系统SSH无密码登录远端主机
- 远端linux服务器安装apache(图解过程)
- 如何向远端linux服务器中传输文件
- linux上的Tomcat 8 的远端debug调试
- windows2008_IIS7请求筛选之规则
- 我再也不想在任何头文件中看到“using namespace xxx;”了
- 【设计模式】代理模式
- Bone Collector hdu2602 01背包
- 隐藏Tabbar的一些方法
- java访问远端Linux及执行远端Linux命令
- 关于find .. | tar .. 的问题
- 修改电视flash bug心得
- 二进制数求01的变换次数
- sql server 里面的convert函数,当传入参数为null时,返回null
- 关于如何读代码?
- ACM -- 蛇和梯子
- sql 增删改查
- jBPM:从jBPM3到jBPM5以及Activiti5