1#!/usr/bin/env python3
2#
3# Copyright (C) 2023 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import paramiko
19import argparse
20
21# Usage:
22# ./remote_slay.py --ip_address 10.42.0.247 --process_name memdump_tracelogger --user_name root
23# If the program is not running on the QNX, output will be:
24# memdump_tracelogger is not running on 10.42.0.247
25#
26# If the program is running on the QNX, output will be:
27# 1 memdump_tracelogger running on 10.42.0.247 are slayed
28
29def slay_process(sshclient, process_name):
30    command = f"slay {process_name}"
31    try:
32        stdin, stdout, stderr = sshclient.exec_command(command)
33    except Exception as e:
34        print(f"slay_process catch an exception: {str(e)}")
35        return False
36
37    # Wait for the command to finish
38    exit_status = stdout.channel.recv_exit_status()
39    print(f"{exit_status} processes are slayed.")
40    return exit_status != 0
41
42if __name__ == "__main__":
43    parser = argparse.ArgumentParser(description='Check if a process is running on a remote QNX system.')
44    parser.add_argument('--ip_address', required=True, help='IP address of the remote QNX system')
45    parser.add_argument('--user_name', required=True, help='Name of user')
46    parser.add_argument('--process_name', required=True, help='Name of the process to check')
47    args = parser.parse_args()
48
49    remote_ip = args.ip_address
50    user_name = args.user_name
51    sshclient = paramiko.SSHClient()
52    sshclient.load_system_host_keys()
53    sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
54    sshclient.connect(remote_ip, username=user_name)
55
56    process_name = args.process_name
57    result = slay_process(sshclient, process_name)
58    sshclient.close()
59
60    if result:
61        print(f'{process_name} running on {remote_ip} are slayed')
62    else:
63        sys.exit('No processes matched the supplied criteria, an error occurred, '
64                 'or the number of processes matched and acted upon was an even multiple of 256')
65