【云巅-Lens】-Shell脚本示例(Lens客户端协助配置网络)

大家可以参考使用,对于Linux小白来讲可以省不少事,希望对大家有帮助~


Shell脚本源码如下:

#! /bin/bash

## Clear the current screen

clear

echo " "

function highlight_green()

{

  echo -ne '\E[1;32;40m'"$1"

  echo -e '\E[0m'""

}

function highlight_red()

{

echo -ne '\E[1;31;40m'"$1"

echo -e '\E[0m'""

}

highlight_green "*** Set Network Configuration ***"

 

## Get Network Configure File Name

filename=$(ls /etc/sysconfig/network-scripts/ | grep ifcfg- | grep -v lo)

#echo "$portname"

## Get Network Port Name

portname=$(ls /etc/sysconfig/network-scripts/ | grep ifcfg- | grep -v lo | cut -d - -f 2)

#echo $portname

## Set Network Type

read -p 'Set Network As(Default is "static")[ dhcp / static ] ' networktype

if [[ "$networktype" == "dhcp" || "$networktype" == "DHCP" ]];then

bootproto=dhcp

else

bootproto=static

fi

## Write General Configure to The File

echo "NM_CONTROLED=yes" > /etc/sysconfig/network-scripts/$filename

echo "DEVICE=$portname" >> /etc/sysconfig/network-scripts/$filename

echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/$filename

echo "BOOTPROTO=$bootproto" >> /etc/sysconfig/network-scripts/$filename

## Write Static IP Address to The File

if [[ "$bootproto" != "dhcp" ]];then

    read -p 'Please Input IP Address [ xxx.xxx.xxx.xxx ] ' ipaddress

    read -p 'Please Input Netmask    [ 255.255.255.0   ] ' netmask

    read -p 'Please Input Gateway    [ xxx.xxx.xxx.xxx ] ' gateway

    read -p 'Please Input DNS1       [ xxx.xxx.xxx.xxx ] ' dns1

    read -p 'Please Input DNS2       [ xxx.xxx.xxx.xxx ] ' dns2

    if [[ $ipaddress == "" ]];then

        echo "IPADDR=" >> /etc/sysconfig/network-scripts/$filename

    else

        echo "IPADDR=$ipaddress" >> /etc/sysconfig/network-scripts/$filename

    fi

    if [[ $netmask == "" ]];then

    echo "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/$filename

    else

    echo "NETMASK=$netmask" >> /etc/sysconfig/network-scripts/$filename

    fi

    if [[ $gateway == ""  ]];then

        echo "GATEWAY=" >> /etc/sysconfig/network-scripts/$filename

    else

        echo "GATEWAY=$gateway" >> /etc/sysconfig/network-scripts/$filename

    fi

    if [[ $dns1 == ""  ]];then

        echo "DNS1=" >> /etc/sysconfig/network-scripts/$filename

    else

        echo "DNS1=$dns1" >> /etc/sysconfig/network-scripts/$filename

    fi

    if [[ $dns2 == ""  ]];then

        echo "DNS2=" >> /etc/sysconfig/network-scripts/$filename

    else

        echo "DNS2=$dns2" >> /etc/sysconfig/network-scripts/$filename

    fi

fi

## Show Network Configuration

echo ""

highlight_green "*** Show Network Configuration *** "

cat /etc/sysconfig/network-scripts/$filename

echo ""

## Restart Network Service

service NetworkManager restart >> /dev/null 2>&1

sleep 5

read -n 1 -p "Press [ $(highlight_green "1") ] to check network, Other key to cancel…” checkflag

echo ""

if [[ $checkflag == "1" ]];then

echo ""

read -p "Input iConn Server IP Address: " serverip

pingresult=$(ping -c 3 $serverip 2>&1 > /dev/null;echo $?)

  if [[ $pingresult == "0" ]];then

  echo "Connect to Server $(highlight_green "Successful")!"

  else

  echo "Connect to Server $(highlight_red "Failed"), Please Check Network Configuration..."

  fi

fi

echo ""

read -n 1 -p "Press [ $(highlight_green "Alt + F2") ] to Change to Lens Login UI..."

clear


【使用方法】

1)将以上代码保存为network.sh, 源文件下载地址:http://pan.baidu.com/s/1o6mT3T4

2)将network.sh文件复制到/root/目录下;

3)执行脚本

#sh network.sh

4)根据提示选择获取IP地址的方式,输入dhcp,或者static,默认static;

5)设置完IP地址后,根据提示输入需要连接的iConn服务器地址,测试网络是否连通;

6)如果连通,提示Successful,如失败,提示Failed;

7)设置完成后提示Alt+F2返回客户端登录界面。

如下示例:

示例1:

【云巅-Lens】-Shell脚本示例(Lens客户端协助配置网络)_第1张图片
示例2:
【云巅-Lens】-Shell脚本示例(Lens客户端协助配置网络)_第2张图片
 

 

你可能感兴趣的:(云巅学习)