subprocess.Popen for “ssh sudo -u user -i” is failing with “sudo: sorry, you must have a tty to run sudo”
问题 I want to ssh into a remote server, change user then execute a script. I'm using subprocess to do this but it appears sudo -u userB -i is not changing user. HOST = 'remote_server' USER = 'userA' CMD = ' whoami; sudo -u userB -i; whoami' ssh = subprocess.Popen(['ssh', '{}@{}'.format(USER, HOST),CMD], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = ssh.stdout.readlines() if not result: err = ssh.stderr.readlines() print('ERROR: {}'.format(err)) else: print "success" print