Unix:控制台输出上的 Grep
这是我在 stackoverflow 上的第一个问题! 我想要一个可以在控制台输出上运行 grep 的 unix 脚本。这是我的脚本的作用: 1. Telnet到远程服务器(我已经成功完成了这部分) 2. 登录成功后,远程服务器在控制台上显示输出信息。我需要在该控制台输出上运行 grep (需要帮助)
因此,我需要一个脚本来在控制台上出现的输出上运行 grep 。
有什么想法吗?
谢谢, 普内特
This is my first question on stackoverflow!
I want to have a unix script that will run grep on the console output. Here is what my script does:
1. Telnet into a remote server (I have done this part successfully)
2. On successful login, the remote server displays outputs information on the console. I need to run grep on that console output (need help with this)
So, I need a script to run grep on the output appearing on the console.
Any thoughts??
Thanks,
Puneet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试过 I/O 重定向吗?您可以执行
然后在该文件上运行 grep,或者直接通过 grep 管道输出,如下所示
请参阅
Have you tried I/O redirection? You could either do
and then run grep on that file, or just directly pipe the output through grep like so
See this article or google around for similar. There are probably thousands of good articles about this around the web.
请改用 SSH。它更安全并且更容易编写脚本。
通过适当的密钥设置,它甚至不会提示您输入密码。
Use SSH instead. It's more secure and far easier to script.
with appropriate key setup, it won't even prompt you for a password.
我建议使用 netcat (nc),而不是 telnet。然后,您可以通过标准输入传递登录凭据并 grep 标准输出(nc 在标准输出上打印服务器发送的任何内容)。
Instead of telnet, I would suggest using netcat (nc). You could then pass your login credentials via standard input and grep the standard output (nc prints anything sent by the server on standard output).
您想要做的可能是使用管道。您可能可以在上面的答案中看到它,它是您在命令中看到的
|
符号。根据布局的不同,在键盘上可能很难找到。 (我不得不承认它并不经常使用)。管道将重定向一个命令的输出。他们不会将其发送到控制台,而是将其作为另一个命令的输入发送。
cmd1 | grep foo
相当于在cmd1
的输出上运行grep foo
(您可以用 netstat 命令替换 cmd1)。最后一件事是,您可以拥有任意数量的管道。例如,在我的机器上,我可以运行 ls -ltr |尾部-1 | awk '{print $9}' | grep foo 在上次修改的文件中查找单词
foo
。What you want to do is probably using a pipe. You can probably see it in the above answers it's the
|
sign you see in the command. It may be difficult to locate on your keyboard, depending on the layout. (I have to admit it is not very often used).Pipes will redirect the output of one command. Instead of sending it to the console, they will send it as an input of another command.
cmd1 | grep foo
is equivalent to runninggrep foo
on the output ofcmd1
(you can replace cmd1 by your netstat command).One last thing is that you can have as many pipes as you want. For instance on my machine I can run
ls -ltr | tail -1 | awk '{print $9}' | grep foo
to look for the wordfoo
in the last modified file.