0

Написал скрипт, который ищет в процессах java-процессы и выводит время, которое они работают и JMX порт. Не знаю, как правильно передать скрипт по ssh, чтобы на удаленном компьютере отработал. В виде с EOF выводит следующуе.

    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
    > 

Пробовал отдельным файлом скрипт передавать:

    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...... "

и это также не работает. Как исправить?

1
  • Как насчет копирования при помощи scp?
    – gbg
    29 окт 2018 в 2:24

1 ответ 1

0

копируешь скрипт на хост примерно так:

  sshArg="-o StrictHostKeyChecking=False"
    sshpass -p [пароль ssh]  scp -P [порт] -r ${sshArg} <что копируем> user@ip: 
    <куда копируем>

потом выполнить ран:

sshpass -p [пароль ssh] ssh -P [порт] -r ${sshArg} user@ip:<что раним>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.