bash 用于运行命令并将命令输出作为报告在电子邮件中发送的 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19327129/
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
Shell script to run a command and send the commands output as a report in email
提问by rond
I am trying to write a shell script which will run a command to ssh into multiple machines and store the output in a variable and send it as a report via email.Here is what I have in the script as of now:
我正在尝试编写一个 shell 脚本,该脚本将运行一个命令以 ssh 进入多台机器并将输出存储在一个变量中,并通过电子邮件将其作为报告发送。这是我目前在脚本中的内容:
#!/bin/bash
DcEmitterConn='yinst ssh -H test.out "netstat -a | grep ES | grep 25019 | wc"'
SUBJECT="DC-Connections"
EMAIL="[email protected]"
EMAILMESSAGE="report.out"
echo $DcEmitterConn> $EMAILMESSAGE
#send email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL"< $EMAILMESSAGE
After executing the above command in the script it would ask me for a password and then would print the requested output. The problem i am facing in the above script is that I am not able to store the command output in the variable and print it in the email body. Can someone please let me know if I am missing something.
在脚本中执行上述命令后,它会要求我输入密码,然后打印请求的输出。我在上述脚本中面临的问题是我无法将命令输出存储在变量中并将其打印在电子邮件正文中。如果我遗漏了什么,有人可以告诉我。
the output would look something like this:
输出看起来像这样:
[email protected]'s password: (yinst-pw)
40 240 3560
[email protected]'s password: (supplied by yinst-pw)
50 300 4450
Thanks in advance!
提前致谢!
回答by damienfrancois
You should put double quotes around $DcEmitterConn on line 8. And you can avoid the temporary file:
你应该在第 8 行的 $DcEmitterConn 周围加上双引号。你可以避免临时文件:
SUBJECT="DC-Connections"
EMAIL="[email protected]"
yinst ssh -H test.out "netstat -a | grep ES | grep 25019 | wc" | /bin/mail -s "$SUBJECT" "$EMAIL"