远程安装软件(1)

2017-10-27  本文已影响0人  风千寻艾

要求

脚本实现在A机器,远程安装软件package到B机器

思路

将需要安装的软件包package从A拷贝到B,然后从A机器ssh到B远程执行安装脚本

为了用户能更方便的安装xx服务,决定用shell写一个工具,实现在本地远程安装服务包,限时1天,由于之前shell写得少,实现过程中遇到很多比较基础的问题,记录一下

实现version1

业务相关信息用xxx param4等屏蔽

#!/bin/bash

function help()
{
#usage
cat << HELP
------------------------------------------------------------------------------
please input 4 parameters in order:
    1st: path of xxx package,
    2nd: ip of the host which xxx will install
    3rd: root's paasword of the host you inputted just now(2nd parameter)
    4th: param4
Example:
    /opt/test/xx.zip 192.168.0.2 YourPaasWord param4

HELP
}

function install()
{
read -a array
echo ${array[*]}
scp ${array[0]} root@${array[1]}:/
echo "bash setup.sh -param ${array[3]}"
ssh root@${array[1]}  "mkdir -p /opt/test && \
    cd /opt/test && \
    cp /xx-*.zip ./ && \
    unzip xx-*.zip && \
    cd /opt/test/bin && \
    bash setup.sh -param ${array[3]} "
#    bash setup.sh -param ${array[3]} >/dev/null 2>&1 &"

################################################
# ssh root@${array[1]}  'mkdir -p /opt/test && \
#    cd /opt/test && \
#    cp /xx-*.zip ./ && \
#    unzip xx-*.zip && \
#    cd /opt/test/bin && \
#    bash setup.sh -param ${array[3]} '
################################################  
}

while [ 1 ]
do
    help
    install
    sleep 1
done

问题记录

  1. shell语言中&& & ; 区别

If previous command failed with ; the second one will run.
But with && the second one will not run.This is a "lazy" logical "AND" operand between operations.

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

  1. 传参接收不到

按照被注释调ssh写法,即

################################################
# ssh root@${array[1]}  'mkdir -p /opt/test && \
#    cd /opt/test && \
#    cp /xx-*.zip ./ && \
#    unzip xx-*.zip && \
#    cd /opt/test/bin && \
#    bash setup.sh -param ${array[3]} '
################################################  

ssh执行bash setup.sh -param ${array[3]}命令时,${array[3]}值一直为空,不管怎么样都传递不进去。
中间google了很多答案,都没解决,后来抱着解决问题优先的态度,打算先把变量值写到远端主机的一个临时文件里,用的时候再读取(用tempvar=$(cat /opt/test/bin/temp.txt)tempvar=$(< /opt/test/bin/temp.txt)读取到变量tempvar,再使用),试了也不行;
后来折腾了一个多小时,发现是低级错误(初学者的代价啊cry),将ssh需要执行的命令用"引起了(而非')就好了,即ssh root@${array[1]} 'mkdir -p /opt/test && \ mkdir前的'以及最后的'换成"

贴一个区分单引号、双引号、反引号的链接:
http://www.cnblogs.com/gx-303841541/archive/2012/10/25/2740333.html

  1. ssh登陆到远端主机执行的命令怎么能不显示

使用>/dev/null 2>&1重定向(源码中被注释掉了# bash setup.sh -param ${array[3]} >/dev/null 2>&1 &"

>/dev/null 2>&1是什么鬼?

>is for redirect
/dev/null is a black hole where any data sent, will be discarded
2 is the file descriptor for Standard Error
> is for redirect
& is the symbol for file descriptor (without it, the following 1 would be considered a filename)
1 is the file descriptor for Standard Out
Therefore >/dev/null 2>&1 is redirect the output of your program to /dev/null. Include both the Standard Error and Standard Out.


Reference
https://stackoverflow.com/questions/6152659/bash-sh-difference-between-and
https://stackoverflow.com/questions/13338870/what-does-at-the-end-of-a-linux-command-mean
https://stackoverflow.com/questions/3314660/passing-variables-in-remote-ssh-command
https://unix.stackexchange.com/questions/163352/what-does-dev-null-21-mean-in-this-article-of-crontab-basics

上一篇下一篇

猜你喜欢

热点阅读