shell 脚本
1.把文件中txt结尾的文件改成log结尾的
# 1.查看a目录下的文件
[root@pingmm ~]# cd a/
[root@pingmm a]# ls
1.txt 2.txt 3.txt 4.txt 5.txt a.mp4 b.mp4 c.mp4 mm
# 2.脚本执行的test.sh
[root@pingmm ~]# cat test.sh
#! /bin/bash
cd /root/a
for i in `ls *.txt`
do
name=`echo $i |cut -d '.' -f1`
mv $i $name.log
done
2.expert
远程执行命令改多台密码
# 1.单台修改密码
[root@pingmm a]# # ssh root@远程ip echo 123 |passwd --stdin root
[root@pingmm a]# cat jiqi.sh
#! /bin/bash
user="root"
old_passwd="123"
new_passwd="12345"
ip=192.168.15.120
cmd="echo $new_passwd | passwd --stdin root"
expect << EOF
spawn ssh $user@$ip $cmd
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$old_passwd\n"}
}
expect eof
EOF
### 修改单台密码成功
[root@pingmm a]# sh jiqi.sh
spawn ssh root@192.168.15.120 echo 12345 | passwd --stdin root
The authenticity of host '192.168.15.120 (192.168.15.120)' can't be established.
ECDSA key fingerprint is SHA256:m8v19sQm1J7mUdBGbUm39TxE4GZOtqz2Xasc+Fglp4A.
ECDSA key fingerprint is MD5:c2:4b:5d:6c:45:e1:2f:44:2d:dc:fa:23:50:5f:76:68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.15.120' (ECDSA) to the list of known hosts.
Changing password for user root.
passwd: all authentication tokens updated successfully.
123
2.修改多台机器密码
# 先保存需要更改的内容
[root@pingmm ~]# cat pass.txt
egon:213ed878:123:192.168.15.120
root:02dcdc3c:1232:192.168.15.1
mm:0sdc3a:112:192.168.15.12
# 第一行代表用户
# 第二行代表旧密码
# 第一行代表新密码
# 第一行代表对方的IP
[root@pingmm ~]# while read line;do echo $line;done <pass.txt
# 执行这条命令会执行显示pass.txt里面的内容
egon:213ed878:123:192.168.15.120
root:02dcdc3c:1232:192.168.15.1
mm:0sdc3a:112:192.168.15.12
# 把pass.txt 里面的内容第一行的用户名截取出来
# echo $line | awk '{print $1}' ==其他同理
# 赋值给变量 user= `echo $line | awk '{print $1}'`
[root@pingmm ~]# vim count/expect1.sh
1 #! /bin/bash
3 while read line
4 do
5 (
6 user=`echo $line | awk -F: '{print $1}'`
7 old_pass=`echo $line | awk -F: '{print $2}'`
8 new_pass=`echo $line | awk -F: '{print $3}'`
9 ip=`echo $line | awk -F: '{print $4}'`
10 cmd="echo $new_pass | passwd --stdin root"
11
12
13 expect << EOF
14 spawn ssh $user@$ip $cmd
15
16 expect {
17 "yes/no" {send "yes\r";exp_continue}
18 "*assword" {send "$old_pass\n"}
19 }
20
21 expect eof
22
23 EOF
24 )&
25 done < pass.txt
3.批量执行命令
# 先保存需要更改的内容
[root@pingmm ~]# cat pass.txt
egon:123:192.168.15.120
egon:234:192.168.15.1
mm:112:192.168.15.12
[root@pingmm ~]# cat expect1.sh
#!/bin/bash
read -p "请输入您想要批量执行的命令: " cmd
while read line
do
user=`echo $line | awk -F: '{print $1}'`
pass=`echo $line | awk -F: '{print $2}'`
ip=`echo $line | awk -F: '{print $3}'`
# 修改单台机器的密码
expect << EOF
spawn ssh $user@$ip $cmd
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$pass\n"}
}
expect eof
EOF
done < pass.txt