【问题标题】:Connect to remote server via telnet and update a file programmatically通过 telnet 连接到远程服务器并以编程方式更新文件
【发布时间】:2017-01-08 04:15:19
【问题描述】:

我想从 Web 应用程序更改 PROROUTE 无线蜂窝路由器 (H685) 的设置。 更改设置的唯一方法是通过 telnet 登录路由器并在编辑器(即 vi)中更改文件并重新启动。

我想使用 JAVA 从服务器端以编程方式执行此操作, 我可以使用 TelnetClient 连接到路由器并发送命令

这里是通过telnet连接到路由器并发送命令的示例代码

public class Test
{
    private TelnetClient telnet = new TelnetClient();
    private InputStream in;
    private PrintStream out;
    private String prompt = "#";

    public Test(String server, String user, String password) {
        try {
            // Connect to the specified server
            telnet.connect(server, 23);
            // Get input and output stream references
            in = telnet.getInputStream();
            out = new PrintStream(telnet.getOutputStream());
            // Log the user on
            readUntil("Login:");
            write(user);
            readUntil("Password:");
            write(password);
            // Advance to a prompt
            readUntil(prompt + " ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String readUntil(String pattern) {
        try {
            char lastChar = pattern.charAt(pattern.length() - 1);
            StringBuffer sb = new StringBuffer();
            boolean found = false;
            char ch = (char) in.read();
            while (true) {
                System.out.print(ch);
                sb.append(ch);
                if (ch == lastChar) {
                    if (sb.toString().endsWith(pattern)) {
                        return sb.toString();
                    }
                }
                ch = (char) in.read();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void write(String value) {
        try {
            out.println(value);
            out.flush();
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    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) {
        try {
            Test telnet = new Test("192.168.1.1", "username", "pwd");
            telnet.sendCommand("ls");
            telnet.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

但我必须在编辑器中更新文件。

  1. 发送命令在vi中打开文件

$vi /flash/.disable_fun_list

function_dtu:

function_wifi:

function_gps: . . . .

function_wifi:on(此行需改)

然后发送命令重启路由器。

$重启

任何想法,如何做到这一点。

【问题讨论】:

  • 是否需要在vi中打开文件?另外,你不能创建一个临时文件,在那里进行修改,删除原始文件并用临时文件替换它吗?
  • ftp 连接并来回传输文件怎么样?
  • 连接后,您可以导航到文件位置,并为您要编辑的行执行string replace。检查sed 例如是否可用?!
  • @Plirkee ftp 不是一个选项
  • 我不是说导航到某行,你可以在提示符下执行vi -c 'command_to_replace' /file/path/fname,这样你就可以替换你想要的字符串并使用vi命令功能保存文件,检查这个sharadchhetri.com/2014/07/06/vi-edit-file-without-opening

标签: java telnet


【解决方案1】:

你的想法听起来很糟糕。

我觉得没有必要在vi编辑器中打开这个文件。在这种情况下,人们通常建议创建一个临时文件并将其替换为原始文件。

建议的方法:

  1. 使用 FileReader 之上的 BufferedReader 打开要修改的文件。

  2. 修改字符串,如果你必须替换当前读取的行;如果没有,则直接进行第 3 步。

  3. 现在,一旦您准备好将字符串写入文件(无论是更改还是未更改),然后创建一个临时文件,并将读取的字符串(行)写入该临时文件。

  4. 一旦你完成了原始文件的所有行,然后关闭 BufferedReader,并删除(或者,最好重命名为其他文件)原始文件。将临时文件重命名为原始文件名,并将此临时文件移动到您复制(和修改)内容的原始文件的位置!

代码应该像这样流动:

BufferedReader br = null;
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("C:\\testing.txt"));
        while ((sCurrentLine = br.readLine()) != null) {
            //Step 2 : do modification with the string read
   //Step 3 : write the final modified string inside a temp file.
   // Step 4 : close the br, and rename/delete the old file; rename the temp file to this original file.
        }

【讨论】:

    【解决方案2】:

    您可以在命令行中使用vi,而无需实际打开vi 作为应用程序

    您可以将 args 传递给 vi 以根据您的需要进行字符串替换 并且你可以传递 args 来保存文件,当然还要加上文件路径。

    所以你最终可能会通过 telnet 连接执行此命令

    vi -c "%s/function_wifi:on/function_wifi:off/g|wq" /path/to/config/file
    

    这会将字符串function_wifi:on 替换为function_wifi:off 并保存文件。

    然后发送一个reboot 命令。

    更多请查看this link

    【讨论】:

      【解决方案3】:

      把上面的程序改成

      private String prompt = ">";
      

      而不是

      private String prompt = "#";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-02
        • 2020-03-31
        • 1970-01-01
        • 1970-01-01
        • 2014-04-05
        • 1970-01-01
        • 2019-11-16
        • 1970-01-01
        相关资源
        最近更新 更多