How to run shell script on VM via python script?

465 Views Asked by At

I am trying to run shell (Bash script) running against Azure Linux VM run via Python script. I have managed to run simple commands, but I am not able to run a script.

This is working:

run_command_parameters = {
'command_id': 'RunShellScript',
'script': 'ls -l /tmp'
}]

poller = compute_client.virtual_machines.begin_run_command(resource_group, virtual_machine, run_command_parameters)

result = poller.result()
print(result.value[0].message)

But this is not:

run_command_parameters = {
'command_id': 'RunShellScript',
'script': './my_script.sh'
}]

poller = compute_client.virtual_machines.begin_run_command(resource_group, virtual_machine, run_command_parameters)

result = poller.result()
print(result.value[0].message)

I believe, based on exactly the same PowerShell script the variable called 'script' is used for commands, while other undocumented variable is used to source a whole script. I have tried 'script_path' and not the script is kind of working. While it does not run any command from my_script.sh I am get this:

Enable succeeded:
[stdout]
This is a sample script
Optional parameters: /var/lib/waagent/run-command/download/59/script.sh

[stderr]

Any help? What is the proper way to run a shell script on Azure VMs using Python script? I was trying to find something directly in libraries, but no success.

Best regards, sunrrrise

0

There are 0 best solutions below