手机怎么做3d短视频网站,网站开发工程师php岗位职责,焦作网站开发公司电话,达州做淘宝网站背景
工作中需要管理多套环境, 有时需要同时登陆多个节点, 且每个环境用户名密码都一样, 因此需要一个方案来解决动态的批量登录问题.
XShell
Xshell有session管理功能: 提供了包括记住登录主机、用户名、密码及登录时执行命令或脚本(js,py,vbs)的功能 session被存储在xsh文…背景
工作中需要管理多套环境, 有时需要同时登陆多个节点, 且每个环境用户名密码都一样, 因此需要一个方案来解决动态的批量登录问题.
XShell
Xshell有session管理功能: 提供了包括记住登录主机、用户名、密码及登录时执行命令或脚本(js,py,vbs)的功能 session被存储在xsh文件中, 默认的存储在%USERPROFILE%\Documents\NetSarang Computer\7\Xshell\Sessions文件夹下 使用xshell可以直接打开存储在xsh文件中的用户登录信息, 比如: /d/Program_Files/Xshell/Xshell 192.168.31.6.xsh xsh文件使用UTF-16LE编码 xsh采用与ini相同的格式进行配置 xsh有许多配置项, 这里列举比较重要的: [CONNECTION].Host: 登录用户名 [CONNECTION:AUTHENTICATION].UserName: 登录用户名 [CONNECTION:AUTHENTICATION].Password: 登录密码, 使用XShell自有加解密算法, 因此在生成时需要先根据加解密算法生成加密后的密码, 参考how-does-Xmanager-encrypt-password1, 我通过pyinstaller -F XShellCryptoHelper.py将其打包为exe供java使用 [CONNECTION:AUTHENTICATION].UseInitScript: 是否使用登录脚本, 1表示开启, 0表示不使用 XShell同时提供了与expect一样的交互功能, 可以和脚本共同使用, 但由于脚本本身具备这种功能, 并且移植性好, 所以本文不考虑expect [CONNECTION:AUTHENTICATION].ScriptPath: 登录脚本存储位置
需求
通过java生成一个/d/test.xsh文件(能生成一个就能生成N个), 并且在登录的同时执行一个python脚本, 效果如下: 所需信息: 用户名root 密码test2023 登录主机192.168.31.6 执行脚本D:\init.py: def Main():# 等待root用户登录成功xsh.Screen.WaitForString(#)xsh.Screen.Send(echo hello word\r)思路
XShell提供了一个默认的session配置文件: %USERPROFILE%\Documents\NetSarang Computer\7\Xshell\Sessions\default:
读取它, 并且根据关键字一一替换: Host - Host192.168.31.6UserName - UserNamerootPassword - Passwordxxx, 这里根据自己生成的密码密文进行替换UseInitScript0 - UseInitScript1ScriptPath - ScriptPathD:\init.py 使用UTF-16LE进行编码并保存在/d/test.xsh中使用XShell /d/test.xsh进行测试, 成功登录并且打印出hello world即可
实现
使用java17进行编码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;public class Xsh {private static final String XSHELL_CRYPTO_HELPER_LOCATION D:/XShellCryptoHelper.exe;private static final String[] ENCRYPT_CMD new String[]{cmd, /c, XSHELL_CRYPTO_HELPER_LOCATION, -e, test2023};private static final String DEFAULT_SESSION_LOCATION System.getenv(USERPROFILE) \\Documents\\NetSarang Computer\\7\\Xshell\\Sessions\\default;private static final String TEST_XSH_LOCATION D:\\test.xsh;private static final String[] RUN_XSHELL_CMD new String[]{cmd, /k, start D:\\Program_Files\\Xshell\\XShell.exe TEST_XSH_LOCATION};public static void main(String[] args) throws IOException, InterruptedException {String xshContent Files.readAllLines(Paths.get(DEFAULT_SESSION_LOCATION), StandardCharsets.UTF_16LE).stream().map(line - {if (line null || line.isBlank()) {return line;}return switch (line.trim()) {case Host - Host192.168.31.6;case UserName - UserNameroot;case Password - Password encrypt();case UseInitScript0 - UseInitScript1;case ScriptPath - ScriptPathD:\\init.py;default - line;};}).collect(Collectors.joining(System.lineSeparator()));Path path Paths.get(TEST_XSH_LOCATION);Files.deleteIfExists(path);Files.writeString(path, xshContent, StandardCharsets.UTF_16LE);CompletableFuture.runAsync(() - {try {Runtime.getRuntime().exec(RUN_XSHELL_CMD);} catch (IOException e) {throw new RuntimeException(e);}});TimeUnit.SECONDS.sleep(3);}private static String encrypt() {InputStream is null;InputStreamReader isr null;BufferedReader br null;try {Process process Runtime.getRuntime().exec(ENCRYPT_CMD);process.waitFor();is process.getInputStream();isr new InputStreamReader(is);br new BufferedReader(isr);// 只有一行输出return br.readLine();} catch (Exception e) {throw new RuntimeException(e);} finally {try {br.close();isr.close();is.close();} catch (IOException e) {throw new RuntimeException(e);}}}
}参考
Using Script 本项目原作者很长时间没有更新, 本来不支持7.*版本的加密, 我参考XDecrypt项目对其进行了补充, 当前已支持XShell全系列加解密!! ↩︎