Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

shell - How can i redirect unwanted output on bash login over ssh?

I've got a script, that will use ssh to login to another machine and run a script there. My local script will redirect all the output to a file. It works fine in most cases, but on certain remote machines, i am capturing output that i don't want, and it looks like it's coming from stderr. Maybe because of the way bash is processing entries in its start-up files, but this is just speculation.

Here is an example of some unwanted lines that end up in my file.

which: no node in (/usr/local/bin:/bin:/usr/bin)

stty: standard input: Invalid argument

My current method is to just strip the predictable output that i don't want, but it feels like bad practice.

How can i capture output from only my script?

Here's the line that runs the remote script.

ssh -p 22 -tq user@centos-server "/path/to/script.sh" > capture

The ssh uses authorized_keys.

Edit: In the meantime, i'm going to work on directing the output from my script on machine B to a file and then copying it to A via scp and deleting it on B. But i would really like to be able to suppress the output completely, because when i run the script on machine A, it makes the output difficult to read.

question from:https://stackoverflow.com/questions/65845216/how-can-i-redirect-unwanted-output-on-bash-login-over-ssh

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To build on your comment on Raman's answer. Have you tried supressing .bashrc and .bash_profile as shown below?

ssh -p 22 -tq user@centos-server "bash --norc --noprofile /path/to/script.sh" > capture

If rc-files is the problem on some servers you should try and fix the broken rc-files instead of your script/invocation since it'll affect all (non-interactive) logins.

Try running ssh user@host 'grep -ls "which node" .*' on all your servers to find if they have "which node" in any dotfiles as indicated by your error message.

Another thing to look out for is your shebang. You tag this as bash and write CentOS but on a Debian/Ubuntu server #!/bin/sh gives you dash instead of (sh-compatible) bash.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...