简体   繁体   English

无法在PowerShell中使用Plink在其他用户(su)上运行命令

[英]Can't run commands on other user (su) using Plink in PowerShell

I'm trying to use Plink to connect to a remote UNIX server, su to another user (without password) and finally execute some commands. 我正在尝试使用Plink连接到远程UNIX服务器,将su到另一个用户(没有密码),最后执行一些命令。

$commands = @(
"su - $UNIXUSER;",
"id"
)

echo y | plink -ssh $SERVER -l $USER -pw $PWD $commands

When I execute the code above using PowerShell I get a message saying he was able to change the user, but when I execute the command id he returns the id I logged in in the first place, not the su user. 当我使用PowerShell执行上面的代码时,我收到一条消息,说他可以更改用户,但是当我执行命令id他返回我首先登录的ID,而不是su用户。

How can I execute commands using Plink within a su user? 如何在su用户中使用Plink执行命令?

This cannot work. 这行不通。

I'm surprised that you even get the id executed. 我很惊讶您甚至执行了id

The PowerShell effectively executes this. PowerShell有效执行此操作。

plink -ssh $SERVER -l $USER -pw $PWD "su - $UNIXUSER;" id

First that's a wrong syntax. 首先,这是一个错误的语法。

An even it were correct, you can provide only a single command string on plink command line. 即使正确,您也只能在plink命令行上提供单个命令字符串。 While you can combine multiple shell commands using ; 虽然您可以使用;组合多个shell命令; or & to a simple command string, that cannot work with su . &表示一个简单的命令字符串,该字符串不能与su The second command, the ls is not command of the main shell anymore. 第二个命令ls不再是主shell的命令。 That's an inner command of the su , ie a complete different stream/input/whatever you call it. 这是su的内部命令,即完全不同的流/输入/无论您叫什么。

What you need to do is to emulate user typing the commands, so that the first command gets processed by the main shell and the second command by the su executed from the main shell. 您需要做的是模拟用户键入命令,以便第一个命令由主外壳处理,第二个命令由从主外壳执行的su You can do that via an input redirection only. 您只能通过输入重定向来做到这一点。

You can do: 你可以做:

"su - $UNIXUSER`nid`nexit`nexit`n" | plink -ssh $SERVER -l $USER -pw $PWD -T

The -T was added to disable pty allocation (ie to get a non-interactive shell), to get the behavior of -m (which implies the -T ). 添加了-T以禁用pty分配(即,获取非交互式shell),以获取-m的行为(这意味着-T )。

(I do not do PowerShell. There's probably a more elegant way to present the commands than using the string.) (我不使用PowerShell。与使用字符串相比,呈现命令的方法可能更优雅。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM