Python Forum
Cannot run kill -9 through pyvomi on ESXi host
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot run kill -9 through pyvomi on ESXi host
#1
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.

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

Attached Files

Thumbnail(s)
   
Reply
#2
I didn't notice the "echo" command:

remote_ssh_command2 = f'echo kill -9 {tempvar1}'

I removed that and now it's working.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  kill python execution program lebossejames 0 769 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  where to host my python script tomtom 1 1,777 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 5,193 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Failing to connect to a host with WMI tester_V 6 6,117 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Using a button to kill and restart a script duckredbeard 3 4,657 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  how to check for thread kill flag nanok66 1 2,901 May-09-2020, 10:06 PM
Last Post: nanok66
  kill thread or process asap, even during time.sleep nanok66 4 4,275 Apr-29-2020, 10:13 AM
Last Post: nanok66
  Python on ESXi thetechuser11 0 1,957 Jul-22-2019, 01:01 PM
Last Post: thetechuser11
  Linux: Automatically kill process with fixed exe path anddontyoucomebacknomore 4 4,201 Apr-22-2019, 07:35 AM
Last Post: anddontyoucomebacknomore
  How to kill a pid [SOLVED] ebolisa 6 10,035 Oct-28-2018, 01:12 PM
Last Post: ebolisa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020