Python script with Paramiko fails from Rundeck

已查看 55 次
跳至第一个未读帖子

Rahul Saxena

未读,
2023年5月31日 06:24:232023/5/31
收件人 rundeck...@googlegroups.com
Hi Team,

I need help with the execution of a Python script that uses the Paramiko module to SSH to host.  The script works fine from VSCode but when i execute the same script from Rundeck i get below error:

image.png

I was going through some Rundeck docs and based on that i updated framework.properties file to add path for private keys:

framework.ssh.keypath = C:/Users/rsaxena/.ssh
framework.ssh.user = rsaxena

I copied both PPK and PEM keys to this directory and restarted Rundeck service.  My Rundeck is installed on a Windows host and I need to run the command on a Linux box.

Below is my working Python script:

from paramiko import SSHClient, AutoAddPolicy
import time
import sys


# Function to restart Nginx service remotely
def restart_nginx_remote(host_ip, username):
    try:
        client = SSHClient()
    # client.load_system_host_keys()
        client.load_host_keys("key.pem")
        client.set_missing_host_key_policy(AutoAddPolicy())

        client.connect(
            f"{host_ip}",
            port=22,
            username=f"{username}",
            key_filename="key.pem",
        )
       
        # Execute the command to restart Nginx service
        stdin, stdout, stderr = client.exec_command('sudo service nginx restart')
       
        # Check the command execution result
        exit_status = stdout.channel.recv_exit_status()
        if exit_status == 0:
            print("Nginx service restarted successfully.")
        else:
            print(f"Failed to restart Nginx service. Exit status: {exit_status}")
       
        # Close the SSH connection
        client.close()
    except Exception as e:
        print(e)

# Call the function to restart Nginx remotely

if __name__ == "__main__":
    # SSH connection parameters
    region = 'NA'
    # region = sys.argv[1]
    if region == 'NA':
        host_ip = '10.0.0.1
    username = 'centos'
    restart_nginx_remote(host_ip, username)

I think Rundeck is unable to find the key but I am not sure where I am going wrong.  Can someone please help me figure this out?

Thanks,
Rahul

rac...@rundeck.com

未读,
2023年5月31日 09:32:292023/5/31
收件人 rundeck-discuss
Hi,

You can obtain more clues running the job in debug mode. Also, you can try launching the script from the script step ("script file or url" step instead of "command" step). Define the right python interpreter and extension by clicking on the "Advanced" link.

Regards.

Rahul Saxena

未读,
2023年5月31日 09:51:182023/5/31
收件人 rundeck...@googlegroups.com
Is the error because Rundeck is trying to use SSH key against localhost:

[workflow] Begin step: 1,NodeDispatch
1: Workflow step executing: CommandItem{command=[3 words]}
preparing for sequential execution on 1 nodes
Executing command on node: localhost, NodeEntryImpl{tags=[], attributes={nodename=localhost, hostname=localhost, osVersion=10.0, osFamily=windows, osArch=amd64, description=Rundeck server node, osName=Windows Server 2019}, project='null'}
[workflow] beginExecuteNodeStep(localhost): NodeDispatch: CommandItem{command=[3 words]}
using charset: null
Current OS is Windows Server 2019

Execute:Java13CommandLauncher: Executing 'cmd.exe' with arguments:

'/c'

'D:\Rahul\automation\myenv\Scripts\python.exe'

'D:\Rahul\automation\restartnginx.py'

'NA'



The ' characters around the executable and arguments are

not part of the command.
not a valid OPENSSH private key file

--
You received this message because you are subscribed to the Google Groups "rundeck-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rundeck-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rundeck-discuss/4945869a-ba9f-4aa6-970d-a487710b3749n%40googlegroups.com.
回复全部
回复作者
转发
0 个新帖子