linux - 如何从浏览器或 Azure Functions 调用远程计算机上的 Shell 脚本

标签 linux shell azure azure-functions

我在远程机器上有一个 shell 文件,它将执行某些必需的操作。我可以从虚拟机外部调用这个 shell 吗?

比如使用 Azure 函数或浏览器本身。

这是 shell 的快照。

enter image description here

最佳答案

根据您的需要,我建议您使用SSH连接到远程服务器并执行命令。

我不确定您使用的是哪种语言。所以,这里我只提供java示例代码。

您可以使用 SSH 组件 JCraft 进行远程连接和 shell 命令调用。

JSch jsch = new JSch();

String command = "/tmp/myscript.sh";
Session session = jsch.getSession(user, host, 22);
session.connect();

Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);

channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();

byte[] tmp = new byte[1024];
while (true) {
  while (in.available() > 0) {
      int i = in.read(tmp, 0, 1024);
      if (i < 0) {
          break;
      }
      System.out.print(new String(tmp, 0, i));
  }
  if (channel.isClosed()) {
      if (channel.getExitStatus() == 0) {
          System.out.println("Command executed successully.");
      }
      break;
  }
}
channel.disconnect();
session.disconnect();

另外,你可以引用这个帖子How do I run SSH commands on remote system using Java? .

希望对你有帮助。如有任何疑虑,请随时告诉我。

关于linux - 如何从浏览器或 Azure Functions 调用远程计算机上的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46606721/

相关文章:

linux - 如何在 bash/shell 脚本中实现树命令?

xml - 用于解析 xml 的 shell 命令无法正常运行

linux - 使用终端时类似 alt-tab 的功能?

linux - 使用 shell 脚本生成一系列数字

azure - 如何将 .NET Core 中的应用程序设置转换为 Azure 应用程序设置?

linux id no_squash_root

python - Crontab Python 脚本执行(找不到设置配置文件)

python - socket.sock().bind() 地址已被使用

python - 从训练脚本访问 AML 数据存储上的数据

azure - Onedrive for Business - 无法共享文件/文件夹 : 403 "forbidden"