📄 sshtest.java
字号:
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class SSHTest {
public static final UserInfo defaultUserInfo = new UserInfo() {
public String getPassphrase() {
return null;
}
public String getPassword() {
return null;
}
public boolean promptPassword(String arg0) {
return false;
}
public boolean promptPassphrase(String arg0) {
return false;
}
public boolean promptYesNo(String arg0) {
return true;
}
public void showMessage(String arg0) {
}
};
public static void main(String[] args) throws Exception {
String hostname = "9.186.61.85";
String username = "root";
String password="nim";
InputStream stdIn;
PrintStream stdOut ;
String mgcoutput = "";
Session session;
Channel channel;
JSch jsch = new JSch();
UserInfo ui = new MyUserInfo("nim");
session = jsch.getSession(username, hostname, 22);
//session.setPassword(password);
session.setUserInfo(ui);
session.connect();
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand("nim -o define -t standalone -a platform=chrp -a netboot_kernel=mp -a if1=\"find_net test 0\" -a cable_type1=bnc -a net_definition=\"ent 255.255.255.128 9.186.61.7 9.186.61.7\" test");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
stdIn = channel.getInputStream();
stdOut = new PrintStream (channel.getOutputStream());
byte[] tmp = new byte[1024];
while(true) {
while(stdIn.available()>0) {
int i = stdIn.read(tmp, 0, 1024);
System.out.print(">"+new String(tmp, 0, i));
mgcoutput = new String(tmp,0,i).toString();
break;
}
if(channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){ee.printStackTrace();}
}
session.disconnect();
}
public static class MyUserInfo implements UserInfo
{
String passwd ;
MyUserInfo(String pwd)
{
this.passwd = pwd;
}
public String getPassword()
{
return passwd;
}
public boolean promptYesNo(String str)
{
return true;
}
public String getPassphrase()
{
return null;
}
public boolean promptPassphrase(String message)
{
return true;
}
public boolean promptPassword(String message)
{
return true;
}
public void showMessage(String message)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -