筆記十二

########### if 語句######node

if語句mysql

if命令檢查if後面的命令或列表的退出值。若是第一個命令評估爲true/零,則運行then以後的命令列表,直至任一else。若是第一個命令評估爲false/非零,則運行else與fi之間的命令列表(反向平寫if,標記if塊的結束)。linux

語法:  if command; then command; command2; else command3; fisql

示例:shell

if test 「$USER」 != 'root' ; thenjson

echo you are not logged in as rootvim

fi數組

if [ $(id -u) -lt 9 ] ; thenbash

echo 「The number $(id -u) is less than 9!」服務器

fi


if grep 「^${USER}:」 /etc/passwd &> /dev/null ; then

echo 「${USER} is a local user on the system.」

else

echo 「${USER} is not a local user.」

fi

systemctl is-active mariadb > /dev/null 2>&1 ; MARIADB_ACTIVE=$?

systemctl is-active postgresql > /dev/null 2>&1 ; POSTGRESQL_ACTIVE=$?

if [ $MARIADB_ACTIVE -eq 0 ];then

mysql

elif [ $POSTGRESQL_ACTIVE -eq 0 ];then

psql

else

sqlite3

fi


########### case 語句#########

case語句

case語句 :它可以把變量的內容與多個模板進行匹配,再根據成功匹配的模板去決定應該執行哪

部分代碼。

case "$1" in

start)

systemctl start $2

;;

stop)

systemctl stop $2

;;

reload|restart)

systemctl stop $2

systemctl start $2

;;

*)

echo "Usage: $0 (start|stop|restart|reload)"

;;

esac


########### expect 語句#######

yum install expect -y


#!/usr/bin/expect

這一行告訴操做系統腳本里的代碼使用那一個shell來執行。

set timeout 10

設置後面全部的expect命令的等待響應的超時時間,單位爲秒。

spawn talk

spawn是expect的內部命令,做用是給後面的shell指令加個殼,用來傳遞交互指令。

expect "who"

判斷上次輸出結果裏是否包含「who」的字符串,若是有則當即返回,不然等待超時時間後返回。

send "westos\n"

執行交互動做,至關於手工輸入"westos"。

expect eof

做用是在輸出中搜索文件結束符,若是沒有這一行,腳本會當即退出,得不到正確結果。

interact

執行完成後保持交互狀態,把控制權交給控制檯,這個時候就能夠手工操做了。不然退出登陸。

$argv 參數數組

expect腳本能夠接受從bash傳遞過來的參數.可使用[lindex $argv n]得到,n從0開始,分別表示第

一個,第二個,第三個....參數。


#############環境變量 ##############

shell和腳本使用變量來存儲數據 ,有些變量能夠連同它們的內容傳遞給子進程,這些變量咱們稱之爲環境變量。

[root@server0 ~]# LINUX=redhat

[root@server0 ~]# echo $LINUX

redhat

[root@server0 ~]# bash

[root@server0 ~]# echo $LINUX

[root@server0 ~]# exit

exit

[root@server0 ~]# export LINUX

[root@server0 ~]# bash

[root@server0 ~]# echo $LINUX

redhat

[root@server0 ~]# exit

exit


使用env命令顯示全部環境變量

使用set命令現實全部本地定義的shell變量

Bash啓動腳本

在用戶登陸的時候,會運行全局變量文件/etc/profile,和用戶自定義變量文件

~/.bash_profile去初始化它們的環境變量。

/etc/profile

\_ /etc/profile.d/*.sh

~/.bash_profile

\_ ~/.bashrc

\_ /etc/bashrc


##########腳本中的別名####

alias命令能夠用來自定義屬於本身的系統命令,寫入~/.bashrc 文件永久生效。

查看別名:

# alias

alias ls='ls --color=auto'

alias mv='mv -i'

alias rm='rm -i'

...

設置別名:

# alias mycom='echo hello;hostname'

# mycomm

hello

server0.example.com

刪除別名: unalias mycomm



示例:

[root@mailwestos ~]# alias xie=vim

[root@mailwestos ~]# xie /etc/passwd

[root@mailwestos ~]# vim .bashrc 

 8 alias xie='vim'

 9 alias vi='vim'

[root@mailwestos ~]# source .bashrc 

[root@mailwestos ~]# vi /etc/passwd

[root@mailwestos ~]# vim /etc/bashrc 在末尾行添加

 alias vi='vim'

[root@mailwestos ~]# source /etc/bashrc 

[root@mailwestos ~]# su - student 

Last login: Wed Dec 14 03:28:35 EST 2016 on pts/2

[student@mailwestos ~]$ vi /etc/passwd ##能夠證實全部用戶均可以使用/etc/bashrc文件中的別名

[student@mailwestos ~]$ logout

[root@mailwestos ~]# vi /etc/bashrc ##刪除剛纔新添加的末尾行

[root@mailwestos ~]# vi .bashrc ##刪除剛纔新添加的 8,9 行

[root@mailwestos ~]# unalias xie

[root@mailwestos ~]# unalias vi

[root@mailwestos ~]# vi /etc/passwd

[root@mailwestos ~]# xie /etc/passwd

bash: xie: command not found...



############## 腳本中的函數 ########



################# 第五單元 iSCSI遠程塊存儲 ############

iSCSI概念

iSCSI(Internet SCSI)支持從客戶端(發起端)經過IP向遠程服務器上的SCSI存儲設備(目標)發送SCSI命令。iSCSI限定名稱用於肯定發起端和目標,並採用iqn.yyyy-mm.{reverse domain}:label的格式。默認狀況下,網絡通訊是至iSCSI目標上的端口3260/tcp的明文。

 iSCSI發起端:須要訪問原始SAN存儲的客戶端。

 iSCSI目標:從iSCSI服務器提供的遠程硬盤磁盤,或「目標門戶」

 iSCSI目標門戶:經過網絡向發起端提供目標的服務器。

IQN:「iSCSI限定名稱」。每一個發起端和目標須要惟一名稱進行標識,最好的作法是使用一個在Internet上可能獨一無二的名稱。


iSCSI目標配置

安裝iSCSI目標軟件包:

# yum install targetcli -y

啓動服務:

# systemctl start target

進入iSCSI目標交互式配置模式:

# targetcli

/> ls

o- / ..................................................................... [...]

o- backstores .......................................................... [...]

| o- block .............................................. [Storage Objects: 0]

| o- fileio ............................................. [Storage Objects: 0]

| o- pscsi .............................................. [Storage Objects: 0]

| o- ramdisk ............................................ [Storage Objects: 0]

o- iscsi ........................................................ [Targets: 0]

o- loopback ..................................................... [Targets: 0]

/> /backstores/block create server0.disk1 /dev/iSCSI_vg/disk1_lv

Created block storage object server0.disk1 using /dev/iSCSI_vg/disk1_lv.

/> /iscsi create iqn.2014-12.com.example:server0

Created target iqn.2014-12.com.example:server0.

Created TPG 1.

/> /iscsi/iqn.2014-12.com.example:server0/tpg1/acls create iqn.2014-12.com.example:desktop0

Created Node ACL for iqn.2014-12.com.example:desktop0

/> /iscsi/iqn.2014-12.com.example:server0/tpg1/luns create /backstores/block/server0.disk1

Created LUN 0.

Created LUN 0->0 mapping in node ACL iqn.2014-12.com.example:desktop0

/> /iscsi/iqn.2014-12.com.example:server0/tpg1/portals create 172.25.0.11

Using default IP port 3260

Created network portal 172.25.0.11:3260.

/> exit

systemctl stop firewalld



訪問iSCSI存儲(客戶端 172.25.254.207):

安裝iSCSI發起端軟件包:

# yum install -y iscsi-initiator-utils

在/etc/iscsi/initiatorname.iscsi中設置發起端的IQN:

InitiatorName=iqn.2014-12.com.example:desktop0


查找iSCSI服務器所提供的iSCSI目標(目標門戶)

# iscsiadm -m discovery -t st -p 172.25.0.11


登陸服務器上的一個或多個iscsi目標

# iscsiadm -m node -T iqn.2024-12.com.example:server0 -p 172.25.0.11 -l


此時,可使用iSCSI磁盤,就好像它是本地鏈接硬盤驅動器。

能夠掛載現有文件系統。若是磁盤未格式化,能夠經過fdisk進行分區,例如,經過

文件系統格式化分區或做爲LVM物理卷。


在/etc/fstab中永久掛載文件系統

1. 使用blkid肯定文件系統UUID並使用UUID掛載,而不是/dev/sd*設備名稱。(每次引導時顯示的設備名稱都不一樣,具體取決於iSCSI設備經過網絡進行響應的順序。若是按設備名稱掛載,這會致使使用錯誤的設備。)

2. 在/etc/fstab中使用_netdev做爲掛載選項。(這將確保客戶端不會嘗試掛載文件系統,直至啓用聯網。不然,在引導時系統將出錯。)

3. 確保iscsi服務在引導時啓動。

vim /etc/fstab 修改內容爲:

/dev/sda1 /mnt xfs defaults,_netdev 0 0


中斷使用iSCSI目標

確保沒有使用目標所提供的任何設備。

確保從/etc/fstab等位置中刪除使用目標的全部永久掛載。

登出iSCSI目標,以暫時斷開鏈接。

# iscsiadm -m node -T iqn.2010-09.com.example:rdisks.demo -p 192.168.0.254 -u


刪除iSCSI目標的本地記錄,以永久斷開鏈接。

# iscsiadm -m node -T iqn.2010-09.com.example:rdisks.demo -p 192.168.0.254 -o delete



示例:

服務器端(172.25.254.107):

[root@maillinux ~]# yum install targetcli -y

[root@maillinux ~]# systemctl start target

[root@maillinux ~]# targetcli 

/> ls

o- / ..................................................................... [...]

  o- backstores .......................................................... [...]

  | o- block .............................................. [Storage Objects: 0]

  | o- fileio ............................................. [Storage Objects: 0]

  | o- pscsi .............................................. [Storage Objects: 0]

  | o- ramdisk ............................................ [Storage Objects: 0]

  o- iscsi ........................................................ [Targets: 0]

  o- loopback ..................................................... [Targets: 0]

/> /backstores/block create westos:storage1 /dev/vdb1

Created block storage object westos:storage1 using /dev/vdb1.

/> ls

o- / ..................................................................... [...]

  o- backstores .......................................................... [...]

  | o- block .............................................. [Storage Objects: 1]

  | | o- westos:storage1 ........ [/dev/vdb1 (1000.0MiB) write-thru deactivated]

  | o- fileio ............................................. [Storage Objects: 0]

  | o- pscsi .............................................. [Storage Objects: 0]

  | o- ramdisk ............................................ [Storage Objects: 0]

  o- iscsi ........................................................ [Targets: 0]

  o- loopback ..................................................... [Targets: 0]

/> /iscsi create iqn.2016-12.com.example:storage1

Created target iqn.2016-12.com.example:storage1.

Created TPG 1.

/> /iscsi/iqn.2016-12.com.example:storage1/tpg1/acls create iqn.2016-12.com.example:key1

Created Node ACL for iqn.2016-12.com.example:key1

/> ls

o- / ..................................................................... [...]

  o- backstores .......................................................... [...]

  | o- block .............................................. [Storage Objects: 1]

  | | o- westos:storage1 ........ [/dev/vdb1 (1000.0MiB) write-thru deactivated]

  | o- fileio ............................................. [Storage Objects: 0]

  | o- pscsi .............................................. [Storage Objects: 0]

  | o- ramdisk ............................................ [Storage Objects: 0]

  o- iscsi ........................................................ [Targets: 1]

  | o- iqn.2016-12.com.example:storage1 .............................. [TPGs: 1]

  |   o- tpg1 ........................................... [no-gen-acls, no-auth]

  |     o- acls ...................................................... [ACLs: 1]

  |     | o- iqn.2016-12.com.example:key1 ..................... [Mapped LUNs: 0]

  |     o- luns ...................................................... [LUNs: 0]

  |     o- portals ................................................ [Portals: 0]

  o- loopback ..................................................... [Targets: 0]

/> /iscsi/iqn.2016-12.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1 

Created LUN 0.

Created LUN 0->0 mapping in node ACL iqn.2016-12.com.example:key1

/> ls

o- / ..................................................................... [...]

  o- backstores .......................................................... [...]

  | o- block .............................................. [Storage Objects: 1]

  | | o- westos:storage1 .......... [/dev/vdb1 (1000.0MiB) write-thru activated]

  | o- fileio ............................................. [Storage Objects: 0]

  | o- pscsi .............................................. [Storage Objects: 0]

  | o- ramdisk ............................................ [Storage Objects: 0]

  o- iscsi ........................................................ [Targets: 1]

  | o- iqn.2016-12.com.example:storage1 .............................. [TPGs: 1]

  |   o- tpg1 ........................................... [no-gen-acls, no-auth]

  |     o- acls ...................................................... [ACLs: 1]

  |     | o- iqn.2016-12.com.example:key1 ..................... [Mapped LUNs: 1]

  |     |   o- mapped_lun0 ................... [lun0 block/westos:storage1 (rw)]

  |     o- luns ...................................................... [LUNs: 1]

  |     | o- lun0 .......................... [block/westos:storage1 (/dev/vdb1)]

  |     o- portals ................................................ [Portals: 0]

  o- loopback ..................................................... [Targets: 0]

/> /iscsi/iqn.2016-12.com.example:storage1/tpg1/portals create 172.25.254.107

Using default IP port 3260

Created network portal 172.25.254.107:3260.

/> exit

Global pref auto_save_on_exit=true

Last 10 configs saved in /etc/target/backup.

Configuration saved to /etc/target/saveconfig.json

[root@maillinux ~]# netstat -antlpe | grep 3260

tcp        0      0 172.25.254.107:3260     0.0.0.0:*               LISTEN      0          42453      -                   

[root@maillinux ~]# systemctl stop firewalld


客戶端:

[root@mailwestos ~]# yum search iscsi

Loaded plugins: langpacks

rhel_dvd                                                 | 4.1 kB     00:00     

============================== N/S matched: iscsi ==============================

iscsi-initiator-utils.i686 : iSCSI daemon and utility programs

iscsi-initiator-utils.x86_64 : iSCSI daemon and utility programs

iscsi-initiator-utils-iscsiuio.x86_64 : Userspace configuration daemon required

                                      : for some iSCSI hardware

libiscsi.i686 : iSCSI client library

libiscsi.x86_64 : iSCSI client library


  Name and summary matches only, use "search all" for everything.

[root@mailwestos ~]# yum install iscsi-initiator-utils.x86_64 -y

[root@mailwestos ~]# vim /etc/iscsi/initiatorname.iscsi 

 InitiatorName=iqn.2016-12.com.example:key1

[root@mailwestos ~]# systemctl restart iscsi

[root@mailwestos ~]# iscsiadm -m node -T iqn.2016-12.com.example:storage1 -p 172.25.254.107 -l

[root@mailwestos ~]# fdisk /dev/sda 

    Last sector, +sectors or +size{K,M,G} (8192-2047999, default 2047999): +100M

[root@mailwestos ~]# partprobe

[root@mailwestos ~]# mkfs.xfs /dev/sda1

[root@mailwestos ~]# mount /dev/sda1 /mnt/

[root@mailwestos ~]# df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/vda1       10473900 3603124   6870776  35% /

devtmpfs          927072       0    927072   0% /dev

tmpfs             942660      80    942580   1% /dev/shm

tmpfs             942660   17016    925644   2% /run

tmpfs             942660       0    942660   0% /sys/fs/cgroup

/dev/sda1          98988    5280     93708   6% /mnt

[root@mailwestos ~]# iscsiadm -m node -T iqn.2016-12.com.example:storage1 -p 172.25.254.107 -u

Logging out of session [sid: 2, target: iqn.2016-12.com.example:storage1, portal: 172.25.254.107,3260]

Logout of [sid: 2, target: iqn.2016-12.com.example:storage1, portal: 172.25.254.107,3260] successful.

[root@mailwestos ~]# iscsiadm -m node -T iqn.2016-12.com.example:storage1 -p 172.25.254.107 -o delete

[root@mailwestos ~]# df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/vda1       10473900 3603204   6870696  35% /

devtmpfs          927072       0    927072   0% /dev

tmpfs             942660      80    942580   1% /dev/shm

tmpfs             942660   17036    925624   2% /run

tmpfs             942660       0    942660   0% /sys/fs/cgroup

[root@mailwestos ~]# fdisk -l


Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors


   Device Boot      Start         End      Blocks   Id  System

/dev/vda1   *        2048    20970332    10484142+  83  Linux


Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

I/O size (minimum/optimal): 512 bytes / 512 bytes



############## 高級網絡配置 #########

網絡橋接

網絡橋接用網絡橋實現共享上網主機和客戶機除了利用軟件外,還能夠用系統自帶的網絡橋創建鏈接用雙網卡的機器作主機


網絡橋接的配置

vim /etc/sysconfig/network-scripts/ifcfg-eth0

– BRIDGE=br0

vim /etc/sysconfig/network-scripts/ifcfg-br0

– TYPE=Bridge


真機:

cd /etc/sysconfig/network-scripts

mv ifcfg-br0 /mnt/

mv ifcfg-enp2so /mnt/

nm-connection-editor  --> 添加一個ip名爲lzt

vim ifcfg-westos

  1 BOOTPROTO=none

  2 DEVICE=enp2s0

  3 ONBOOT=yes

  4 BRIDGE=br0  


vim ifcfg-br0  

  1 DEVICE=br0

  2 BOOTPROTO=none

  3 ONBOOT=yes

  4 TYPE=Bridge

  5 IPADDR=172.25.254.7                                                              

  6 NETMASK=255.255.255.0


systemctl stop NetworkManager

systemctl restart network

systemctl start NetworkManager


網絡橋接的管理命令

brctl

### 橋接管理命令

– show ### 顯示

– addbr ### 添加網橋

– delbr ### 刪除網橋

– addif ### 添加網橋鏈接

– delif ### 刪除網橋鏈接



在虛擬機中:

systemctl stop NetworkManager

brctl addbr br0

brctl show

ifconfig

ifconfig br0 172.25.254.107 netmask 255.255.255.0

ifconfig

ping 172.25.254.250

brctl show

brctl addif br0 eth0

ifconfig eth0 up

ifconfig

ping 172.25.254.250

brctl show

ifconfig br0

ifconfig br0 down

ifconfig br0

ifconfig

brctl delif br0 eth0

brctl delbr br0

ifconfig

brctl show


bond 網絡 -- 最多可加兩塊

Red Hat Enterprise Linux 容許管理員使用bonding 內核模塊和稱爲通道綁定接口的特殊網絡接口將多個網絡接口綁定到一個通道。根據選擇的綁定模式 , 通道綁定使兩個或更多個網絡接口做爲一個網絡接口 , 從而增長帶寬和 / 提供冗餘性。


選擇 Linux 以太網綁定模式

模式 0 ( 平衡輪循 ) - 輪循策略 , 全部接口都使用採用輪循方式在全部 Slave 中傳輸封包 ; 任何 Slave 均可以接收

模式 1 ( 主動備份 ) - 容錯。一次只能使用一個 Slave 接口 , 可是若是該接口出現故障 , 另外一個Slave 將接替它

模式 3 ( 廣播 ) - 容錯。全部封包都經過全部 Slave 接口廣播


虛擬機:

systemctl start NetManager

nm-connection-editor --> 刪除全部

nmcli connection add con-name bond0 ifname bond0 type bond mode active-backup ip4 172.25.254.107/24

nmcli connection add con-name eth0 ifname eth0 type bond-slave master bond0

nmcli connection add con-name eth1 ifname eth1 type bond-slave master bond0

ping 172.25.254.250

ifconfig eth0 down

ifconfig eth0 up


監控命令:

watch -n 1 cat /proc/net/bonding/bond0



Team 接口

Team 和 bond0 功能相似

Team 不須要手動加載相應內核模塊

Team 有更強的拓展性

– 支持 8 快網卡


虛擬機:

nmcli connection add con-name team0 ifname team0 type team config '{"runner":{"name":"activebackup"}}' ip4 172.25.254.107/24

nmcli connection add con-name eth0 ifname eth0 type team-slave master team0

nmcli connection add con-name eth1 ifname eth1 type team-slave master team0

ping 172.25.254.250

ifconfig eth0 down

ifconfig eth0 up


監控命令:

watch -n 1 teamdctl team0 stat


############# Samba ###########

yum install samba samba-common samba-client -y

systemctl stop firewalld

systemctl start smb

netstat -anutlpe | grep smbd

useradd westos

useradd linux

smbpasswd -a westos

smbpasswd -a linux

pdbedit -L

pdbedit -x linux ##刪除linux用戶

smbclient -L //172.25.254.107 -U westos --> 輸入密碼進入 退出命令:quit

setsebool -P samba_enable_home_dirs on 許掛載遠程CIFS文件共享並將其用

做本地Linux主目錄

cd /etc/samba

vim smb.conf 修改內容爲:在最後

 [westos]

 comment = samba directory

 path = /samba

 [linux]

 comment = mnt directory

 path = /mnt

semanage fcontext -a -t samba_share_t '/samba(/.*)?'

restorecon -vvFR /samba


systemctl restart smb.service

setsebool -P samba_export_all_ro on 用於共享系統目錄

smbclient -L //172.25.254.107 -U westos --> ls 便可看到 /mnt 目錄下的文件


vim smb.conf 修改內容爲:

 security = user

 passdb backend = tdbsam

 map to guest = bad user

 [westos]

 comment = samba directory

 guest ok = yes ##容許匿名用戶訪問

 path = /samba

相關文章
相關標籤/搜索