【雲巔-Lens】-Shell指令碼示例(Lens客戶端協助配置網路)

戴小四發表於2018-08-01

大家可以參考使用,對於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:


示例2:

 

 

相關文章