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