Zero's Blog

expect实现scp或ssh登录

expect 是一种交互语言,可以为 shell 脚本提供交互方式

  • scp
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
read -p "`whoami`@server Password:" -s -r passwd ;echo -e "\n"
passwd=$(echo ${passwd}|sed 's/[^[:alnum:]]/\\&/g')
expect -c "
set timeout 600
spawn /usr/bin/scp `whoami`@$1:/root/test.txt /root/
expect {
\"*yes/*no\" {send \"yes\r\"; exp_continue}
\"*assword\" {set timeout 300;send \"$passwd\r\";}
}
expect eof"|grep -v 'spawn'
  • SSH
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
read -p "`whoami`@$1 Password:" -s -r passwd ;echo -e "\n"
passwd=$(echo ${passwd}|sed 's/[^[:alnum:]]/\\&/g')
expect -c "
set timeout 600
spawn /usr/bin/ssh `whoami`@$1 $2;
expect {
\"*yes/*no\" {send \"yes\r\"; exp_continue}
\"*assword\" {set timeout 300;send \"$passwd\r\";}
}
expect eof"|grep -v 'spawn'