最佳答案
根据您的需要,我建议您使用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/