#因实验要求。我需要大量虚拟机来做实验。我这里用expect交互式软件写了自动化一键配置环境脚本
#我里是脚本克隆虚拟机 新建的虚拟机是没有IP的 这里是真机远程配虚拟机IP
#通过 virsh console 加虚拟主机名 可以进入没有IP的
#!/bin/bash
expect << EOF
spawn virsh console $1
expect “]” {send “\r”}
expect “:” {send “root\r”}
expect “密码:” {send “123456\r”}
expect “#” {send “nmcli connection modify eth0 ipv4.method manual ipv4.addresses $2/24 connection.autoconnect yes\r”}
expect “#” {send “nmcli connection up eth0\r”}
expect “#” {send “hostnamectl set-hostname $3\r”}
expect “#” {send “exit\r”}
EOF
sleep 1
#因实验环境有多个yum仓库 为了方便我直接先写好yum仓库文件 然后远程把虚拟机的
#/etc/yum.repos.d/下的文件干掉。然后再写好的yum仓库文件远程传过去
#scp -o StrictHostKeyChecking=no 第一次远程虚报机时免交互
#
expect << EOF
spawn scp -o StrictHostKeyChecking=no scp /root/桌面/lnmp_soft.tar.gz root@$2:/root/
expect “password” {send “123456\r”}
spawn ssh $2
expect “password” {send “123456\r”}
expect “#” {send "rm -rf /etc/yum.repos.d/\r"}
expect “#” {send “exit\r”}
spawn scp -o StrictHostKeyChecking=no scp /root/yum.repo $2:/etc/yum.repos.d/
expect “password:” {send “123456\r”}
expect “#” {send “exit\r*”}
EOF
~
#上面的yum仓库 cat /etc/yum.repos.d/yum.repo
[192.168.4.254_rhel7]
name=added from: ftp://192.168.4.254/rhel7
baseurl=ftp://192.168.4.254/rhel7
enabled=1
gpgcheck=0
[hare_MON]
name=added from: ftp://192.168.4.254/share/MON
baseurl=ftp://192.168.4.254/share/MON
gpgcheck=0
enabled=1
[192.168.4.254_share_OSD]
name=added from: ftp://192.168.4.254/share/OSD
baseurl=ftp://192.168.4.254/share/OSD
gpgcheck=0
enabled=1
[192.168.4.254_share_Tools]
name=added from: ftp://192.168.4.254/share/Tools
baseurl=ftp://192.168.4.254/share/Tools
gpgcheck=0
enabled=1