Shell scripts

#! /bin/bash

shopt -s -o nounset

declare -i p=0
SRC=${1:?'Please input src files name'}
DEST=${2:?'Plesae input dest files name'}

Total=$(stat -c %s $SRC)
touch $DEST

(cp -f $SRC $DEST; sync) &

{
while ((p < 100))
do
    Size=$(stat -c %s $DEST)
    p=Size*100/Total
    echo $p
    sleep 1
done
} | dialog --guage "复制进度" 10 50 0
#!/bin/bash
host="192.168.99.99 21"
id="user"
pw='mima'
basedir="/var/lib/db"
filename="mybackup.`date +%Y%m%d%H%M`.gz"
cd $basedir
/usr/local/mysql/bin/mysqldump --opt -hlocalhost -uroot -ppswd data | gzip -f>/"$basedir"/"$filename"
ftp -n $host>/dev/null 2>/dev/null < <EOF
user $id    $pw
binary
put $filename
bye
EOF
cd $basedir
rm -rf $filename

#!/bin/bash
myday=`date +%F`
mytime=`date +%T --date="-1 hour"`
##PURGE BINARY LOGS BEFORE '2000-02-02 22:22:22';
#echo $myday $mytime
mysql -uroot -p123456 << EOF

PURGE BINARY LOGS BEFORE '${myday} ${mytime}';

#EOF


#!/bin/bash
#Disable sshd dns
sed -i 's/#UseDNS.*/UseDNS no/g' /etc/ssh/sshd_config
sed -i 's/^GSSAPIAuthentication.*/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
sed -i 's/^#GSSAPIAuthentication.*/GSSAPIAuthentication no/' /etc/ssh/sshd_config

#Sudo log
groupadd admin
echo "%admin ALL=(ALL)  NOPASSWD: ALL" >> /etc/sudoers
echo "Defaults        logfile = \"/var/log/sudo\""  >> /etc/sudoers
echo "local2.debug  /var/log/sudo" >> /etc/syslog.conf
 
#Turn off snmpd log
echo "OPTIONS=\"-LS3d -Lf /dev/null -p /var/run/snmpd.pid\"" >> /etc/sysconfig/snmpd.options
 
#Record sftp log
sed -i '/Subsystem/d' /etc/ssh/sshd_config
echo "Subsystem       sftp    /usr/libexec/openssh/sftp-server -l INFO -f AUTH" >> /etc/ssh/sshd_config
echo "auth,authpriv.*                                          /var/log/sftp" >> /etc/syslog.conf
 

#!/bin/bash
yum install -y vsftpd
yum install -y db4*
rm -rf /etc/vsftpd/vsftpd.conf
cat < < 'EOF' >> /etc/vsftpd/vsftpd.conf

listen_port=21
anonymous_enable=NO
local_enable=YES
write_enable=NO
anon_upload_enable=NO
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
chroot_local_user=YES
guest_enable=YES
guest_username=nobody
user_config_dir=/etc/vsftpd/nobody/
ftpd_banner=welcome vsftp!
pam_service_name=vsftpd.vu
#tcp_wrappers=YES
vsftpd_log_file=/var/log/vsftpd.log
listen=YES
dual_log_enable=YES
xferlog_enable=YES
EOF


cat < < 'EOF' >> /etc/pam.d/vsftpd.vu
auth required /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account required /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
EOF

mkdir  /etc/vsftpd/nobody

cat < < 'EOF' >> /etc/vsftpd/nobody/ftpadmin
anon_world_readable_only=NO
write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES
anon_other_write_enable=YES
anon_umask=022
file_open_mode=0777
local_root=/tmp
EOF

cat < < 'EOF' >> /etc/vsftpd/nobody/upload
anon_world_readable_only=NO
write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES
local_root=/tmp/up
EOF

cat < < 'EOF' >> /etc/vsftpd/nobody/download
anon_world_readable_only=NO
local_root=/tmp/down
EOF

echo "ftpadmin"  > /etc/vsftpd/logins.txt
echo "ftpadmin" >> /etc/vsftpd/logins.txt
echo "upload" >> /etc/vsftpd/logins.txt
echo "upload" >> /etc/vsftpd/logins.txt
echo "download" >> /etc/vsftpd/logins.txt
echo "download" >> /etc/vsftpd/logins.txt

echo "db_load -T -t hash -f /etc/vsftpd/logins.txt /etc/vsftpd/vsftpd_login.db" >/etc/vsftpd/cmd

chmod u+x /etc/vsftpd/cmd

sh /etc/vsftpd/cmd

chkconfig  --level 3 vsftpd on
service vsftpd start

if [ "`whoami`" != "root" ] ;then
 
        print "This must be run as root."
        exit -1
fi
if [ "`uname -m`" == "x86_64" ] ; then
 
        wget -c http://xxoo.tv/xxoo2-2-2.x86_64.tar.gz
 
fi
if [ ! -d /usr/local/jpeg ]
     then 
        tar zxvf jpegsrc.v7.tar.gz
        cd jpeg-7
        ./configure --prefix=/usr/local/jpeg
        make 
        make install
        cd ..
        rm -rf jpeg-7
        echo "gpeg install is ok!"
  fi
if [ -e "./soft.tar.gz" ] && [ ! -d "./soft" ]; then
	tar -zxvf soft.tar.gz
fi

qmailshow.sh

#!/bin/sh
#show qmail queue

MESS=`find /var/qmail/queue/mess -type f | wc | awk '{print $1}'`
INFO=`find /var/qmail/queue/info -type f | wc | awk '{print $1}'`
LOCAL=`find /var/qmail/queue/local -type f | wc | awk '{print $1}'`
REMOTE=`find /var/qmail/queue/remote -type f | wc | awk '{print $1}'`
BOUNCE=`find /var/qmail/queue/bounce -type f | wc | awk '{print $1}'`

echo ""
echo "Current Qmail Queue:"
echo "--------------------"
echo "mess:             "$MESS
echo "info:             "$INFO
echo "local:            "$LOCAL
echo "remote:           "$REMOTE
echo "bounce:           "$BOUNCE
echo "--------------------"
date
echo ""

简单查看linux系统cpu\memory\disk

#!/bin/bash
# SCRIPT: hardware.sh work on Centos5
# AUTHOR: Gaizaoren
# DATE: 2012/08/08
# REV: 1.0 http://www.gaizaoren.org
# PURPOSE: This shell script use for find hareware
##########################################################
# BEGINNING OF MAIN
##########################################################
hard=$(dmidecode -t system |grep Manufa |awk '{print $2}')
hardware=$(dmidecode -t system |grep Product |awk '{print $3,$4}')
serial=$(dmidecode -t system |grep Serial |awk '{print $3}')

core=$(dmidecode -t processor |grep Version |grep -v "Not Specified"|awk '{print $2,$3,$4,$5,$6,$7}'|sed -n '1p')
core_count=$(dmidecode -t processor |grep Version |grep -v "Not Specified"|wc -l)

#raid=$( smartctl -a /dev/sda |grep "^Device" |awk '{print $3,$4}')
disk=$(fdisk -l |grep "^Disk"|awk -F"," '{print $1}')

Mem=$(dmidecode -t memory |grep "Size: [0-9]* MB"|sed -n '1p'|awk '{print $2,$3}'|awk '{print $1}')
Mem_count=$(dmidecode -t memory |grep "Size: [0-9]* MB"|wc -l)
Mem_total=`expr $Mem \* $Mem_count`
Mem_speed=$(dmidecode -t memory |grep Speed|grep MHz|awk '{print $2,$3}'|sed -n '1p')

echo "$hard $hardware $serial; $core X $core_count; ${Mem_total}MB $Mem_count X ${Mem}MB $Mem_speed; $disk"

##########################################################
# End of script
##########################################################

linux清空文件内容方法

[root@dbrg-2 ~]# vi mbox

iiii
~
root@dbrg-2 ~]# >mbox
[root@dbrg-2 ~]# more mbox
[root@dbrg-2 ~]# vi mbox

iiii

~                                                                                                                                                                                                                                  “mbox” 1L, 5C 已写 入
[root@dbrg-2 ~]# more mbox
iiii
[root@dbrg-2 ~]# :>mbox
[root@dbrg-2 ~]# more mbox

[root@dbrg-2 ~]# echo “haha” >mbox
[root@dbrg-2 ~]# more mbox
haha
[root@dbrg-2 ~]# echo “” > mbox
[root@dbrg-2 ~]# more mbox
[root@dbrg-2 ~]# more arp.txt
00:1f:3c:10:82:d2
00:e0:4c:02:54:14
00:1d:0f:06:36:39
00:e0:4c:02:48:5e
00:1f:3c:0f:0c:0d
00:16:76:17:68:d9
[root@dbrg-2 ~]# echo >arp.txt
[root@dbrg-2 ~]# more arp.txt

[root@dbrg-2 ~]#

[root@dbrg-2 ~]# echo “haha” >mbox

[root@dbrg-2 ~]# cp /dev/null mbox
cp:是否覆盖‘mbox’? y
[root@dbrg-2 ~]# more mbox

[root@dbrg-2 ~]# echo “haha” >mbox
[root@dbrg-2 ~]# more mbox
haha

[root@dbrg-2 ~]# cat /dev/null >mbox
[root@dbrg-2 ~]# more mbox
[root@dbrg-2 ~]#
[root@dbrg-2 ~]# more arp.txt
00:1f:3c:10:82:d2
00:e0:4c:02:54:14
00:1d:0f:06:36:39
00:e0:4c:02:48:5e
00:1f:3c:0f:0c:0d
00:1f:3c:74:25:b0
00:1b:77:6f:73:6e
00:1f:3c:cf:d2:68
00:1f:d0:0d:f7:12
[root@dbrg-2 ~]# sed -i ‘1,$’d arp.txt
[root@dbrg-2 ~]# more arp.txt
[root@dbrg-2 ~]#