?? testsshconnect.java
字號:
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import com.teamsun.itcc.util.StringUtil;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class TestSshConnect {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn = null;
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
conn=new Connection("10.3.7.3",22);
try {
conn.connect();
conn.authenticateWithPassword("itcc", "itcc123");
SCPClient ftp = conn.createSCPClient();
while(true)
{
Session s=conn.openSession();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
String str=sin.readLine();
s.execCommand(str);
InputStream stdout = new StreamGobbler(s.getStdout());
int count;
while ((count = stdout.read(bytes)) != -1){
buffer.write(bytes, 0, count);
if(count==1024){
System.out.println(new String(bytes));
}else{
byte[] bs2=new byte[count];
for(int j=0;j<count;j++)
{
bs2[j]=bytes[j];
}
System.out.println(new String(bs2));
}
}
System.out.println(new String(bytes));
if(s!=null)
{s.close();}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String execCommand(Connection conn, String cmd) throws IOException{
Session sess = conn.openSession();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
sess.execCommand(cmd);
int count;
byte[] bytes = new byte[1024];
InputStream stdout = new StreamGobbler(sess.getStdout());
while ((count = stdout.read(bytes)) != -1)
buffer.write(bytes, 0, count);
} finally {
sess.close();
}
return StringUtil.trim2(buffer.toString("GBK"));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -