linux ssh -l 命令运用

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 两部分。

posted @ 2016-04-10 20:48  快乐菠菜  阅读(26318)  评论(0编辑  收藏  举报