java telnet(java telnetclient)

      最后更新:2024-03-19 15:53:56 手机定位技术交流文章

      java代码访问telnet,并受到返回值

      import java.io.InputStream; import java.io.PrintStream;import org.apache.commons.net.telnet.TelnetClient;public class Shell {private TelnetClient telnet = new TelnetClient();private InputStream in;private PrintStream out;private char prompt = '$';// 普通用户结束public Shell(String ip, int port, String user, String password) {try {telnet.connect(ip, port);in = telnet.getInputStream();out = new PrintStream(telnet.getOutputStream());// 根据root用户设置结束符this.prompt = user.equals("root") ? '#' : '>';login(user, password);} catch (Exception e) {e.printStackTrace();}}/*** 登录** @param user* @param password*/public void login(String user, String password) {// read()Until("login:");readUntil("login:");write(user);readUntil("Password:");write(password);readUntil(prompt + "");}/*** 读取分析结果** @param pattern* @return*/public String readUntil(String pattern) {try {char lastChar = pattern.charAt(pattern.length() - 1);StringBuffer sb = new StringBuffer();char ch = (char) in.read();while (true) {sb.append(ch);if (ch == lastChar) {if (sb.toString().endsWith(pattern)) {return sb.toString();}}ch = (char) in.read();System.out.print(ch);}} catch (Exception e) {e.printStackTrace();}return null;}/*** 写操作** @param value*/public void write(String value) {try {out.println(value);out.flush();} catch (Exception e) {e.printStackTrace();}}/*** 向目标发送命令字符串** @param command* @return*/public String sendCommand(String command) {try {write(command);return readUntil(prompt + "");} catch (Exception e) {e.printStackTrace();}return null;}/*** 关闭连接*/public void disconnect() {try {telnet.disconnect();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {//TelnetClient telnet = new TelnetClient();try {Shell she = new Shell("192.168.1.10", 23, "root", "123456");System.out.println(she);//执行的命令System.out.println(she.sendCommand("ll"));she.disconnect();} catch (Exception e) {// TODO: handle exception}} }
      java代码访问telnet,并受到返回值

      java实现telnet连接操作,打包到服务器后访问报null?

      java">下面是我自己写的一个读取并显示txt文件的demo,希望对您有帮助。publicclassClient{publicstaticvoidmain(String[]args){ClientFramef=newClientFrame();}}importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Dimension;importjava.awt.GridLayout;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.Socket;importjava.net.UnknownHostException;importjava.util.Vector;importjavax.swing.BorderFactory;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JList;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.event.ListSelectionEvent;importjavax.swing.event.ListSelectionListener;publicclassClientFrameextendsJFrameimplementsActionListener,ListSelectionListener{privateJListlist=null;privateJButtonsbtn=null;privateJButtoncbtn=null;privateVectorv=null;privateJTextAreatxt=null;privateContainercontrol=null;privateContainerbtn=null;privateSocketclient=null;privateDataInputStreamreader=null;privateDataOutputStreamwriter=null;publicClientFrame(){this.list=newJList();this.list.setBorder(BorderFactory.createTitledBorder("文件列表"));this.list.addListSelectionListener(this);this.sbtn=newJButton("显示");this.sbtn.addActionListener(this);this.cbtn=newJButton("清除");this.cbtn.addActionListener(this);this.control=newContainer();this.control.setPreferredSize(newDimension(150,400));this.control.setLayout(newBorderLayout());this.control.add(newJScrollPane(this.list),BorderLayout.CENTER);this.btn=newContainer();this.btn.setLayout(newGridLayout(1,2));btn.add(sbtn);btn.add(cbtn);this.control.add(this.btn,BorderLayout.SOUTH);this.txt=newJTextArea();this.txt.setEditable(false);this.txt.setSize(350,400);this.setTitle("客户端");this.setSize(500,400);this.setVisible(true);DimensiondisplaySize=Toolkit.getDefaultToolkit().getScreenSize();this.setLocation((displaySize.width-this.getWidth())/2,(displaySize.height-this.getHeight())/2);this.setLayout(newBorderLayout());this.add(this.control,BorderLayout.WEST);this.add(newJScrollPane(this.txt),BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);try{//this.client=newSocket("192.168.32.34",6666);this.client=newSocket("192.168.1.100",6666);this.reader=newDataInputStream(client.getInputStream());this.writer=newDataOutputStream(client.getOutputStream());}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}publicvoidactionPerformed(ActionEventevent){if(event.getSource()==sbtn){if(v==null){v=newVector();}else{v.clear();}try{writer.writeUTF("getfilelist");writer.flush();Stringt=reader.readUTF();while(t!=null&&!t.equals("")){v.add(t);t=reader.readUTF();}}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}this.list.setListData(v);}if(event.getSource()==cbtn){this.txt.setText("");}}publicvoidvalueChanged(ListSelectionEvente){inti=this.list.getSelectedIndex();if(!this.list.getValueIsAdjusting()&&i!=-1){try{writer.writeUTF("getfilecontent_"+i);writer.flush();Stringtmp=reader.readUTF();this.txt.setText(tmp);}catch(IOExceptione1){e1.printStackTrace();}}}}importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.Reader;importjava.net.ServerSocket;importjava.net.Socket;importjava.util.ArrayList;importjava.util.Iterator;publicclassServer{staticArrayListfileArray=newArrayList();publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;Socketclient=null;Stringcmd="";try{server=newServerSocket(6666);client=server.accept();DataInputStreamreader=newDataInputStream(client.getInputStream());DataOutputStreamwriter=newDataOutputStream(client.getOutputStream());while(true){cmd=reader.readUTF();System.out.println(cmd);if(cmd.equals("getfilelist")){fileArray.clear();//fileArray=getFile(newFile("D:/tmp"));fileArray=getFile(newFile("D:/学习/教程/学习笔记"));Stringfn="";for(intk=0;kgetFile(Filef){File[]ff=f.listFiles();for(Filechild:ff){if(child.isDirectory()){getFile(child);}else{fileArray.add(child);}}returnfileArray;}}
      java实现telnet连接操作,打包到服务器后访问报null?

      急,java中telnet访问远程电脑,然后执行cmd命令,再获取返回值。

      需要个jar包:commons-net-3.3-bin.zip下载地址:http://commons.apache.org/proper/commons-net/download_net.cgi你找的代码我修改过了,复制直接使用。linux系统需要先开启telnet服务//执行的命令System.out.println(she.sendCommand("ll"));这个输入linux 命令import java.io.InputStream;import java.io.PrintStream;import org.apache.commons.net.telnet.TelnetClient;public class Shell { private TelnetClient telnet = new TelnetClient(); private InputStream in; private PrintStream out; private char prompt = '$';// 普通用户结束 public Shell(String ip, int port, String user, String password) { try { telnet.connect(ip, port); in = telnet.getInputStream(); out = new PrintStream(telnet.getOutputStream()); // 根据root用户设置结束符 this.prompt = user.equals("root") ? '#' : '>'; login(user, password); } catch (Exception e) { e.printStackTrace(); } } /**  * 登录*  * @param user  * @param password  */ public void login(String user, String password) { // read()Until("login:"); readUntil("login:"); write(user); readUntil("Password:"); write(password); readUntil(prompt + ""); } /**  * 读取分析结果*  * @param pattern  * @return  */ public String readUntil(String pattern) { try { char lastChar = pattern.charAt(pattern.length() - 1); StringBuffer sb = new StringBuffer(); char ch = (char) in.read(); while (true) { sb.append(ch); if (ch == lastChar) { if (sb.toString().endsWith(pattern)) { return sb.toString(); } } ch = (char) in.read(); System.out.print(ch); } } catch (Exception e) { e.printStackTrace(); } return null; } /**  * 写操作*  * @param value  */ public void write(String value) { try { out.println(value); out.flush(); } catch (Exception e) { e.printStackTrace(); } } /**  * 向目标发送命令字符串*  * @param command  * @return  */ public String sendCommand(String command) { try { write(command); return readUntil(prompt + ""); } catch (Exception e) { e.printStackTrace(); } return null; } /**  * 关闭连接  */ public void disconnect() { try { telnet.disconnect(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) {// TelnetClient telnet = new TelnetClient(); try { Shell she = new Shell("192.168.1.10", 23, "root", "123456"); System.out.println(she); //执行的命令 System.out.println(she.sendCommand("ll")); she.disconnect(); } catch (Exception e) { // TODO: handle exception } }}
      很多人安装win7后,无法使用telnet命令了,提示“telnet不是内部或外部命令,也不是可运行的程序”。 首先你要要确认你的win7版本,如果是家庭版,或高级家庭版,这个命令不能使用,只有win7旗舰版才有telnet命令。如果你已经是win7旗舰版,请按照下面的步骤开启telnet命令:1、打开控制面板,打开程序和功能;2、点击左边的“打开或关闭Windows功能”3、找到telnet客户端,勾选,如果你想别人telnet到你的机器,你就需要把telnet服务器端勾选,如果你想两个都要——既想通过本机telnet到别的机器,也想在别的机器上telnet到本机器,那么就勾选两个选项吧。4、安装完成后,Telnet服务默认情况下是禁用的。右键我的电脑,点击“管理”,找到“服务”,找到Telnet服务,设置其启动方式为“手动”(更安全,只在需要的时候才启用),或点击“开始”→“运行”,输入servcies.msc打开服务管理器。最后“启动”该服务“确定”退出即可。特别说明:部分被优化过的win7旗舰版无法安装telnet服务,或者这个所谓的旗舰版其实只是冒牌的。5、如果你在win7上无法成功安装telnet客户端,建议你使用telnet的替代工具,推荐你是用 Xshell ,比telnet更为强大的远程调试工具。 到此希望你解决了问题。
      B要开通telnet服务,简单测试,你自己telnet 一下B,开有没有反应就可以了。 再有,cmd是windows命令,B是windows吗,如果是的话,只能支持很少的命令。
      用公网ip试试
      急,java中telnet访问远程电脑,然后执行cmd命令,再获取返回值。

      怎样用JAVA编程实现telnet?

      use Socket's connect(SocketAddress endpoint, int timeout)PROC can enablement telnet's function,if catch exception declaration telnet fail try ...{Socket server = new Socket();InetSocketAddress address = new InetSocketAddress("192.168.0.113",8080);server.connect(address, 5000);server.close();} catch (UnknownHostException e) ...{System.out.println("telnet lose");} catch (IOException e) ...{System.out.println("telnet lose");} 乱码凑合着交八
      怎样用JAVA编程实现telnet?

      java telnet返回乱码问题

      终端类型不对应,或编码不对应 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      java telnet返回乱码问题

      本文由 在线网速测试 整理编辑,转载请注明出处,原文链接:https://www.wangsu123.cn/news/305642.html

          热门文章

          文章分类