Today production to batch connect the server and execute the command: Write all the IP to a iplist file, then use while read and for to traverse the stitching SSH command found two problems: SSH connection parameter-N and while read and for read the difference
Script
IPList
192.168.4.12
192.168.4.13
192.168.4.14
tongjidf.sh
#!/bin/bash
cmd= "Df-m | grep app "
While Read line
Do
SSH [email protected]${line} $cmd
Done <./iplist
The results of the execution found that only one address was connected 192.168.4.12, the following address was not executed
and changed it into a for.
#!/bin/bash
Cmd= "Df-m|grep App"
For IPs in ' Cat IPList '
Do
SSH [email protected]${line} $cmd
Done
This script can be implemented by connecting all of the above addresses and then executing commands.
Finally found a saying that is for is a row of reads, while the while read line is a read into the memory
The second is that SSH reads the local content by default, and the-n parameter blocks it, but reads it from/dev/null/, so that SSH is connected in the form of a while-read line
- 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'll be automatically forwarded over an encrypted C Hannel. 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
SSH traversal IPs for connection