Hi all,
Hopefully someone can assist. I have a python script that uses pyvomi to connect to an ESXi host. I'm trying to run a "kill -9" command and it's not running. I have attached the output from running the script. It isn't erroring out so I'm not sure why it ran the first command but not the second.
Hopefully someone can assist. I have a python script that uses pyvomi to connect to an ESXi host. I'm trying to run a "kill -9" command and it's not running. I have attached the output from running the script. It isn't erroring out so I'm not sure why it ran the first command but not the second.
# https://stackoverflow.com/questions/54400977/trigger-host-custom-shell-command-using-vmware-sdk
# enables and disables SSH, does not require SSH to be started initially
# pip install vmwc paramiko
from pyVim.connect import SmartConnect
from pyVmomi import vim
from getpass import getpass
from vmwc import VMWareClient
import paramiko
def main():
host = '1.1.1.1'
username = 'root'
password = getpass()
with VMWareClient(host, username, password) as client:
client.enable_ssh()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password)
remote_ssh_command = "ps | grep sshd | awk '{print $2}' | head -1" # Your remote command
stdin, stdout, stderr = ssh.exec_command(remote_ssh_command)
tempvar1=stdout.read().decode('utf-8')
print(tempvar1)
output = stdout.read().decode('utf-8')
print(f"Output: {output}")
error = stderr.read().decode('utf-8')
print(f"Error: {error}")
remote_ssh_command2 = f'echo kill -9 {tempvar1}'
stdin, stdout, stderr = ssh.exec_command(remote_ssh_command2)
tempvar2=stdout.read().decode('utf-8')
print(tempvar2)
output = stdout.read().decode('utf-8')
print(f"Output: {output}")
error = stderr.read().decode('utf-8')
print(f"Error: {error}")
client.disable_ssh() # optional in case you want to close the ssh access
if __name__ == '__main__':
main()
