SSH 到服务器,Sudo su - 然后在 bash 中运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22812853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:46:17  来源:igfitidea点击:

SSH to server, Sudo su - then run commands in bash

bashshellsshsudoaix

提问by Ben Coughlan

I have the following

我有以下

#!/bin/bash

USER='scott'
PASS='tiger'

ssh -t [email protected] "sudo su - http" 

This Works, but I was trying to get it to run a script afterwards, and if I do, using -c or <

这有效,但我试图让它在之后运行脚本,如果我这样做,使用 -c 或 <

The script does a grep like this:

该脚本执行这样的 grep:

grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F'|' '{print }' >> log.log

This also works fine on it's own, but I need to be http to do it.

这本身也可以正常工作,但我需要成为 http 才能做到这一点。

I cannot SCP the output of the script back to server001 either, so I'm stuck here,

我也无法将脚本的输出 SCP 回 server001,所以我被困在这里,

Any ideas would be relay appreciated. Ben

任何想法将不胜感激。本

采纳答案by Patrik Kullman - Neovici

Try

尝试

ssh -t [email protected] 'sudo -u http grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F"|" "{print }" >> log.log'

Sudo already runs the command as a different user to there's no need to su again.

Sudo 已经以不同的用户身份运行该命令,无需再次 su。

Only reason to do sudo su is to have a fast way to start a new shell with another user.

执行 sudo su 的唯一原因是有一个快速的方法来与另一个用户启动一个新的 shell。

回答by glenn Hymanman

You probably want sudo -uinstead of sudo su -:

你可能想要sudo -u而不是sudo su -

ssh -t [email protected] sudo -u http script 

回答by wbt11a

Guess I'm late to the party. My solution:

猜猜我参加聚会迟到了。我的解决方案:

ssh -t [email protected] "sudo cat /etc/shadow"

and replace cat /etc/shadow with your desired program.

并将 cat /etc/shadow 替换为您想要的程序。