通过ssh

时间:2018-10-28 19:17:37

标签: bash ssh ps

我有剧本。它检查系统中是否正在运行某些Java进程,并显示运行时间和JMX端口号。如果我执行所有这些命令,只需在shell中键入它们-一切正常。但是,当我运行此脚本时,会出现错误。请帮助我。

    for ADDR in $(cat servers.list)
    do 
            ssh -tt $ADDR << EOF
            IFS=$'\n'
            for JAVAPS in $(ps ax|grep java|grep -v grep|grep -v javaps.sh|cut -d' ' -f3)
            do      
                    RUNTIME=$(ps -o etimes= -p $JAVAPS)
                    printf -v TIME '%d % 02d % 02d' `expr $RUNTIME / 3600` `expr $RUNTIME / 60 % 60` `expr $RUNTIME % 60`
                    echo 'Java application has been restarted ' $TIME ' ago.'
                    if [[echo $JAVAPS|grep jmxremote.port]]
                    then
                            JMXPORT=$(echo $JAVAPS|grep -oP '(?<=jmxremote.port=)(\S+)')
                            echo 'Jmx port=' $JMXPORT
                    fi
            done
            exit
    EOF
    done

这是我得到的:

    `root@d9gnome:/home# bash javaps.sh 
    error: list of process IDs must follow -p 

    Usage:
     ps [options]

     Try 'ps --help <simple|list|output|threads|misc|all>'
      or 'ps --help <s|l|o|t|m|a>' 
     for additional help text.

    For more details see ps(1).
    expr: синтаксическая ошибка
    expr: синтаксическая ошибка 
    expr: синтаксическая ошибка
        IFS=$'\n'
        for JAVAPS in 
        do
            RUNTIME=
            printf -v TIME '%d % 02d % 02d'   
            echo 'Java application has been restarted '  ' ago.'
            if [[echo |grep jmxremote.port]]
            then
                JMXPORT=    
                echo 'Jmx port=' 
            fi
        done
        exit
    Linux node12 4.9.0-7-amd64 #1 SMP Debian 4.9.110-3+deb9u2 (2018-08-13) x86_64

    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.

    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: SuThis what I get:

    root@d9gnome:/home# bash javaps.sh 
    error: list of process IDs must follow -p

    Usage:
     ps [options]

     Try 'ps --help <simple|list|output|threads|misc|all>'
      or 'ps --help <s|l|o|t|m|a>'
     for additional help text.

    For more details see ps(1).
    expr: синтаксическая ошибка
    expr: синтаксическая ошибка
    expr: синтаксическая ошибка
        IFS=$'\n'
        for JAVAPS in 
        do 
            RUNTIME=
            printf -v TIME '%d % 02d % 02d'   
        echo 'Java application has been restarted '  ' ago.'
        if [[echo |grep jmxremote.port]]
        then
            JMXPORT=
            echo 'Jmx port=' 
        fi
    done
    exit
    Linux node12 4.9.0-7-amd64 #1 SMP Debian 4.9.110-3+deb9u2 (2018-08-13) x86_64

    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.   

    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Sun Oct 28 21:34:54 2018 from 192.168.234.210
    root@node12:~# IFS=$'\n'
    root@node12:~# for JAVAPS in 
     > do 
    > 
       Display all 1611 possibilities? (y or n)
    > TIME=
    > 
    Display all 1611 possibilities? (y or n)
    > tf -v TIME '%d % 02d % 02d'   
    > 
    Display all 1611 possibilities? (y or n)
    :                                      nl
    !                                      nohup
    ./                                     nologin
    [                                      nproc
    [[                                     nroff
    ]]                                     nsenter
    {                                      nstat
    }                                      numfmt
    2to3                                   oakdecode
    2to3-2.7                               od
    2to3-3.5                               on_ac_power
    411toppm                               open
    accept                                 openssl
    accessdb                               openvt
    acpi_available                         opldecode
    addgnupghome                           orbd
    addgroup                               os-prober
    addpart                                ownership
    add-shell                              pack200
    adduser                                pager
    agetty                                 painter
    alias                                  palmtopnm
    _allowed_groups                        pam-auth-update
    _allowed_users                         pamcut
    animate                                pamdeinterlace
    animate-im6                            pamdice
    animate-im6.q16                        pamfile
    anytopnm                               pam_getenv
    apm_available                          pamoil
    applygnupgdefaults                     pamstack
    appres                                 pamstretch
    apropos                                pamstretch-gen
    apt                                    pam_tally
    apt-cache                              pam_tally2
    apt-cdrom                              pam_timestamp_check
    apt-config                             paperconf
    apt-extracttemplates                   paperconfig
    apt-ftparchive                         _parse_help
    apt-get                                __parse_options
    apt-key                                _parse_usage
    apt-listchanges                        partx
    apt-mark                               passwd
    apt-sortpkgs                           paste
    arch                                   pathchk
    arm2hpdl                               pbmclean
    arp                                    pbmlife
    >  has been restarted '  ' ago.'
    > .if [[echo |grep jmxremote.port]]
    > .then
    > .
    ./             .bash_history  .lesshst       .ssh/          
     ../            .bashrc        .profile       
    > .JMXPORT=
    > .
    ./             .bash_history  .lesshst       .ssh/          
    ../            .bashrc        .profile       
    > .echo 'Jmx port=' 
    > .fi
    > .done
    > .exit
    > 

ve tried to call script on remote node like this from another script file:

    for ADDR in $(cat servers.list)
    do  
            ssh -tt $ADDR 'bash -s' < java.sh`

并这样:

    for ADDR in $(cat servers.list)
    do 
            ssh -tt $ADDR "......code...... "

它也不起作用。怎么了?

0 个答案:

没有答案