Home > other >  Server operations necessary shell script
Server operations necessary shell script

Time:10-17

Automatically create a virtual machine
#!/bin/bash
Read -p 'please enter the back-end disk path BACK_DISK
CD/var/lib/libvirt/images/# CD to a virtual machine disk storage directory
Qemu - img create -f qcow2 - b $BACK_DISK ${1}. Img 20 g # create disk image; . Node_base. Qcow2 (backend)
Sed "s, node_base, $1," node_base. XML & gt; The/etc/libvirt/qemu/${1}. The XML # to create corresponding XML file
With virsh define/etc/libvirt/qemu/${1}. The XML # define XML file
With virsh start at $1 # start virtual machine



Regular backup/var/log log file
#!/bin/bash
The tar - ZCF log - ` date + % Y % m % d ` tar. Gz/var/log
System Settings: crontab -e 00 03 * * 5/root/script name



Real-time monitoring of native memory and disk space, and then send the mail to alert
#!/bin/bash
While:
Do
DISK_SIZE=$(df/| awk '////{print $4}') # call this machine available disk space
MEM_SIZE=$(free | awk '/Mem/{print $4}') # call native memory available
If [$DISK_SIZE - le 512000 - a $MEM_SIZE - le 1024000]. Then
Mail - 'alarm' s root & lt; Insufficient resources, please timely top-up.
EOF
Fi
Sleep 1
The done



Unit the boot and shutdown state
#!/bin/bash
For I in {1.. 254}
Do
Ping - 2-0.3 - W I c 1 192.168.4. $I & amp;>/dev/null
if [ $? -eq 0 ]; Then
Echo "192.168.4. $I is up"
The else
Echo "192.168.4. $I is" down "
Fi
The done



Real-time display eth0 adapter to send and receive packets flow
#!/bin/bash
While:
Do
Echo "eth0 network traffic information is as follows:"
Ifconfig eth0 | grep "RX pack" | awk '{print $5}' # received data flow package
Ifconfig eth0 | grep "TX pack" | awk '{print $5}' # send data traffic package
Sleep 1 # refresh once per second
The done



User list file is used to create the user and the initial password is 123456
#!/bin/bash
# $1 for storing user list file
For I ` cat in $1 `
Do
Useradd $I
Echo 123456 | passwd -- stdin $I & amp;>/dev/null
The done



According to connect the machine remote IP
#!/bin/bash
Netstat atn | awk 'NR> 2 {print $5} '| awk' {print $1} '| sort - rn | uniq -c



Statistics within a certain period of time all access to the Apache server request number
#!/bin/bash
Awk -f "//:" '$7 ":" $8 & gt;="and" & amp; & $7 ":" $8 & lt;="passion" '/var/log/HTTPD/access_log | wc -l'
Statistics of all access to the Apache server within a certain period of time the client IP
#!/bin/bash
Awk -f "//:" '$7 ":" $8 & gt;="and" & amp; & $7 ":" $8 & lt;="passion" {print $1} '/var/log/HTTPD/access_log
The statistics for each remote IP access native Apache several
#!/bin/bash
Awk '{IP + +} [$1] END {for (I) in IP {print IP [I], I}}'/var/log/HTTPD/access_log



Cutting Nginx log file
#!/bin/bash
Logs_path="/usr/local/nginx/logs"
Mv ${logs_path} access. Log ${logs_path} access_ $(date + % Y % m % d). The log
Lill - USER1 $(cat/usr/local/nginx/logs/nginx pid)
The Linux command line: crontab -e 0 1 * * */data/scripts/script name



Detection of mysql database connection number
#!/bin/bash
Log_file="/var/log/mysql_count log"
User=root
Passwd=123456
While:
Do
Sleep 2
"$user" count=` mysqladmin -u -p "$passwd" status | awk '{print $4}' `
Echo "` date + % Y % m % d ` concurrent connection number is: ${count}." & gt;> $log_file
The done



Non-interactive generated SSH secret key file
#!/bin/bash
Rm - rf ~/. SSH/id_rsa *
SSH - the keygen -t RSA -n '- f ~/. SSH/id_rsa



Cycle test user name and password is correct
#!/bin/bash
For I in {1.. 3}
Do
Read -p "please enter the user name" user
The read - p "please enter the password" pass
If [$user "=='Tom' - a" $pass "=='123456'); Then
Echo "landing successful
"The exit
The else
Echo "please enter your user name and password again"
Fi
The done
Echo "login failed"


List the machine to the user's password
#!/bin/bash
User=$(awk - F: '/bash ${print $1}'/etc/passwd)
For I in $user
Do
The awk - F: - v x=$I '$1==x {print $1, $2}'/etc/shadow
The done


Statistical number of Linux process related information
#!/bin/bash
Running=0
Sleeping=0
Stoped=0
Zombie=0
# in the/proc directory all begins with a number of current computer running process of the process PID
# each PID number directory records related to the process information
For pid in ` ls/proc/[1-9] * `
Do
Procs=$[procs + 1]
The stat=$(awk '3} {print $$pid/stat)
# each pid directory has a stat file, the file of the third column is the state of the process information
Case $stat in
R)
Running=$[running + 1];;
T)
Stoped=$[stoped + 1];;
S)
Sleeping=$[sleeping + 1];;
Z)
Zombie=$[zombie + 1];;
Esac
The done
Echo "process statistics as follows"
Echo "total number of process is: $procs"
Echo "Running process for: $Running"
Echo "process Stoped for: $Stoped"
Echo "Sleeping process for: $Sleeping"
Echo "Zombie process for: $Zombie"




Using a script to open close the virtual machine
#!/bin/bash
# script by calling with virsh command implementation management of the virtual machine, if you don't have this command, you need to install libvirt - client software package
# $1 is the first parameter of the script, $2 is the second script parameter
# 1 parameter is you want to the operation of the virtual machine instructions, the second parameter is the name of the virtual machine
In case $1
The list)
With virsh list -- all;;
Start)
With virsh start $2;;
Stop)
With virsh destroy; $2;
The enable)
With virsh autostart; $2;
Disable)
With virsh autostart - disable; $2;
*)
Echo "Usage: $0 list"
Echo "Usage: $0 [start | stop | enable | disable] VM_name"
The cat & lt; The list shows that virtual machine list

start to start the virtual machineStop virtual machines are shut down
The enable set the virtual machine to boot from the rev.
Disable shut down the virtual machine can boot from qi gong
EOF;;
Esac





Don't log in virtual machine, modify the network card IP address
#!/bin/bash
# scripts without log in the virtual machine, change the IP address of the virtual machine information; In some circumstances, the virtual machine no IP or IP address and the host is not a real network segment; Real host in the absence of virt - manager graphics, virtual machine remote connection is very trouble. The script can solve similar problems.
Read -p "please enter the virtual machine name:" name
If with virsh domstate $name | grep - q running; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related