2018.02.08

今天验证 sh 脚本直接拼接 web 输入作为命令执行,存在命令注入问题,学到如下几点:


1. Linux expect 脚本

1)完成用户和命令行之间的交互,比如,脚本中远程 ssh 登陆系统,需要按提示输入 username password,bash 脚本无法完成,此时就需要用到expect。

2)expect 脚本默认安装在 /usr/bin/expect(可以通过 whereis expect 查看),所以需要在前面指定解释器:

纯 expect 脚本:#!/usr/bin/expect

sh 脚本中嵌套 expect 脚本:/usr/bin/expect

3)expect 脚本关键词

spawn:后跟命令,开启一路会话。比如,spawn ssh [email protected]

expect:一般跟在命令执行后面,后跟字符串,预期接收到指定字符串。比如,expect "Please input your password:"。

send:后跟字符串,向对端发送指定字符串。比如,send "Change_Me"。

4)expect 脚本实例,实现自动登录服务器,删除某个目录下的日志文件:

#!/usr/bin/expect

set username [lindex $argv 0]

set password [lindex $argv 1] 

set ip [lindex $argv 2]

set timeout 10

spawn ssh $username@$ip

expect "*password*"

send "$password"

send "rm -f /opt/*log"

expect eof

你可能感兴趣的:(2018.02.08)