关于shell:shell-编程

开启敞开SSH

# 开启ssh
function disable_ssh() {
    sed -i "s/PasswordAuthentication yes/PasswordAuthentication no/g" /etc/ssh/sshd_config
    systemctl restart ssh
    w | grep sshd | awk '{print $2}' | while read one; do pkill -9 -t $one; done #退出以后登录的用户
    return 0
}

# 敞开ssh
function enable_ssh() {
    sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
    sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
    systemctl restart ssh
    return 0
}

批改以后用户登录明码

function change_passwd() {
    passwd=$1
    [[ -z "$passwd" ]] && return 0
    echo "$(whoami):$passwd" | /usr/sbin/chpasswd || true
    sync
    return 0
}

# usage: change_passwd pwd

shell 实现并发执行并且管制并发数量

#!/bin/bash
start_time=$(date +%s)             #定义脚本运行的开始工夫
[ -e /tmp/fd1 ] || mkfifo /tmp/fd1 #创立有名管道
exec 3<>/tmp/fd1                   #创立文件描述符,以可读(<)可写(>)的形式关联管道文件,这时候文件描述符3就有了有名管道文件的所有个性
rm -rf /tmp/fd1                    #关联后的文件描述符领有管道文件的所有个性,所以这时候管道文件能够删除,咱们留下文件描述符来用就能够了
for ((i = 1; i <= 10; i++)); do
    echo >&3 #&3代表援用文件描述符3,这条命令代表往管道外面放入了一个"令牌"
done

for ((i = 1; i <= 1000; i++)); do
    read -u3 #代表从管道中读取一个令牌
    {
        sleep 1 #sleep 1用来模拟执行一条命令须要破费的工夫(能够用实在命令来代替)
        echo 'success'$i
        echo >&3 #代表我这一次命令执行到最初,把令牌放回管道
    } &
done
wait

stop_time=$(date +%s) #定义脚本运行的完结工夫

echo "TIME:$(expr $stop_time - $start_time)"
exec 3<&- #敞开文件描述符的读
exec 3>&- #敞开文件描述符的写

byte 格式化显示

function byte_format() {
    totalsize=$1
    if [[ "$totalsize" =~ ^[0-9]+$ ]]; then
        if [ 1024 -gt $totalsize ]; then
            size="$totalsize"B
        elif [ 1048576 -gt $totalsize ]; then
            size=$(echo "scale=3; a = $totalsize / 1024 ; if (length(a)==scale(a)) print 0;print a" | bc)
            size="$size"KB
        elif [ 1073741824 -gt $totalsize ]; then
            size=$(echo "scale=3; a = $totalsize / 1048576 ; if (length(a)==scale(a)) print 0;print a" | bc)
            size="$size"MB
        elif [ 1073741824 -le $totalsize ]; then
            size=$(echo "scale=3; a = $totalsize / 1073741824 ; if (length(a)==scale(a)) print 0;print a" | bc)
            size="$size"GB
        else
            size="0"
        fi
    else
        size="NULL"
    fi
    echo $size
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理