简体   繁体   中英

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

您可以使用sudo sh -c 'commands'在单个sudo调用中运行多个shell命令。

session.exec_command("sudo sh -c 'ip address add 192.168.0.1/24 dev cloud_tap && ip link set cloud_tap up'")

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