ssh login as user and change to root, without sudo












1














I have the following task:




  • the command has to be run as root on server remotely in bash script over ssh and the command output has to be fetched in variable.

  • logging over ssh as root is disabled.

  • sudo on server is disabled, so I have to use su.

  • EDIT: since I want to make it as automated as possible in bash, the password has to be stored inside command


I have Googled for days, but it seems that I cannot find a solution for this.



Solution proposed here: ssh to server and switch user and change a directory



    ssh -t username@hostname "sudo su - otheruser -c "cd /path/to/directory && command""


does not work because sudo is disabled on server:



Does anyone have a solution to this?










share|improve this question




















  • 1




    To switch to another user, you need not use sudo su - username, you can plainly use su - username.
    – Mukesh Sai Kumar
    Apr 6 at 16:55










  • Thanks for your answer @MukeshSaiKumar. Do you really think that I have "Googled for days" just to find "su" command? Also have you at least tried this? It is not working!
    – spaceman117X
    Apr 9 at 7:26










  • su is already a setuid binary, so it must be able to switch to any user which you demand it to, with proper authentication. If su doesn't seem to be working, check if it has the setuid bit set.
    – Mukesh Sai Kumar
    Apr 21 at 18:32
















1














I have the following task:




  • the command has to be run as root on server remotely in bash script over ssh and the command output has to be fetched in variable.

  • logging over ssh as root is disabled.

  • sudo on server is disabled, so I have to use su.

  • EDIT: since I want to make it as automated as possible in bash, the password has to be stored inside command


I have Googled for days, but it seems that I cannot find a solution for this.



Solution proposed here: ssh to server and switch user and change a directory



    ssh -t username@hostname "sudo su - otheruser -c "cd /path/to/directory && command""


does not work because sudo is disabled on server:



Does anyone have a solution to this?










share|improve this question




















  • 1




    To switch to another user, you need not use sudo su - username, you can plainly use su - username.
    – Mukesh Sai Kumar
    Apr 6 at 16:55










  • Thanks for your answer @MukeshSaiKumar. Do you really think that I have "Googled for days" just to find "su" command? Also have you at least tried this? It is not working!
    – spaceman117X
    Apr 9 at 7:26










  • su is already a setuid binary, so it must be able to switch to any user which you demand it to, with proper authentication. If su doesn't seem to be working, check if it has the setuid bit set.
    – Mukesh Sai Kumar
    Apr 21 at 18:32














1












1








1


1





I have the following task:




  • the command has to be run as root on server remotely in bash script over ssh and the command output has to be fetched in variable.

  • logging over ssh as root is disabled.

  • sudo on server is disabled, so I have to use su.

  • EDIT: since I want to make it as automated as possible in bash, the password has to be stored inside command


I have Googled for days, but it seems that I cannot find a solution for this.



Solution proposed here: ssh to server and switch user and change a directory



    ssh -t username@hostname "sudo su - otheruser -c "cd /path/to/directory && command""


does not work because sudo is disabled on server:



Does anyone have a solution to this?










share|improve this question















I have the following task:




  • the command has to be run as root on server remotely in bash script over ssh and the command output has to be fetched in variable.

  • logging over ssh as root is disabled.

  • sudo on server is disabled, so I have to use su.

  • EDIT: since I want to make it as automated as possible in bash, the password has to be stored inside command


I have Googled for days, but it seems that I cannot find a solution for this.



Solution proposed here: ssh to server and switch user and change a directory



    ssh -t username@hostname "sudo su - otheruser -c "cd /path/to/directory && command""


does not work because sudo is disabled on server:



Does anyone have a solution to this?







ssh su






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 9 at 7:18

























asked Apr 6 at 16:25









spaceman117X

3618




3618








  • 1




    To switch to another user, you need not use sudo su - username, you can plainly use su - username.
    – Mukesh Sai Kumar
    Apr 6 at 16:55










  • Thanks for your answer @MukeshSaiKumar. Do you really think that I have "Googled for days" just to find "su" command? Also have you at least tried this? It is not working!
    – spaceman117X
    Apr 9 at 7:26










  • su is already a setuid binary, so it must be able to switch to any user which you demand it to, with proper authentication. If su doesn't seem to be working, check if it has the setuid bit set.
    – Mukesh Sai Kumar
    Apr 21 at 18:32














  • 1




    To switch to another user, you need not use sudo su - username, you can plainly use su - username.
    – Mukesh Sai Kumar
    Apr 6 at 16:55










  • Thanks for your answer @MukeshSaiKumar. Do you really think that I have "Googled for days" just to find "su" command? Also have you at least tried this? It is not working!
    – spaceman117X
    Apr 9 at 7:26










  • su is already a setuid binary, so it must be able to switch to any user which you demand it to, with proper authentication. If su doesn't seem to be working, check if it has the setuid bit set.
    – Mukesh Sai Kumar
    Apr 21 at 18:32








1




1




To switch to another user, you need not use sudo su - username, you can plainly use su - username.
– Mukesh Sai Kumar
Apr 6 at 16:55




To switch to another user, you need not use sudo su - username, you can plainly use su - username.
– Mukesh Sai Kumar
Apr 6 at 16:55












Thanks for your answer @MukeshSaiKumar. Do you really think that I have "Googled for days" just to find "su" command? Also have you at least tried this? It is not working!
– spaceman117X
Apr 9 at 7:26




Thanks for your answer @MukeshSaiKumar. Do you really think that I have "Googled for days" just to find "su" command? Also have you at least tried this? It is not working!
– spaceman117X
Apr 9 at 7:26












su is already a setuid binary, so it must be able to switch to any user which you demand it to, with proper authentication. If su doesn't seem to be working, check if it has the setuid bit set.
– Mukesh Sai Kumar
Apr 21 at 18:32




su is already a setuid binary, so it must be able to switch to any user which you demand it to, with proper authentication. If su doesn't seem to be working, check if it has the setuid bit set.
– Mukesh Sai Kumar
Apr 21 at 18:32










4 Answers
4






active

oldest

votes


















2














Perhaps somewhat off topic but this could be achieved with Python and the paramiko module:



#!/usr/bin/python2

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', port=22, username='user', password='pass')

stdin, stdout, stderr = ssh.exec_command('su')
stdin.write('root_password_goes_heren')

[ add extra code here to execute a command ]

stdin.flush()
print (stdout.readlines())
ssh.close()


It should be noted that storing passwords in script generally is a bad idea from a security perspective. Make sure you have proper permissions set to the script (e.g. chmod 740)






share|improve this answer





















  • Thanks! looks nice, I heard about paramiko but I have not used it so far. I will try this. I am always interested in trying some alternative methods.
    – spaceman117X
    Apr 9 at 7:35










  • I just tried this. I put both regular user and root password. However, when I put touch /tmp/test/ into the [ add extra code here to execute a command ] line it looks like that /tmp/test is still owned by regular user and not root. I have also tried to do it in one line like this stdin, stdout, stderr = ssh.exec_command('su root -c "touch /tmp/test"') stdin.write('passwordn') then /tmp/test is not created. Any ideas?
    – spaceman117X
    Apr 9 at 9:49










  • When I'm home later today I will see what I can come up with for you. I should have something similar somewhere that you could use.
    – Jeroen - IT Nerdbox
    Apr 9 at 14:06



















2














Login via SSH (as unprivileged user), then run the command su without any arguments to change to the root user. You will need the root password to do this. Then run whatever commands you want to run.
EDIT: If you want to do it in one line, you can use the following:
ssh username@hostname "su -c "code_here""

If this doesn't work, make sure the root password is enabled by running passwd as root. This will ask you for a new root password.
Extra: To run a command as another user besides root (Please note that this requires the target user's password):
ssh username@hostname "su - username_of_target -c "code_here""






share|improve this answer























  • Thanks for your answer @Giraffer and everyone who voted this up. Have you at least tried this? It is not working! it says: user@server ~ $ ssh admin@server "su - root -c "code_here""admin@server.com's password: su: must be run from a terminal Please do not post stuffs that are not checked. Everyone can copy/paste from google!
    – spaceman117X
    Apr 9 at 7:30










  • I have checked, and it worked. No copy/paste from google here. Did you run passwd as root before hand?
    – Giraffer
    Apr 10 at 2:28










  • The issue here can be different versions of the su command, as I found on another websites. But then I would need to upgrade su on all servers. Thats why python solution might be better and easier in this case.
    – spaceman117X
    Apr 10 at 6:38



















0














So, after 4 hours of additional web crawling, I finally made it! Big thanks for @jeroen-it-nerdbox for giving me insights on this:



Task was to take data from smartctl (which requires root credentials), from server with disabled ssh-root and disabled sudo.
This will of course also work with sudo instead of su.



here is the full workable code in Python with Paramiko implementation.



#!/usr/bin/python2

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('rootserver.domain.com', port=22, username='admin', password='adminpass')

stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output"', get_pty=True)
stdin.write('rootpassn')

stdin.flush()
print (stdout.readlines())
ssh.close()





share|improve this answer





























    0














    You can also used paramiko invoke shell command for interactive session.
    https://www.youtube.com/watch?v=lLKdxIu3-A4



    enter code here

    import paramiko
    from paramiko.channel import Channel
    import time
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('ip', port=22, username='non-root', password='non-root-password')

    channel:Channel = ssh.invoke_shell()
    print(type(channel))
    channel_data = str()

    while True:
    if channel.recv_ready():
    time.sleep(2)
    channel_data += str(channel.recv(999))
    else:
    continue

    channel.send("whoamin")
    time.sleep(2)
    channel_data += str(channel.recv(999))

    channel.send("sun")
    time.sleep(5)
    channel_data += str(channel.recv(999))

    # if "Password" in channel_data:
    channel.send("rootpaaswordn")
    time.sleep(2)
    channel_data += str(channel.recv(999))

    channel.send("whoamin")
    time.sleep(2)
    channel_data += str(channel.recv(999))

    # channel_data += str(channel.recv(999))
    break


    print(channel_data)






    share|improve this answer








    New contributor




    Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436013%2fssh-login-as-user-and-change-to-root-without-sudo%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Perhaps somewhat off topic but this could be achieved with Python and the paramiko module:



      #!/usr/bin/python2

      import paramiko

      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('127.0.0.1', port=22, username='user', password='pass')

      stdin, stdout, stderr = ssh.exec_command('su')
      stdin.write('root_password_goes_heren')

      [ add extra code here to execute a command ]

      stdin.flush()
      print (stdout.readlines())
      ssh.close()


      It should be noted that storing passwords in script generally is a bad idea from a security perspective. Make sure you have proper permissions set to the script (e.g. chmod 740)






      share|improve this answer





















      • Thanks! looks nice, I heard about paramiko but I have not used it so far. I will try this. I am always interested in trying some alternative methods.
        – spaceman117X
        Apr 9 at 7:35










      • I just tried this. I put both regular user and root password. However, when I put touch /tmp/test/ into the [ add extra code here to execute a command ] line it looks like that /tmp/test is still owned by regular user and not root. I have also tried to do it in one line like this stdin, stdout, stderr = ssh.exec_command('su root -c "touch /tmp/test"') stdin.write('passwordn') then /tmp/test is not created. Any ideas?
        – spaceman117X
        Apr 9 at 9:49










      • When I'm home later today I will see what I can come up with for you. I should have something similar somewhere that you could use.
        – Jeroen - IT Nerdbox
        Apr 9 at 14:06
















      2














      Perhaps somewhat off topic but this could be achieved with Python and the paramiko module:



      #!/usr/bin/python2

      import paramiko

      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('127.0.0.1', port=22, username='user', password='pass')

      stdin, stdout, stderr = ssh.exec_command('su')
      stdin.write('root_password_goes_heren')

      [ add extra code here to execute a command ]

      stdin.flush()
      print (stdout.readlines())
      ssh.close()


      It should be noted that storing passwords in script generally is a bad idea from a security perspective. Make sure you have proper permissions set to the script (e.g. chmod 740)






      share|improve this answer





















      • Thanks! looks nice, I heard about paramiko but I have not used it so far. I will try this. I am always interested in trying some alternative methods.
        – spaceman117X
        Apr 9 at 7:35










      • I just tried this. I put both regular user and root password. However, when I put touch /tmp/test/ into the [ add extra code here to execute a command ] line it looks like that /tmp/test is still owned by regular user and not root. I have also tried to do it in one line like this stdin, stdout, stderr = ssh.exec_command('su root -c "touch /tmp/test"') stdin.write('passwordn') then /tmp/test is not created. Any ideas?
        – spaceman117X
        Apr 9 at 9:49










      • When I'm home later today I will see what I can come up with for you. I should have something similar somewhere that you could use.
        – Jeroen - IT Nerdbox
        Apr 9 at 14:06














      2












      2








      2






      Perhaps somewhat off topic but this could be achieved with Python and the paramiko module:



      #!/usr/bin/python2

      import paramiko

      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('127.0.0.1', port=22, username='user', password='pass')

      stdin, stdout, stderr = ssh.exec_command('su')
      stdin.write('root_password_goes_heren')

      [ add extra code here to execute a command ]

      stdin.flush()
      print (stdout.readlines())
      ssh.close()


      It should be noted that storing passwords in script generally is a bad idea from a security perspective. Make sure you have proper permissions set to the script (e.g. chmod 740)






      share|improve this answer












      Perhaps somewhat off topic but this could be achieved with Python and the paramiko module:



      #!/usr/bin/python2

      import paramiko

      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('127.0.0.1', port=22, username='user', password='pass')

      stdin, stdout, stderr = ssh.exec_command('su')
      stdin.write('root_password_goes_heren')

      [ add extra code here to execute a command ]

      stdin.flush()
      print (stdout.readlines())
      ssh.close()


      It should be noted that storing passwords in script generally is a bad idea from a security perspective. Make sure you have proper permissions set to the script (e.g. chmod 740)







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 9 at 7:30









      Jeroen - IT Nerdbox

      1363




      1363












      • Thanks! looks nice, I heard about paramiko but I have not used it so far. I will try this. I am always interested in trying some alternative methods.
        – spaceman117X
        Apr 9 at 7:35










      • I just tried this. I put both regular user and root password. However, when I put touch /tmp/test/ into the [ add extra code here to execute a command ] line it looks like that /tmp/test is still owned by regular user and not root. I have also tried to do it in one line like this stdin, stdout, stderr = ssh.exec_command('su root -c "touch /tmp/test"') stdin.write('passwordn') then /tmp/test is not created. Any ideas?
        – spaceman117X
        Apr 9 at 9:49










      • When I'm home later today I will see what I can come up with for you. I should have something similar somewhere that you could use.
        – Jeroen - IT Nerdbox
        Apr 9 at 14:06


















      • Thanks! looks nice, I heard about paramiko but I have not used it so far. I will try this. I am always interested in trying some alternative methods.
        – spaceman117X
        Apr 9 at 7:35










      • I just tried this. I put both regular user and root password. However, when I put touch /tmp/test/ into the [ add extra code here to execute a command ] line it looks like that /tmp/test is still owned by regular user and not root. I have also tried to do it in one line like this stdin, stdout, stderr = ssh.exec_command('su root -c "touch /tmp/test"') stdin.write('passwordn') then /tmp/test is not created. Any ideas?
        – spaceman117X
        Apr 9 at 9:49










      • When I'm home later today I will see what I can come up with for you. I should have something similar somewhere that you could use.
        – Jeroen - IT Nerdbox
        Apr 9 at 14:06
















      Thanks! looks nice, I heard about paramiko but I have not used it so far. I will try this. I am always interested in trying some alternative methods.
      – spaceman117X
      Apr 9 at 7:35




      Thanks! looks nice, I heard about paramiko but I have not used it so far. I will try this. I am always interested in trying some alternative methods.
      – spaceman117X
      Apr 9 at 7:35












      I just tried this. I put both regular user and root password. However, when I put touch /tmp/test/ into the [ add extra code here to execute a command ] line it looks like that /tmp/test is still owned by regular user and not root. I have also tried to do it in one line like this stdin, stdout, stderr = ssh.exec_command('su root -c "touch /tmp/test"') stdin.write('passwordn') then /tmp/test is not created. Any ideas?
      – spaceman117X
      Apr 9 at 9:49




      I just tried this. I put both regular user and root password. However, when I put touch /tmp/test/ into the [ add extra code here to execute a command ] line it looks like that /tmp/test is still owned by regular user and not root. I have also tried to do it in one line like this stdin, stdout, stderr = ssh.exec_command('su root -c "touch /tmp/test"') stdin.write('passwordn') then /tmp/test is not created. Any ideas?
      – spaceman117X
      Apr 9 at 9:49












      When I'm home later today I will see what I can come up with for you. I should have something similar somewhere that you could use.
      – Jeroen - IT Nerdbox
      Apr 9 at 14:06




      When I'm home later today I will see what I can come up with for you. I should have something similar somewhere that you could use.
      – Jeroen - IT Nerdbox
      Apr 9 at 14:06













      2














      Login via SSH (as unprivileged user), then run the command su without any arguments to change to the root user. You will need the root password to do this. Then run whatever commands you want to run.
      EDIT: If you want to do it in one line, you can use the following:
      ssh username@hostname "su -c "code_here""

      If this doesn't work, make sure the root password is enabled by running passwd as root. This will ask you for a new root password.
      Extra: To run a command as another user besides root (Please note that this requires the target user's password):
      ssh username@hostname "su - username_of_target -c "code_here""






      share|improve this answer























      • Thanks for your answer @Giraffer and everyone who voted this up. Have you at least tried this? It is not working! it says: user@server ~ $ ssh admin@server "su - root -c "code_here""admin@server.com's password: su: must be run from a terminal Please do not post stuffs that are not checked. Everyone can copy/paste from google!
        – spaceman117X
        Apr 9 at 7:30










      • I have checked, and it worked. No copy/paste from google here. Did you run passwd as root before hand?
        – Giraffer
        Apr 10 at 2:28










      • The issue here can be different versions of the su command, as I found on another websites. But then I would need to upgrade su on all servers. Thats why python solution might be better and easier in this case.
        – spaceman117X
        Apr 10 at 6:38
















      2














      Login via SSH (as unprivileged user), then run the command su without any arguments to change to the root user. You will need the root password to do this. Then run whatever commands you want to run.
      EDIT: If you want to do it in one line, you can use the following:
      ssh username@hostname "su -c "code_here""

      If this doesn't work, make sure the root password is enabled by running passwd as root. This will ask you for a new root password.
      Extra: To run a command as another user besides root (Please note that this requires the target user's password):
      ssh username@hostname "su - username_of_target -c "code_here""






      share|improve this answer























      • Thanks for your answer @Giraffer and everyone who voted this up. Have you at least tried this? It is not working! it says: user@server ~ $ ssh admin@server "su - root -c "code_here""admin@server.com's password: su: must be run from a terminal Please do not post stuffs that are not checked. Everyone can copy/paste from google!
        – spaceman117X
        Apr 9 at 7:30










      • I have checked, and it worked. No copy/paste from google here. Did you run passwd as root before hand?
        – Giraffer
        Apr 10 at 2:28










      • The issue here can be different versions of the su command, as I found on another websites. But then I would need to upgrade su on all servers. Thats why python solution might be better and easier in this case.
        – spaceman117X
        Apr 10 at 6:38














      2












      2








      2






      Login via SSH (as unprivileged user), then run the command su without any arguments to change to the root user. You will need the root password to do this. Then run whatever commands you want to run.
      EDIT: If you want to do it in one line, you can use the following:
      ssh username@hostname "su -c "code_here""

      If this doesn't work, make sure the root password is enabled by running passwd as root. This will ask you for a new root password.
      Extra: To run a command as another user besides root (Please note that this requires the target user's password):
      ssh username@hostname "su - username_of_target -c "code_here""






      share|improve this answer














      Login via SSH (as unprivileged user), then run the command su without any arguments to change to the root user. You will need the root password to do this. Then run whatever commands you want to run.
      EDIT: If you want to do it in one line, you can use the following:
      ssh username@hostname "su -c "code_here""

      If this doesn't work, make sure the root password is enabled by running passwd as root. This will ask you for a new root password.
      Extra: To run a command as another user besides root (Please note that this requires the target user's password):
      ssh username@hostname "su - username_of_target -c "code_here""







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 6 at 22:10

























      answered Apr 6 at 16:30









      Giraffer

      997




      997












      • Thanks for your answer @Giraffer and everyone who voted this up. Have you at least tried this? It is not working! it says: user@server ~ $ ssh admin@server "su - root -c "code_here""admin@server.com's password: su: must be run from a terminal Please do not post stuffs that are not checked. Everyone can copy/paste from google!
        – spaceman117X
        Apr 9 at 7:30










      • I have checked, and it worked. No copy/paste from google here. Did you run passwd as root before hand?
        – Giraffer
        Apr 10 at 2:28










      • The issue here can be different versions of the su command, as I found on another websites. But then I would need to upgrade su on all servers. Thats why python solution might be better and easier in this case.
        – spaceman117X
        Apr 10 at 6:38


















      • Thanks for your answer @Giraffer and everyone who voted this up. Have you at least tried this? It is not working! it says: user@server ~ $ ssh admin@server "su - root -c "code_here""admin@server.com's password: su: must be run from a terminal Please do not post stuffs that are not checked. Everyone can copy/paste from google!
        – spaceman117X
        Apr 9 at 7:30










      • I have checked, and it worked. No copy/paste from google here. Did you run passwd as root before hand?
        – Giraffer
        Apr 10 at 2:28










      • The issue here can be different versions of the su command, as I found on another websites. But then I would need to upgrade su on all servers. Thats why python solution might be better and easier in this case.
        – spaceman117X
        Apr 10 at 6:38
















      Thanks for your answer @Giraffer and everyone who voted this up. Have you at least tried this? It is not working! it says: user@server ~ $ ssh admin@server "su - root -c "code_here""admin@server.com's password: su: must be run from a terminal Please do not post stuffs that are not checked. Everyone can copy/paste from google!
      – spaceman117X
      Apr 9 at 7:30




      Thanks for your answer @Giraffer and everyone who voted this up. Have you at least tried this? It is not working! it says: user@server ~ $ ssh admin@server "su - root -c "code_here""admin@server.com's password: su: must be run from a terminal Please do not post stuffs that are not checked. Everyone can copy/paste from google!
      – spaceman117X
      Apr 9 at 7:30












      I have checked, and it worked. No copy/paste from google here. Did you run passwd as root before hand?
      – Giraffer
      Apr 10 at 2:28




      I have checked, and it worked. No copy/paste from google here. Did you run passwd as root before hand?
      – Giraffer
      Apr 10 at 2:28












      The issue here can be different versions of the su command, as I found on another websites. But then I would need to upgrade su on all servers. Thats why python solution might be better and easier in this case.
      – spaceman117X
      Apr 10 at 6:38




      The issue here can be different versions of the su command, as I found on another websites. But then I would need to upgrade su on all servers. Thats why python solution might be better and easier in this case.
      – spaceman117X
      Apr 10 at 6:38











      0














      So, after 4 hours of additional web crawling, I finally made it! Big thanks for @jeroen-it-nerdbox for giving me insights on this:



      Task was to take data from smartctl (which requires root credentials), from server with disabled ssh-root and disabled sudo.
      This will of course also work with sudo instead of su.



      here is the full workable code in Python with Paramiko implementation.



      #!/usr/bin/python2

      import paramiko

      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('rootserver.domain.com', port=22, username='admin', password='adminpass')

      stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output"', get_pty=True)
      stdin.write('rootpassn')

      stdin.flush()
      print (stdout.readlines())
      ssh.close()





      share|improve this answer


























        0














        So, after 4 hours of additional web crawling, I finally made it! Big thanks for @jeroen-it-nerdbox for giving me insights on this:



        Task was to take data from smartctl (which requires root credentials), from server with disabled ssh-root and disabled sudo.
        This will of course also work with sudo instead of su.



        here is the full workable code in Python with Paramiko implementation.



        #!/usr/bin/python2

        import paramiko

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect('rootserver.domain.com', port=22, username='admin', password='adminpass')

        stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output"', get_pty=True)
        stdin.write('rootpassn')

        stdin.flush()
        print (stdout.readlines())
        ssh.close()





        share|improve this answer
























          0












          0








          0






          So, after 4 hours of additional web crawling, I finally made it! Big thanks for @jeroen-it-nerdbox for giving me insights on this:



          Task was to take data from smartctl (which requires root credentials), from server with disabled ssh-root and disabled sudo.
          This will of course also work with sudo instead of su.



          here is the full workable code in Python with Paramiko implementation.



          #!/usr/bin/python2

          import paramiko

          ssh = paramiko.SSHClient()
          ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
          ssh.connect('rootserver.domain.com', port=22, username='admin', password='adminpass')

          stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output"', get_pty=True)
          stdin.write('rootpassn')

          stdin.flush()
          print (stdout.readlines())
          ssh.close()





          share|improve this answer












          So, after 4 hours of additional web crawling, I finally made it! Big thanks for @jeroen-it-nerdbox for giving me insights on this:



          Task was to take data from smartctl (which requires root credentials), from server with disabled ssh-root and disabled sudo.
          This will of course also work with sudo instead of su.



          here is the full workable code in Python with Paramiko implementation.



          #!/usr/bin/python2

          import paramiko

          ssh = paramiko.SSHClient()
          ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
          ssh.connect('rootserver.domain.com', port=22, username='admin', password='adminpass')

          stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output"', get_pty=True)
          stdin.write('rootpassn')

          stdin.flush()
          print (stdout.readlines())
          ssh.close()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 9 at 14:06









          spaceman117X

          3618




          3618























              0














              You can also used paramiko invoke shell command for interactive session.
              https://www.youtube.com/watch?v=lLKdxIu3-A4



              enter code here

              import paramiko
              from paramiko.channel import Channel
              import time
              ssh = paramiko.SSHClient()
              ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
              ssh.connect('ip', port=22, username='non-root', password='non-root-password')

              channel:Channel = ssh.invoke_shell()
              print(type(channel))
              channel_data = str()

              while True:
              if channel.recv_ready():
              time.sleep(2)
              channel_data += str(channel.recv(999))
              else:
              continue

              channel.send("whoamin")
              time.sleep(2)
              channel_data += str(channel.recv(999))

              channel.send("sun")
              time.sleep(5)
              channel_data += str(channel.recv(999))

              # if "Password" in channel_data:
              channel.send("rootpaaswordn")
              time.sleep(2)
              channel_data += str(channel.recv(999))

              channel.send("whoamin")
              time.sleep(2)
              channel_data += str(channel.recv(999))

              # channel_data += str(channel.recv(999))
              break


              print(channel_data)






              share|improve this answer








              New contributor




              Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                0














                You can also used paramiko invoke shell command for interactive session.
                https://www.youtube.com/watch?v=lLKdxIu3-A4



                enter code here

                import paramiko
                from paramiko.channel import Channel
                import time
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect('ip', port=22, username='non-root', password='non-root-password')

                channel:Channel = ssh.invoke_shell()
                print(type(channel))
                channel_data = str()

                while True:
                if channel.recv_ready():
                time.sleep(2)
                channel_data += str(channel.recv(999))
                else:
                continue

                channel.send("whoamin")
                time.sleep(2)
                channel_data += str(channel.recv(999))

                channel.send("sun")
                time.sleep(5)
                channel_data += str(channel.recv(999))

                # if "Password" in channel_data:
                channel.send("rootpaaswordn")
                time.sleep(2)
                channel_data += str(channel.recv(999))

                channel.send("whoamin")
                time.sleep(2)
                channel_data += str(channel.recv(999))

                # channel_data += str(channel.recv(999))
                break


                print(channel_data)






                share|improve this answer








                New contributor




                Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





















                  0












                  0








                  0






                  You can also used paramiko invoke shell command for interactive session.
                  https://www.youtube.com/watch?v=lLKdxIu3-A4



                  enter code here

                  import paramiko
                  from paramiko.channel import Channel
                  import time
                  ssh = paramiko.SSHClient()
                  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                  ssh.connect('ip', port=22, username='non-root', password='non-root-password')

                  channel:Channel = ssh.invoke_shell()
                  print(type(channel))
                  channel_data = str()

                  while True:
                  if channel.recv_ready():
                  time.sleep(2)
                  channel_data += str(channel.recv(999))
                  else:
                  continue

                  channel.send("whoamin")
                  time.sleep(2)
                  channel_data += str(channel.recv(999))

                  channel.send("sun")
                  time.sleep(5)
                  channel_data += str(channel.recv(999))

                  # if "Password" in channel_data:
                  channel.send("rootpaaswordn")
                  time.sleep(2)
                  channel_data += str(channel.recv(999))

                  channel.send("whoamin")
                  time.sleep(2)
                  channel_data += str(channel.recv(999))

                  # channel_data += str(channel.recv(999))
                  break


                  print(channel_data)






                  share|improve this answer








                  New contributor




                  Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  You can also used paramiko invoke shell command for interactive session.
                  https://www.youtube.com/watch?v=lLKdxIu3-A4



                  enter code here

                  import paramiko
                  from paramiko.channel import Channel
                  import time
                  ssh = paramiko.SSHClient()
                  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                  ssh.connect('ip', port=22, username='non-root', password='non-root-password')

                  channel:Channel = ssh.invoke_shell()
                  print(type(channel))
                  channel_data = str()

                  while True:
                  if channel.recv_ready():
                  time.sleep(2)
                  channel_data += str(channel.recv(999))
                  else:
                  continue

                  channel.send("whoamin")
                  time.sleep(2)
                  channel_data += str(channel.recv(999))

                  channel.send("sun")
                  time.sleep(5)
                  channel_data += str(channel.recv(999))

                  # if "Password" in channel_data:
                  channel.send("rootpaaswordn")
                  time.sleep(2)
                  channel_data += str(channel.recv(999))

                  channel.send("whoamin")
                  time.sleep(2)
                  channel_data += str(channel.recv(999))

                  # channel_data += str(channel.recv(999))
                  break


                  print(channel_data)







                  share|improve this answer








                  New contributor




                  Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 21 mins ago









                  Chetan Kolhe

                  1




                  1




                  New contributor




                  Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Chetan Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436013%2fssh-login-as-user-and-change-to-root-without-sudo%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      サソリ

                      広島県道265号伴広島線

                      Setup Asymptote in Texstudio