Wednesday, May 8, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 499  / 1 Year ago, sun, december 11, 2022, 2:43:18

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.


More From » ssh

 Answers
6

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 + "
")
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 + "
")
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)

[#387] Monday, December 12, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
llael

Total Points: 209
Total Questions: 102
Total Answers: 118

Location: Rwanda
Member since Fri, May 5, 2023
1 Year ago
llael questions
Fri, May 19, 23, 07:42, 1 Year ago
Tue, May 18, 21, 06:06, 3 Years ago
Sun, Aug 21, 22, 23:41, 2 Years ago
Sat, Oct 16, 21, 08:01, 3 Years ago
;