-1

This is my piece of code that I am trying to use to restart a service but I am not able to. I am using python's paramiko module to restart a service by going into the container of the service.

def sshOpensips(ip):
        warnings.filterwarnings(action='ignore',module='.*paramiko.*')
        client = pm.SSHClient()
        commands = ["docker exec -it opensips bash", "service opensips restart"]
        client.set_missing_host_key_policy(pm.AutoAddPolicy())
        pk = pm.RSAKey.from_private_key(open('/home/asad.javed/.ssh/y'))
        try:
                client.connect(ip, username='asad.javed', pkey=pk)
                print("Connection Established")
        except pm.SSHException:
                print("Connection Failed")
        for command in commands:
                stdin, stdout, stderr = client.exec_command(command);
        client.close()
        return True

Can someone please guide how can I restart a service by going into a docker container?

Asad Javed
  • 57
  • 6
  • *"But I am not able to"* is not a problem description. – Anyway, I believe that this is what you are after: [Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko](https://stackoverflow.com/q/58452390/850848). – Martin Prikryl Oct 13 '21 at 17:33
  • Can you guide me how can I go inside a docker container and restart a service? – Asad Javed Oct 13 '21 at 17:45
  • Did you read and understood my answer to the linked question? – Martin Prikryl Oct 14 '21 at 17:49
  • Hi Martin, thanks for the help I was able to resolve the problem I was facing. I will share my answer. – Asad Javed Oct 15 '21 at 12:15
  • Your solution seems unnecessarily complicated to me. If you ended up executing a single command only, why don't you use plain `SSHClient.exec_command`? – Martin Prikryl Oct 16 '21 at 06:53
  • SSHClient.exec_command was working for user with sudo privileges but on VMs where I had to login as a normal user and then login as root then I had to use the complicated code. – Asad Javed Oct 17 '21 at 19:22
  • But why? What does your code do differently than `SSHClient.exec_command`? – Martin Prikryl Oct 17 '21 at 20:11
  • With root I was able to login once. But on VMs where I was logging in without root priviliges wasn't able to execute the command I shared in the question that is why I had to create a session and then use keyring module to hide my password because a password prompt would appear and I would have to type in the password again which was a one time thing. Sorry but I am new to Python world and still learning. This worked for me and I shared the solution. – Asad Javed Oct 17 '21 at 20:16
  • I do not see anything in your code what plain `SSHClient.exec_command` won't do equally well with lot less code. – Martin Prikryl Oct 18 '21 at 05:48
  • Yes the above code is working well when I have to login just once as root user but when required to login twice (once with normal user and then with sudo) then I have to use the below solution. – Asad Javed Oct 18 '21 at 09:29

1 Answers1

-1

So the solution to my above problem was:

def sshOpensips(ip):
        warnings.filterwarnings(action='ignore',module='.*paramiko.*')
        command = ('docker exec opensips /bin/bash -c \"systemctl restart opensips\" && echo Opensips has restarted')
        client = pm.SSHClient()
        client.set_missing_host_key_policy(pm.AutoAddPolicy())
        pk = pm.RSAKey.from_private_key(open('/root/.ssh/id_rsa'))
        try:
                client.connect(ip, username='asad.javed', pkey=pk)
        except pm.SSHException:
                print("Connection Failed")
        session = client.get_transport().open_session()
        session.set_combine_stderr(True)
        session.get_pty()
        session.exec_command(command)
        stdin = session.makefile('wb', -1)
        stdout = session.makefile('rb', -1)
        kr.get_password("Opensips", "asad.javed")
        time.sleep(2)
        print(stdout.read().decode("utf-8"))
        for line in stdout.readlines():
                print(line.strip());

        exit_status = stdout.channel.recv_exit_status()

        if exit_status == 0:
                print("Command Executed")
        else:
                print("Error", exit_status)

        session.close()
        client.close()

For anyone who faces a similar issue in the future can use this solution to execute command in docker container. Two modules would be required for this Paramiko and Keyring.

Asad Javed
  • 57
  • 6