0

I have a shell file on the remote machine which will perform certain required actions. Can I call this shell from outside of the VM.

Like by using Azure functions or browser itself.

Here is the Snapshot for shell.

enter image description here

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
Anand Deshmukh
  • 1,086
  • 2
  • 17
  • 35

1 Answers1

0

According your needs,I suggest you connecting to a remote server using SSH and execute commands.

I'm not sure which language you are using. So,I just offer java sample code for you here.

You could use SSH component JCraft for remote connection and shell commands invocations.

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();

Also,you could refer to this thread How do I run SSH commands on remote system using Java?.

Hope it helps you. Any concern,please feel free to let me kown.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32