实验楼-小时赛第1期 linux练习

1 服务管理

安装并运行samba:

sudo apt-get update
sudo apt-get install samba
sudo service samba restart

2  提取文本中的信息

用脚本提取给定文档中以png和jpg结尾的图片链接:

grep "http.*\.\(jpg\|png\)" $1 

grep "http.*\.\ ( jpg \ | png \ )" $1 


3  查找指定文件

将 /etc 目录下所有内容中包含 shiyanlou 字符串的文件的完整路径都写入 /home/shiyanlou/output 文件中:

touch output
sudo grep -R "shiyanlou" /etc > output


4   获取满足条件的程序

请实现一个脚本获取指定端口上正在运行的程序的绝对路径,如果没有运行任何程序则打印 OK 字符串。

t=`lsof -i:$1|tail -1|awk '{print $1}'`
if [  -n "$t" ] ;then
    which $t
else
    echo "OK"
fi


5  证书配置
请为 shiyanlou 用户配置一个 SSH 证书,使 shiyanlou 用户 SSH 登陆本地实验环境的时候不需要输入密码。同时设置 SSH 服务禁止所有用户使用密码登陆。

#1 修改sshd配置
sudo vi /etc/ssh/sshd_config
#禁止密码登录
PasswordAuthentication  no 
#注释三行
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys

#重启让配置生效
sudo service ssh restart

#生成公钥写入文件
cd ~/.ssh
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
cp  id_dsa.pub  authorized_keys
#不安全的设置,将不能使用RSA功能
chmod 600 ~/.ssh/authorized_keys

6 避免误删

(1)rm -f 命令删除的文件或文件夹都临时存入 /tmp/trash 文件夹,而不删除,例如使用 rm -f /home/shiyanlou/testfile 后,文件 testfile 会被移动到 /tmp/trash/testfile,如果 /tmp/trash 目录下已经有 testfile 重名文件则直接覆盖老的文件。
(2)rm 命令不加 -f 参数的时候执行流程不变,不需要移动到 /tmp/trash 文件夹。

sudo cp /bin/rm /bin/oldrm
sudo cp  ~/rm.sh  /bin/rm

#脚本内容
#!/bin/bash
if [ $# -eq 2 ] && [ "$1"=="-f" ]
then
        mv $2 /tmp/trash
else
        /bin/oldrm $1 $2
fi


7  密码生成器

(1)生成的密码字符串长度为12位
(2)密码中必须同时包含数字,大小写字母及至少1个特殊字符
(3)只允许使用这些特殊字符:><+-{}:.&;

arr1=(\> \< \+ \- \{ \} \: \. \& \;)
arr2=(a b c d e f g h i j k l m n o p q r s t u v w x y z)
arr3=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
t=${arr1[$[$RANDOM%10]]}
t=$t${arr2[$[$RANDOM%26]]}
t=$t${arr3[$[$RANDOM%26]]}
t=$t$[$RANDOM%10]
echo $t"abcdefgh"


10 拷贝指定文件

拷贝指定目录下(包括子目录)所有大于4M文件

#!/bin/bash
function getdir(){
    #目录一定要一致
    if [ ! -d "/tmp"$1 ]; then
        `mkdir "/tmp"$1`
    fi
    for element in `sudo ls $1 `
    do
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ]
        then
            getdir $dir_or_file
            continue
        else
            ss=`sudo ls -l $dir_or_file| awk '{print $5}'`
            if [ $ss -gt 10240 ]; then
                `cp $dir_or_file "/tmp"$1`
            fi
        fi
    done
}
root_dir="/etc"
getdir $root_dir






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值