-1

I want to run ssh without asking for password and invoke systemctl without password i tried doing the following:

ssh -t <user>@<host> "echo <password> | sudo -S systemctl status kubelet"

and:

sshpass -p '$<password>' ssh -t -o 'StrictHostKeyChecking=no' <user>@<host> 'sudo service kubelet restart

also:

 echo <password> | ssh -tt <user>@<host> "sudo service kubelet restart"

i added the user to the sudoers and i am able to run systemctl without password all the commands from above ask for a password except:

sshpass -p '$<password>' ssh -t -o 'StrictHostKeyChecking=no' <user>@<host> 'sudo service kubelet restart

which gives me no prompt and looks like it worked but did nothing.

1
  • Welcome to Ask Ubuntu. This is a question answer site. I am happy that you found a solution to your problem. Please don't put SOLVED in the question title and the solution inside your question. Use the Answer Your Own Question button below for the answer. Then after waiting for 2 days, accept your answer as the correct one by clicking on the gray check mark ✔️ next to your answer and turn it green ✅. This will help others.
    – user68186
    Jul 5, 2022 at 12:24

2 Answers 2

0

sshpass only passes password for your ssh connection. But your "sudo service kubelet restart" - command itself asks for sudo password separately. Your systemctl status command does not need sudo privilege, that's why it goes through. Try it like this:

sshpass -p '$<password>' ssh -t -o 'StrictHostKeyChecking=no' <user>@<host> "echo <password> | sudo -S service kubelet restart"
3
  • Still not working, I did systemctl stop kubelet and tried to execute the command above from a remote machine, it looks like it did something but the service is still stopped. I did find a solution to the problem yesterday but I don't like it, it's adding the ssh pub key of the user to the remote system so that I could ssh without a password and add the user to the sudoers. but this is way too complicated for something that seems too simple
    – Daniel
    Jun 30, 2022 at 7:29
  • Can you run the "history" command on your remote machine and see what command actually got executed after running the sshpass command? Jun 30, 2022 at 9:52
  • yes i checked that, found nothing, the command didn't pass any commands to the remote server
    – Daniel
    Jun 30, 2022 at 10:30
0

I used python3 for this solution, and tho not elegant but it works(this is not the full script but only the relevant part).

initiate_restart = [
    "echo <password> | sudo -S systemctl restart kubelet",
    "echo <password> | sudo -S systemctl restart docker",
]
node = paramiko.SSHClient()
print(node)
node.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    node.connect(hostname=get_ip, username=user_name, password=node_pass)
    with open("/tmp/reset_log.log", "a") as f:
        f.write(date + ' Restarting ' + 'Node-' + get_ip + "\n")
except:
    print("[!] Cannot connect to the SSH Server")
    with open("/tmp/reset_log.log", "a") as f:
        f.write(date + ' Could not connect to' + ' Node-' + get_ip + "\n")
    exit()
for command in initiate_restart:
    print("="*50, command, "="*50)
    stdin, stdout, stderr = node.exec_command(command)
    print(stdout.read().decode())
    err = stderr.read().decode()
    if err:
        print(err)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .