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

标签 bash shell ssh sudo aix

<分区>

我有以下内容

#!/bin/bash

USER='scott'
PASS='tiger'

ssh -t $USER@server006.web.com "sudo su - http" 

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

脚本像这样执行 grep:

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

它自己也能正常工作,但我需要成为 http 才能执行此操作。

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

如有任何想法,我们将不胜感激。 本

最佳答案

尝试

ssh -t $USER@server006.web.com 'sudo -u http grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F"|" "{print $2}" >> log.log'

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

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

关于bash - SSH 到服务器,Sudo su - 然后在 bash 中运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22812853/

相关文章:

bash - 为什么管道中的重定向在 Bash 和 Zsh 中表现不同?

java - 使用 shell 脚本执行 .jar 文件

bash - 为什么需要这个斜杠?

perl - 将一个文件拆分成多个文件

.htaccess - 基于协议(protocol)限制对目录的访问

bash - 在 bash 文件中的两行之间添加一行

linux - 从在 Linux KornShell 脚本中运行的 SAS 程序收集返回码和标准输出字符串

linux - 在日志文件中查找间隔

linux - 从任何服务器的脚本动态ssh

Git可以 pull 好但不能推送 "could not read username"