Bali Vinayak
Bali Vinayak

Reputation: 289

How to run two sudo commands subsequently in python paramiko -SSH client linux?

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()

Upvotes: 0

Views: 493

Answers (1)

tripleee
tripleee

Reputation: 189397

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

Upvotes: 1

Related Questions