fabric:连接远程服务器后,以user2身份执行命令

0 投票
1 回答
519 浏览
提问于 2025-04-17 17:26

我需要写一个Python脚本,这个脚本要能连接到一个远程服务器(连接到user1),然后执行一些命令(这部分比较简单)。接下来,它需要通过sudo su - user2切换到user2,并执行一些命令(这部分就有点问题了)。

我该怎么做才能在不让我输入密码的情况下切换到user2(我可以把密码放在脚本里的某个地方),并以user2的身份执行命令呢?

谢谢!

1 个回答

1

试试用大写的 -S。根据sudo的手册说明:

  -S          The -S (stdin) option causes sudo to read the password from
               the standard input instead of the terminal device.

撰写回答