Considerations for using SSH-free remote command execution in a loop in Shell programming

Source: Internet
Author: User
Tags stdin

Prerequisite: Configure your own machine ssh password-free login
#!/bin/bash
Cat << ' EOD ' > list
1
2
3
4
EOD

While Read line
Do
if SSH [email protected] "test-f/tmp/a.sh"; Then #配置了免密登录, no password is entered here
echo "$line file exist."
Else
echo "$line file not exist."
Fi
Done < list

Question: It is supposed that there are 4 lines in the list file, and this while loop should be executed 4 times before, why is this only executed once?

The answers are as follows:

Check the SSH options and discover

-N redirects stdin from/dev/null (actually, prevents reading from stdin).  This must was used when SSH was run in the background. A
Common trick is the use of this to run X11 programs in a remote machine. For example, Ssh-n shadows.cs.hut.fi Emacs & would start
An emacs on shadows.cs.hut.fi, and the X11 connection would be a automatically forwarded over an encrypted channel. The SSH pro-
Gram'll is put in the background. (This does isn't work if SSH needs to ask for a password or passphrase; see also The-f
option.)

It can be said that this option is specifically designed to solve this problem. Use/dev/null to SSH input, prevent SSH from reading the local standard input content.

#要想使本脚本执行次数正常, the SSH command needs to add the-N option .

42028359

Considerations for using SSH-free remote command execution in a loop in Shell programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.