案例1:在物理机上编写一个自动化批量管理服务器的脚本。要求用ssh命令远程给3台虚拟机全自动安装vsftpd、ftp、lftp、tree、samba、tftp-server、bind、dhcp、httpd、mariadb-server软件,用rpm  -q查询软件是否已安装,全自动启动vsftpd、smb、rpcbind、nfs、mariadb服务。

参考脚本一:

vim auto.sh

#!/bin/bash

IPS=192.168.10.

for I  in {6..9};do

(

ssh  root@${IPS}$I 'yum   install  -y   vsftpd   ftp  lftp   tree  samba  tftp-server   bind   dhcp  httpd   mariadb-server'

ssh  root@${IPS}$I 'rpm  -q   vsftpd   ftp   lftp   tree  samba   tftp-server  bind  dhcp  httpd   mariadb-server'

ssh  root@${IPS}$I  'systemctl   restart   vsftpd'

ssh  root@${IPS}$I  'systemctl   restart   smb'

ssh  root@${IPS}$I  'systemctl   restart   rpcbind'

ssh  root@${IPS}$I  'systemctl   restart   nfs'

ssh  root@${IPS}$I  'systemctl   restart   mariadb'

)&

done

wait


参考脚本二:先将需要安装的软件写到一个文件里,然后用scp传送到服务端

vim install.sh

#!/bin/bash

yum   install  -y   vsftpd   ftp  lftp   tree  samba  tftp-server   bind   dhcp  httpd   mariadb-server

rpm  -q   vsftpd   ftp   lftp   tree  samba   tftp-server  bind  dhcp  httpd   mariadb-server

systemctl   restart   vsftpd
systemctl   restart   smb
systemctl   restart   rpcbind
systemctl   restart   nfs
systemctl   restart   mariadb



vim scp.sh

#!/bin/bash

IPS=192.168.10.

for I in {6..9};do

(

scp  -r install.sh root@${IPS}$I:/opt/

ssh root@${IPS}$I 'bash /opt/install.sh'

)&

done

wait