⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testsshconnect.java

📁 基于java的telnet功能实现。是用java编写的实现telnet功能的java源代码。
💻 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"));
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -