两个脚本搞定
1.qssh_qunero.sh ,调用了脚本二:my_ssh.expect
#!/bin/sh
# qssh_qunero.sh
echo usage:qssh_qunero.sh host_id
echo "1) 192.168.8.241"
echo "2) 192.168.8.33"
echo "3) 192.168.8.34"
echo "4) 192.168.8.35"
echo "5) 192.168.8.36"
case $1 in
1) my_ssh.expect 192.168.8.241;;
2) my_ssh.expect 192.168.8.33;;
3) my_ssh.expect 192.168.8.34;;
4) my_ssh.expect 192.168.8.35;;
5) my_ssh.expect 192.168.8.36;;
*) echo "wrong host_id! make sure it's in [1..5]!"
esac
2.脚本二:my_ssh.expect
#!/usr/bin/expect -f
# my_ssh.expect
set timeout 20
set sshhost [lindex $argv 0]
spawn ssh -l 用户名 $sshhost
expect "password:"
send "你的用户密码\r"
interact
注:可能需要自行安装expect解释器;将以上两个脚本保存好,修改权限,建议为 700;
3.改良版 修改脚本一,提供默认参数:
#!/bin/sh
echo usage:qssh_qunero.sh host_id
echo "1) 192.168.8.241"
echo "2) 192.168.8.33"
echo "3) 192.168.8.34"
echo "4) 192.168.8.35"
echo "5) 192.168.8.36"
i=$1
if [ $# = 0 ];then
i=2
echo "use default host_id :2)192.168.8.33"
fi
case $i in
1) my_ssh.expect 192.168.8.241;;
2) my_ssh.expect 192.168.8.33;;
3) my_ssh.expect 192.168.8.34;;
4) my_ssh.expect 192.168.8.35;;
5) my_ssh.expect 192.168.8.36;;
*) echo "wrong host_id! make sure it's in [1..5]!"
esac
评论