标签:shell脚本
倒计时脚本:
1 #!/bin/bash
2 read -p "please input the num of hour: " hour
3 read -p "please input the num of minutes:" minutes
4 read -p "please input the num of sec: " s
5 sec=$hour*60*60+$minutes*60+$s
6 for ((sec=$sec;sec>0;sec--))
7 do
8 echo -ne "Come on,$(($sec/(3600))) hours $((($sec/60)-60)) minute $((($sec%3600)%60)) s later , New Year is coming... "
9 echo -ne "\r \r"
10 sleep 1
11 done
作业脚本(不完全版本):
文件1: /mnt/11/scan.sh
内容:
1 #!/bin/bash
2 for num in {1..30}
3 do
4 ping -c1 -w1 172.25.254.$num &> /dev/null && (
5 /mnt/11/scp.exp 172.25.254.$num
6 /mnt/11/ssh.exp 172.25.254.$num
7 )
8 done
文件2: /mnt/11/ssh.exp
内容:
1 #!/usr/bin/expect
2 set timeout 2
3 set ip [lindex $argv 0]
4 spawn ssh root@$ip sh /mnt/11/creat/creatuser.sh
5 expect {
6 "yes/no" {send "yes\r";exp_continue}
7 "password" {send "redhat\r"}
8 }
9 expect eof
文件3: /mnt/11/scp.exp
内容:
1 #!/usr/bin/expect
2 set timeout 2
3 set ip [lindex $argv 0]
4 spawn scp /mnt/11/creat/creat_user.sh root@$ip:/mnt
5 expect {
6 "yes/no" {send "yes\r";exp_continue}
7 "password" {send "redhat\r"}
8 }
9 expect eof
10
11 spawn scp /mnt/11/creat/userfile root@$ip:/mnt
12 expect {
13 "yes/no" {send "yes\r";exp_continue}
14 "password" {send "redhat\r"}
15 }
16 expect eof
17
18 spawn scp /mnt/11/creat/passwdfile root@$ip:/mnt
19 expect {
20 "yes/no" {send "yes\r";exp_continue}
21 "password" {send "redhat\r"}
22 }
23 expect eof
文件4: /mnt/11/creat/creatuser.sh
内容:
1 #!/bin/bash
2 if [ ! -f /mnt/11/creat/userfile ];then
3 echo "userfile is not exist"
4 exit 1
5 fi
6 if [ ! -f /mnt/11/creat/passwdfile ];then
7 echo "passwdfile is not exist"
8 exit 1
9 fi
10
11 line1=$(wc -l /mnt/11/creat/userfile | cut -d " " -f 1)
12 line2=$(wc -l /mnt/11/creat/passwdfile | cut -d " " -f 1)
13
14 if [ ! "$line1" -eq "$line2" ];then
15 echo "the user and passwd not match"
16 exit 1
17 fi
18
19 for num in $( seq 1 $line1 );do
20 name=`sed -n ${num}p /mnt/11/creat/userfile`
21 pass=`sed -n ${num}p /mnt/11/creat/passwdfile`
22 useradd $name &> /dev/null
23 if [ "$?" -eq 0 ];then
24 echo $pass | passwd --stdin $name &> /dev/null
25 echo "$name is success"
26 else
27 echo "$name is exits"
28 fi
29 done
文件5: /mnt/11/creat/userfile
内容:
lzt1
lzt2
lzt3
文件6: /mnt/11/creat/passwdfile
内容:
123
123
123
运行:
chmod +x /mnt/11/ssh.exp
chmod +x /mnt/11/scp.exp
sh /mnt/11/scan.sh
本文出自 “12097560” 博客,请务必保留此出处http://12107560.blog.51cto.com/12097560/1882990
标签:shell脚本
原文地址:http://12107560.blog.51cto.com/12097560/1882990