公司要求开发出浏览器一键调用本地应用(CRT,XFTP,XSHELL)并登录功能
1、本地应用调用思路
开发的web是否支持IE
1.1、IE ActiveX 通过调用控件js文件中编辑代码如下:
<script type="text/javascript" language="javascript">
function RunFile() {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run('cmd /k F:\\SecureCRT\\SecureCRT.exe /SSH2 /L root /P 22 /PASSWORD root 172.16.4.65 & exit' );
}
window.onload = function(){
RunFile();
}
</script>
1.2、Google、Firefox启动
这个东西真的是折磨了一阵没思路后来一顿查找操作,发现可以通过修改注册表浏览器使用URL Protocol
1.2.1、思路一将CRT启动指令直接插入注册表启动CRT
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\CRTTOOL]
@="CRTTOOL Protocol"
"url Protocol"=""
[HKEY_CLASSES_ROOT\CRTTOOL\DefaultIcon]
@="CRTTool.exe,1"
[HKEY_CLASSES_ROOT\DMPTOOL\shell]
@=""
[HKEY_CLASSES_ROOT\CRTTOOL\shell\open]
@=""
[HKEY_CLASSES_ROOT\DMPTOOL\shell\open\command]
@="\"C:\\CRTTool.exe\" \"%1\""
上方代码为crt.reg文件内容指定了浏览器访问指令为:CRTTOOL Protocol,软件.exe文件的存放位置%1为入参,即为你要传给exe文件的值。完整指令如下:
CRTTOOL ://F:\\SecureCRT\\SecureCRT.exe ssh2://root:root@172.16.4.65:22
出现问题
软件可以正常打开但是无法自动登录对应IP的账户
1.2.2、思路二通过JAVA加入中间件
我是学JAVA于是就有了以下操作,通过又一顿的查资料了解到,可以通过JAVA编写的exe程序来调用本地cmd程序执行指令实现软件启动,使用**main(String[] args)**自定义对exe的入参操作,实现登录。
import javax.swing.*;
import java.io.IOException;
import java.net.URLDecoder;
/**
* @description: TODO
* @author: linyh
* @date: 2020-03-25
*/
public class tool {
public static void main(String[] args) throws IOException {
String msg = "";
if (args != null && args.length > 0) {
for (String str : args) {
msg += str;
}
Runtime.getRuntime().exec("cmd /c " + URLDecoder.decode(msg.replace("dmptool://", "")));
} else {
JOptionPane.showMessageDialog(null, "程序启动发生异常");
}
}
}
页面调用 :
DMPTOOL://param --param为字符串入参,JAVA中对应main(String[] args)的args
注:如果路径中有空格就在param上加""号
示例:DMPTOOL://F:\SecureCRT\SecureCRT.exe ssh2://root:root@172.16.4.65:22
js调用
window.location = 'DMPTOOL://"F:\\SecureCRT\\SecureCRT.exe ssh2://root:root@172.16.4.65:22"';
EXE
一、JAVAEXE 依赖jre请确保电脑已配置
二、执行.reg文件参考1.2.1修改软件路径与对应的名称即可
出现问题
软件可以正常打开账号正常登录但是需要jre
1.2.3、思路三C#编写中间键
没学过临时写的见谅
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
if (args!=null && args.Length>0)
{
string strInput = "";
foreach (string str in args)
{
strInput += str;
}
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(System.Web.HttpUtility.UrlDecode(strInput.Replace("dmptool://", ""), System.Text.Encoding.UTF8) + "&exit");
//p.StandardInput.AutoFlush = true;
//获取输出信息
//string strOuput = p.StandardOutput.ReadToEnd();
//等待程序执行完退出进程
//p.WaitForExit();
p.Close();
}
}
}
}
Crt、XFTP、XSHELL连接指令
CRT
window.location = "DMPTOOL://F:\\SecureCRT\\SecureCRT.exe ssh2://root:root@172.16.4.65:22";
Xshell
window.location = "DMPTOOL://D:\\Xshell\\Xshell.exe ssh://root:root@172.16.4.65:22";
Xftp
window.location = "DMPTOOL://D:\\Xftp6\\Xftp.exe sftp://root:root@172.16.4.65:22";
2、附件地址
https://editor.csdn.net/md/?articleId=105383403