Re: [问题] sudo 执行 expect 问题

楼主: kentyeh (kent)   2016-12-15 16:24:08
如果你的目的是ssh自动登录的话,可以提供我在用的脚本,
例如我公司的内部网址是 192.168.10. 开头,
登录 192.168.10.1 只要执行 $./sshp.sh 1 密码 就可以了
bash shell如下(如果登录不是root,请自行把root也改成参数):
#!/bin/sh
alive=$(ping -c4 192.168.10.$1 | grep 'received')
password=$2
if [ ! -z "$alive" ]; then
sed -i.old /^192\.168\.10\.$1/d ~/.ssh/known_hosts
expect -c "
set timeout 20
spawn ssh \"root\@192.168.10.$1\"
set timeout 5
expect {
\"Are you sure you want to continue connecting (yes/no)?\" {send
\"yes\r\"}
}
expect \"password: \"
send \"$password\r\"
expect \"]# \"
interact
"
else
echo 192.168.10.$1 无法连线
fi

Links booklink

Contact Us: admin [ a t ] ucptt.com