【云巅-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:


示例2:

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值