0

I am trying to do a ssh to my local machine - 127.0.0.1, which works fine. Next, I am trying to run two commands through ssh client. However, I see that the next command fails. I could see that my tap device is created. However, the tap device is not turned up. Here is my code. I tried the ifconfig and it works fine. However, it is the sudo commands that is creating a problem.

self.serverName is 127.0.0.1

  def configure_tap_iface(self):
        ssh = paramiko.SSHClient()
        print('SSH on to PC')
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(self.serverName, username='zebra', password='Zebra@2018')
        stdin, stdout, stderr = ssh.exec_command('ifconfig')
        #print(stdout.read())
        session = ssh.get_transport().open_session()
        session.get_pty()
        session.exec_command('sudo ip address add 192.168.0.1/24 dev cloud_tap && sudo ip link set cloud_tap up')
        session.close()
        time.sleep(3)
        ssh.close()
4
  • If you do the same with some simple commands (not sudo), does it work? Commented Aug 22, 2018 at 5:48
  • yes, the simple command works without sudo Commented Aug 22, 2018 at 6:14
  • Is your account configured to run sudo passwordless? Can you run sudo -l from Paramiko, what does it display? Commented Aug 22, 2018 at 10:29
  • env_reset, mail_badpass, secure_path=/usr/local/sbin\\:/usr/local/bin\\:/usr/sbin\\:/usr/bin\\:/sbin\\:/bin\\:/snap/bin\n\nUser bali may run the following commands on pc-ubu:\n (root) NOPASSWD: /sbin/ip\n (ALL : ALL) ALL\n' Commented Aug 22, 2018 at 10:49

1 Answer 1

1

You can use sudo sh -c 'commands' to run multiple shell commands in a single sudo invocation.

session.exec_command("sudo sh -c 'ip address add 192.168.0.1/24 dev cloud_tap && ip link set cloud_tap up'")
Sign up to request clarification or add additional context in comments.

2 Comments

what does sh -c means? is sh for shell ? how about -c switch?
sh -c 'commands; more commands' says to read the script from the command line instead of from a script file. Yes, sh is the standard Bourne shell which is generally used for noninteractive scripts even if you have a different interactive shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.