转载: java teln…
来源:互联网 发布:alpha go tensorflow 编辑:程序博客网 时间:2024/06/05 00:14
java telnet ssh 实现
- package
telnet; - import
java.io.IOException; - import
java.io.InputStream; - import
java.io.OutputStream; - import
java.net.SocketException; - import
java.util.regex.Pattern; - import
org.apache.commons.net.telnet.TelnetClient; - public
class Telnet { -
private TelnetClient telnetClient; -
private InputStream inputStream; -
private OutputStream outputStream; -
private long timeout= 30000;//默认超时为30秒 -
-
public void connect(String inthostIp, port,String throwsuserName,String password) SocketException, IOException{ -
telnetClient=new TelnetClient(); -
telnetClient.connect(hostIp, port); -
inputStream=telnetClient.getInputStream(); //接收服务器消息的输入流 -
outputStream=telnetClient.getOutputStream(); //发送消息到服务器的输出流 -
//连接完毕,服务器就等待客户发送 用户名,然后是密码 -
if(sendLoginInfo(userName,".*PASSWORD:")){//假如发送完用户名后得到的提示符是密码提示符 -
sendLoginInfo(password, "//]//$"); //假如发送密码信息后得到的提示符是命令提示符 -
} -
} -
private boolean sendLoginInfo(String info,String regext){ -
Pattern pattern=Pattern.compile(regext); -
try { -
outputStream.write((info+"/n").getBytes()); -
outputStream.flush(); -
int i=- 1; -
StringBuilder sb=new StringBuilder(); -
long startTime=System.currentTimeMillis(); -
while(System.currentTimeMillis()-startTime -
while( (i=( char)inputStream.read())>- 1){ -
if(i==-1){ -
throw IllegalArgumentException "接收不到消息");( -
} -
char ch=( char)i; -
if(ch=='/n'||ch=='/r'){ //命令提示符是最后在一行的 -
sb.delete(0, sb.length()); -
continue; -
} -
sb.append((char)ch); -
-
if(pattern.matcher(sb.toString()).find()){ -
return true; -
} -
} -
} -
throw IllegalArgumentException "超时收不到提示符");( -
} catch (IOException e) { -
close(); -
return false; -
} -
} -
-
public String send(String cmd){ -
if(null==telnetClient||null==inputStream||null==outputStream){ -
throw IllegalArgumentException "请先( 建立连接 );或建立连接失败" -
} -
Pattern pattern=Pattern.compile("//]//$");//假如命令提示符为 /$ -
StringBuilder text=new StringBuilder(); -
try { -
outputStream.write((cmd+"/n").getBytes()); -
outputStream.flush(); -
StringBuilder sb=new StringBuilder(); -
long startTime=System.currentTimeMillis(); -
int i=- 1; -
while(System.currentTimeMillis()-startTime -
while( (i=inputStream.read())>- 1){ -
if(i==-1){ -
throw IllegalArgumentException "接收不到消息");( -
} -
char ch=( char)i; -
text.append(ch); -
if(ch=='/n'||ch=='/r'){ -
sb.delete(0, sb.length()); -
continue; -
} -
sb.append(ch); -
if(pattern.matcher(sb.toString()).find()){//返回字符流中找到了命令提示符 -
return text.toString(); -
} -
} -
} -
throw IllegalArgumentException "超时收不到提示符");( -
} catch (IOException e) { -
close(); -
return null; -
} -
} -
public void close(){ -
if(null!=null){ -
try { -
telnetClient.disconnect(); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
} -
} -
public static void main(String[] throwsargs) SocketException, IOException { -
Telnet telnet=new Telnet(); -
telnet.connect("ip" , 23,"username", "passowrd"); -
System.out.println(telnet.send("ls")); -
telnet.close(); -
} - }
-
-
public void connect(String inthostIp, port,String userName,String password){ -
try { -
session=jsch.getSession(userName,hostIp,port ); -
session.setPassword(password); -
session.setUserInfo(defaultUserInfo); -
session.connect(); -
channel=session.openChannel("shell"); -
inputStream=channel.getInputStream(); -
outputStream=channel.getOutputStream(); -
} catch (JSchException e) { -
// TODO Auto-generated catch block -
e.printStackTrace(); -
}catch (IOException e) { -
// TODO Auto-generated catch block -
e.printStackTrace(); -
} -
} -
//关闭连接 -
public void close(){ -
if(null!=channel){ -
channel.disconnect(); -
} -
if(null!=session){ -
session.disconnect(); -
} -
}
- 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) { -
} -
}
- 转载: java teln…
- 转载:C, Erlang, Java and Go Web…
- Why ASMLIB and why not? <转载…
- 当 今 中 国 的 深 层 悲 剧(转载…
- 【转载】pthread_mutex_init …
- 转载--request_irq() | 注册…
- 转载--request_irq() | 注册…
- 【转载】linux C …
- 【转载】 linux-python …
- 【转载】Sublime Text 2…
- 【转载】 linux Python&…
- 【转载】python -- 15个…
- 【转载】cannot be cast…
- (转载)IplImage, CvMat,&nbsp…
- IBM Filenet java Flex FileRefere…
- java Struts2 jquery uploadify 多…
- em的错误 <转载>
- 转载:Xlib: No protocol sp…
- hibernate 对fetch的FetchType理解
- 前端MVC框架之 Angular
- H5实战(三):路径图页面布局
- Package.json详解
- Nginx常见配置说明
- 转载: java teln…
- 1009. 说反话 (20)
- 使用DES加密方式对文件进行加密
- 正则表达式
- 【python】urllib
- MD5加密简单应用
- Windows Mysql 导入utf8mb4编码sql文件出错问题
- java实现MD5
- ShellExecute使用详解