shell进阶、进程和计划任务

1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。 expect脚本:①安装expect root@ubuntu:~# apt install expect -y root@ubuntu:~# dpkg -l expect Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-====================================================-===============================-===============================-============================================================================================================== ii expect 5.45.4-1 amd64 Automates interactive applications ②使用expect写脚本并运行 root@ubuntu:~# cat remotelogin_expect.sh #!/usr/bin/expect spawn ssh root@192.168.112.129 expect { "*(yes/no*" { send "yes\n";exp_continue } "*password:*" { send "root\n";exp_continue } "*]#" { send "hostname -I\n" } } interact root@ubuntu:~# ./remotelogin_expect.sh spawn ssh root@192.168.112.129 root@192.168.112.129's password: Activate the web console with: systemctl enable --now cockpit.socket Last login: Tue Dec 21 16:23:16 2021 from 192.168.112.130 [root@Centos8 ~]#hostname -I 192.168.112.129 想要退出运行一下exit命令就好了,那样就可以回到那个主机了 [root@Centos8 ~]#exit logout Connection to 192.168.112.129 closed. shell脚本 root@ubuntu:~# cat remotelogin.sh #!/bin/bash IP=192.168.112.129 USER=root PASSWD=root expect << EOF spawn ssh $USER@$IP expect { "*(yes/no*" { send "yes\n";exp_continue } "*password:*" { send "$PASSWD\n";exp_continue } "*]#" { send "hostname -I\n" } } expect eof; 执行写好的脚本remotelogin.sh root@ubuntu:~# ./remotelogin.sh ./remotelogin.sh: line 13: warning: here-document at line 6 delimited by end-of-file (wanted `EOF') spawn ssh root@192.168.112.129 root@192.168.112.129's password: Activate the web console with: systemctl enable --now cockpit.socket Last login: Tue Dec 21 16:46:06 2021 from 192.168.112.130 [root@Centos8 ~]#hostname -I 192.168.112.129 2、生成10个随机数保存于数组中,并找出其最大值和最小值 root@ubuntu:~# cat max_min.sh #!/bin/bash declare -i min max declare -a nums for ((i=0;i<10;i++));do nums[$i]=$RANDOM [ $i -eq 0 ] && min=${nums[$i]} && max=${nums[$i]}&& continue [ ${nums[$i]} -gt $max ] && max=${nums[$i]} [ ${nums[$i]} -lt $min ] && min=${nums[$i]} done echo “All numbers are ${nums[*]}” echo Max is $max echo Min is $min root@ubuntu:~# bash max_min.sh “All numbers are 31226 5817 24046 15179 27696 24069 15955 24371 15002 1984” Max is 31226 Min is 1984 root@ubuntu:~# bash max_min.sh “All numbers are 7302 11216 31002 17231 8879 10572 27876 5576 15367 1989” Max is 31002 Min is 1989 3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序 升序: root@ubuntu:~# cat bubble.sh #!/bin/bash arr=( 14 5 78 92 2 99 45 68 ) len=${#arr[*]} echo "原始数组是:" ${arr[*]} for ((j=0;j<$len;j++));do for ((i=0;i<$len-1;i++));do if [ ${arr[$i]} -gt ${arr[$i+1]} ];then x=${arr[$i]} arr[$i]=${arr[$i+1]} arr[$i+1]=$x fi done done echo "数组从小到大的顺序是:" ${arr[*]} root@ubuntu:~# bash bubble.sh 运行的结果 原始数组是: 14 5 78 92 2 99 45 68 数组从小到大的顺序是: 2 5 14 45 68 78 92 99 降序: root@ubuntu:~# cat bubble.sh #!/bin/bash arr=( 14 5 78 92 2 99 45 68 ) len=${#arr[*]} echo "原始数组是:" ${arr[*]} for ((j=0;j<$len;j++));do for ((i=0;i /dev/null;then echo "$NET$ID lived,ping test Success" else echo "$NET$ID lived,ping test Fail" fi } & done wait while脚本: root@ubuntu:~# cat while_ping.sh #!/bin/bash # NET=192.168.0. declare -i ID=1 while [ $ID -le 254 ];do { ping -c1 -w1 $NET$ID > /dev/null if [ $? -eq 0 ];then echo echo "$NET$ID lived,ping test Success" else echo "$NET$ID not lived,ping test Fail" fi } & let ID++ done wait 6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间 先写好一个备份的脚本 root@ubuntu:/data/scripts# cat etcback.sh #!/bin/bash # PATH=/apps/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin BACKDIR="/backup" FILENAME_TAR="etcbak-"`date -d "-1 day" +%F-%H`".tar.xz" [ ! -d $BACKDIR ] && mkdir -p /backup tar Jcvf $BACKDIR/$FILENAME_TAR /etc &>/dev/null 在使用crontab -e来添加任务 root@ubuntu:/data/scripts# crontab -e root@ubuntu:/data/scripts# crontab -l # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command 30 1 * * 1-5 /data/scripts/etcback.sh

尚美源码教程库提供精美的网站源码教程,小程序、公众号、H5、APP、游戏、直播、支付、区块链、商城、影音、小说等源码信息大全。
用户必须遵守《计算机软件保护条例(2013修订)》第十七条:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。鉴于此条例,用户从本平台下载的全部源码(软件)教程仅限学习研究,未经版权归属者授权不得商用,若因商用引起的版权纠纷,一切责任均由使用者自行承担,本平台所属公司及其雇员不承担任何法律责任。
尚美源码教程库 » shell进阶、进程和计划任务
赞助VIP 享更多特权,立即登录下载海量资源
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡