我在远程计算机上有一个shell文件,它将执行某些必需的操作。我能从虚拟机外部调用这个shell吗。
比如使用Azure功能或浏览器本身。
这是shell的快照。
linux - 如何从浏览器或Azure函数在远程计算机上调用Shell脚本-LMLPHP

最佳答案

根据您的需要,我建议您使用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?
希望对你有帮助。有什么问题,请随时告诉我。

08-04 12:55