脚本

以前用到的一些简单脚本

1.拷贝文件

scp.sh

#!/bin/bash

read -p “将要存放的路径:” save_path
read -p “要复制的文件路径:” get_path
iplist_path=/data/iplist.txt
while read ip ;do
([ -s $get_path ] && scp $get_path $ip:$save_path) || echo ‘file do not exist !’
done < $iplist_path

2.第一次输入免秘钥

expect.sh

#!/bin/bash

mkdir /data
rpm -q expect || yum -y install expect
path=/data/iplist.txt
pass=”123456″
ssh-keygen -t rsa -P “” -f /root/.ssh/id_rsa
while read ip ;do
expect <<EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $ip
expect {
“yes/no” { send “yes ”;exp_continue }
“password” { send “$pass ” }
}
expect eof
EOF
done < $path

3.执行脚本

run.sh

#!/bin/bash

#想要在执行完成后删除脚本,可以在想要执行的脚本后面添加”rm -f $0″

rpm -q pssh||yum -y install pssh
path=/data/iplist.txt
pscp.pssh -v -h $path “$1” /
pssh -v -h $path “bash /*.sh”

3.批量创建账户

path=/data/passwd
chattr +a $path
read -p “input the num of beginning : ” j
read -p “input the num of beginning : ” k
for i in `seq $j $k` ; do
if `seq $j $k` ;then
echo user$i is exist
continue
else
eval echo user$i|xargs -n 1 useradd &>/dev/null
eval echo -e user$i:`openssl rand -base64 12`” ”|tr -s ‘ ‘ “ ” >> $path
passwd=`sed -nr “s/user$i:(.*)/1/p $path`
sed -nr “s/.user$i:(.)/1/p” $path |passwd –stdin user$i
echo The passwd of user$i is $passwd

done

#设置密码另一种方式,以前的文章中应该有设置密码的string函数,那个方式是从前领导那学来的
tr -cd [[:alnum:]!_#@] < /dev/urandom |head -c 8

4.扫描IP,并设置免密登录

read -p “输入想要扫描的范围” field

#!/bin/bash

grep “id” `ls /root/.ssh/`

if [ “$0” -eq 0 ];then

rm /root/.ssh/id -f
fi
pass=”1″
ssh-keygen -t rsa -P “” -f /root/.ssh/id_rsa
path=/data/iplist.txt

yum -y install tree expect pssh bash-completion* lrzsz vim net-tools
cd /
if [ -e ./data ];then
if [ -d ./data ];then
touch iplist.txt;echo 是个目录,创建了iplist.txt文件
else
rm -f ./data ;echo 是个文件
fi
else
mkdir ./data && touch /data/iplist.txt;echo 没有这个目录以及文件
fi

for i in `seq 254`;do
{
ping -c1 -w1 $field.$i &>/dev/null
if [ “$?” -eq 0 ];then
grep “$field.$i” $path &>/dev/null
if [ “$?” -ne 0 ];then
while [ “$i” -le 255 ] ;do
expect << EOF

spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $field.$i
expect {
“yes/no” { send “yes ”;exp_continue }
“password” { send “$pass ” }
}
expect eof
EOF
if [ “$?” -eq 0 ];then
echo $field .$i >> $path
fi
let i++
done
fi
fi
}&
done

5.判断文件类型(从宗华老师那学来的)

#!/bin/bash

read -p “input path (example:/root/*) ” path
if [ ! -e $path ];then
echo “file don’t exist”&&exit 1
else
for i in $path ;do
if [ -b $i ];then
echo “$i:块设备”
elif [ -c $i ];then
echo “$i:符号设备”
elif [ -d $i ];then
echo “$i:目录文件”
elif [ -f $i ];then
echo “$i:普通文件”
elif [ -h $i ];then
echo “$i:链接文件”
elif [ -p $i ];then
echo “$i:管道设备”
elif [ -s $i ];then
echo “$i:套接字文件”
fi
done
fi

6.创建html文件

#!/bin/bash

path=/data/
read -p “First :” f
read -p “Last :” l
for i in `seq $f $l`;do
touch $path$i`tr -dc “[:alnum:]” </dev/urandom |head -c 8`.html
done

6.服务脚本(从宗华老师那学来的)

#!/bin/sh

#chkconfig:- 96 07

#description

name=`basename $0`
[ -f /etc/rc.d/init.d/$name ]||mv /app/script/$name /etc/rc.d/init.d/$name &>/dev/null
chkconfig –add $name
start () {
touch /var/lock/subsys/ $name
. /etc/init.d/functions
action ” $name start successful!” true
}
stop () {
rm /var/lock/subsys/$name
. /etc/init.d/functions
action ” $name is stopped” true
}
restart () {
stop
start
}
status () {
if [ -f /var/lock/subsys/$name ];then
. /etc/init.d/functions
action “$name is running” true
else
action “$name is stopped” true
fi
}
case $1 in
start)
if [ -f /var/lock/subsys/$name ];then
echo “$name is running”
else
start
fi
;;
stop)
if [ -f /var/lock/subsys/$name ];then
stop
else
echo “$name has been stopped before”
fi
;;
restart)
if [ -f /var/lock/subsys/$name ];then
stop
start
else
start
fi
;;
status)
status
;;
esac

原文地址:https://www.cnblogs.com/dengbingbing/p/10459700.html