简体   繁体   中英

Python command fails to restart linux service

I have this python code on my windows machine that gets the status of a service on my linux server :

import paramiko

client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('MY_HOST', username='MY_USER', password='MY_PASS')

stdin, stdout, _stderr = client.exec_command('systemctl status example.service')

print(_stdout.read())

client.close()

But when I switch from "systemctl status" to "systemctl restart" to restart the service nothing happens and the service doesn't restart!

Am I doing something wrong ? Is there another way to restart a linux service using python code on my windows machine? Thanks in advance!

I found the solution, the problem was that I wasn't logged in with the root user , so I changed the ssh configuration to login directly with the root user and the restart worked!

1 - First, enable ROOT by running this command " sudo passwd root ", then enter the password and then confirm!

2 - Now, let's edit the file that will enable SSH access in Ubuntu with this command " vi /etc/ssh/sshd_config "!

3 - Look for this part of the code and leave it like this:

# Authentication:
LoginGraceTime 120
#PermitRootLogin prohibit-password
PermitRootLogin yes
StrictModes yes

4 - Finally restart the SSHD service with this command " sudo systemctl restart ssh " and that's it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM