Shell为新创建的KVM虚拟机配置IP地址,修改主机名
#!/bin/bash#AUTHOR:AN#DATE:2019-04-19#MODIFY:2019-05-06#DESCRIBE:连接KVM虚拟机,虚拟机初始密码为123456,设置IP地址,主机名#PEOBLEM:1.检测网卡是否存在Local_Dir=`pwd`#当前目录#加载函数库if [ -f "$Local_Dir/myfunction.lib" ];thens...
·
函数库链接地址:https://blog.csdn.net/anqixiang/article/details/89964048
#!/bin/bash
#AUTHOR:AN
#VERSION:1.1.0
#DATE:2019-04-19
#MODIFY:2019-05-06 2019-05-15
#DESCRIBE:连接KVM虚拟机,虚拟机初始密码为123456,设置IP地址,主机名
#PEOBLEM:1.检测网卡是否存在
######################定义变量######################
Nfs_Dir=/cloud_nsd #NFS共享目录
Script_Path=$Nfs_Dir/shell #脚本路径
Kvm_Pwd=123456 #虚拟机密码
#加载函数库
if [ -f "$Script_Path/myfunction.lib" ];then
source $Script_Path/myfunction.lib
else
echo -e "\033[31m函数库不存在\033[0m" && exit $NOEXIST
fi
#添加网卡,并设置主机名和IP地址
ADD_NIC(){
YUM expect
expect << EOF
spawn virsh console $1
expect " " {send "\r"}
expect ":" {send "root\r"}
expect "密码:" {send "$Kvm_Pwd\r"}
expect "#" {send "hostnamectl set-hostname $3\r"}
expect "#" {send "nmcli connection add type ethernet con-name $2 ifname $2\r"}
expect "#" {send "nmcli connection modify $2 ipv4.method manual ipv4.addresses $4 connection.autoconnect yes\r"}
expect "#" {send "nmcli connection up $2 \r"}
expect "#" {send "exit\r"}
EOF
}
#配置IP地址
CONF_IP(){
YUM expect
expect << EOF
spawn virsh console $1
expect " " {send "\r"}
expect ":" {send "root\r"}
expect "密码:" {send "$Kvm_Pwd\r"}
expect "#" {send "hostnamectl set-hostname $2\r"}
expect "#" {send "nmcli connection modify $3 ipv4.method manual ipv4.addresses $4 connection.autoconnect yes\r"}
expect "#" {send "nmcli connection up $3 \r"}
expect "#" {send "exit\r"}
EOF
}
#帮助信息
HELP(){
cat << EOF
virsh_ip version 1.1.0
Usage: virsh_ip [-h] [-a 虚拟机编号 网卡名]
[-num 虚拟机编号 主机名 网卡名 IP地址]
=======================================================================
optional arguments:
-h 提供帮助信息
-num 虚拟机编号
-a 添加网卡
EXAMPLE:
bash virsh_ip.sh -num 10 web1 eth0 192.168.4.1/24
bash virsh_ip.sh -a 10 eth1 web1 192.168.4.1/24
EOF
}
#############################主程序#############################
[ $# -eq 0 ] && HELP
while getopts :hnum::a: ARGS
do
case $ARGS in
h)
HELP;;
nu|m)
Name=rh7_node$OPTARG #KVM虚拟机名字
CONF_IP $Name $3 $4 $5;;
a)
Name=rh7_node$OPTARG
ADD_NIC $Name $3 $4 $5;;
\?)
cecho 31 "Invalid option:bash `basename $0` [-h]"
esac
done
更多推荐
已为社区贡献20条内容
所有评论(0)