ssh是遠程登錄命令,-l選項是最常用的選項,下面是我的一些總結
遠程登錄:ssh -l userName ip
# 遠程登錄到 10.175.23.9
ssh -l root2 10.175.23.9
執行遠程命令(不登錄):ssh -l userName ip command
# 不遠程登錄到 10.175.23.9,但通過命令查看10.175.23.9上面的nginx的進程情況
ssh -l root2 10.175.23.9 ps -ef|grep nginx
更多的情況是,command處為一個shell腳本,而配合expect spawn 一起使用,效果是最好的,比如下面這段shell腳本是實現同時刷新兩個ip主機的redis緩存的:
#!/bin/sh
echo "--------------------flushall_all_redis init--------------------"
Server09=("root1" "root1_pwd")
Server10=("root2" "root2_pwd")
function exe()
{
expect -c "
$1
set timeout 300
expect {
\"*(yes/no)?\"
{
send \"yes\n\"
expect \"*assword:\" {
send \"$2\n\"
}
}
\"*password:\"
{
send \"$2\n\"
}
}
expect eof
"
}
echo ""
echo "--------------------start reflush redis from 10.175.23.9--------------------"
exe "spawn ssh -l ${Server09[0]} 10.175.23.9 \"/home/weihu/bin/flushall_redis.sh\"" "${Server09[1]}"
echo ""
echo "--------------------start reflush redis from 10.175.23.10--------------------"
exe "spawn ssh -l ${Server10[0]} 10.175.23.10 \"/home/weihu/bin/flushall_redis.sh\"" "${Server10[1]}"
上面例子中的 /home/weihu/bin/flushall_redis.sh 即為各自主機上面需要執行shell文件(用於刷新各自主機redis緩存的)。例子中的重點就是exe函數、以及ssh -l 兩部分。
