명령은 제대로 작동하지만 SSH를 통해 원격으로 실행하면 실패합니다.

명령은 제대로 작동하지만 SSH를 통해 원격으로 실행하면 실패합니다.

서버에서 실행하면 다음 명령이 원격 AiX 서버에서 제대로 작동합니다.

ls -ltr /tmp/*.pid  | grep "$(date '+%b %e')"

산출:

-rwx-rwx-rw-- ........ Oct 1 /tmp/new.pid

echo $?
0

를 사용하여 위 명령을 실행하려고 했으나 ssh실패했습니다. 아래 출력을 참조하세요.

ssh [email protected] -C 'ls -ltr /tmp/*.pid  | grep "$(date "'"+%b %e"'")"'

echo $?
1

참고: 이 명령은 ansible 자동화를 통해 획득됩니다 "$(date '+%b %e')".grep "$(date "'"+%b %e"'")"

-name: Chek is pid file was modified today
 raw: "ls -ltr /tmp/{{ another_folder }}/*.pid  | grep \"$(date '+%b %e')\""
 register: pidfiledet

이 문제를 해결하고 명령을 원격으로 실행하는 방법을 제안해 주시겠습니까?

업데이트: 답변 및 설명 섹션에서 사용자가 제안한 명령을 실행했지만 ssh가 실패했습니다. 다음은 제안된 답변에 대한 디버그 모드에서 실패한 SSH의 출력입니다.

$ ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/app/mysshkeys/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="tbaadm"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o ControlPath=/home/targetuser/.ansible/cp/f623d39604 -tt 10.9.9.12 'ls -ltr /tmp/prjfolder/bin/*.pid | grep "$(date +%b\ %e)"'
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug3: kex names ok: [curve25519-sha256,[email protected],diffie-hellman-group14-sha1,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,gss-gex-sha1-,gss-group14-sha1-]
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 73756
debug3: mux_client_request_session: session request sent
debug1: mux_client_request_session: master session id: 2
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 1
Shared connection to 10.9.9.12 closed.

SSH를 사용하여 전달된 위 명령은 대상 호스트에서 로컬로 실행될 때 제대로 작동합니다!

답변1

불행히도 나는 그것에 익숙하지 않지만 혼합에 ansible있는 것 같고 ansible문제는 참조를 깨뜨리는 방식으로 명령을 변환하는 또 다른 프로세스이므로 명령의 해당 부분에서 참조를 제거하는 것이 도움이 될 수 있습니다.

다음과 같은 기본 명령을 시도해 볼 가치가 있습니다.

ls -ltr /tmp/*.pid  | grep "$(date +%b\ %e)"

번역가가 이 내용도 깨뜨릴 수 있을지 의문이 남아 있습니다.

답변2

무슨 뜻인지 설명하지 않았잖아"실패하다"그럼 추측만 할 수 있겠네요

  1. 원격으로 실행하기 위해 전체 파이프라인을 패키징하는 대신 필요한 부분을 원격으로 실행한 다음 나머지 부분을 로컬에서 실행할 수 있습니다.

     ssh [email protected] -C 'ls -ltr /tmp/*.pid' | grep "$(date '+%b %e')"
    

    이 작업의 가장 큰 장점은(확실히 제 생각에는) 참조를 단순하고 명확하게 유지한다는 것입니다. 네트워크 파이프를 통과하는 데이터 양과 시간대 차이(예 date:

소스 머신의 시간대를 알고 있다면 date로컬에서 실행하는 경우에도 사용하도록 지시할 수 있습니다.

    ssh [email protected] -C 'ls -ltr /tmp/*.pid' | grep "$(TZ=Europe/Paris date '+%b %e')"
  1. 종료 상태 코드는 단순히 grep일치하는 항목이 없음을 의미할 수도 있습니다. 귀하의 경우 이것이 버그인지는 모르겠지만 ssh명령의 최종 종료 상태를 반환하는 것은 정상적인 동작입니다.

     ssh remoteHost true; echo SS=$?
     SS=0
     ssh remoteHost false; echo SS=$?
     SS=1
    

답변3

Ansible을 모르지만 이것은 유효한 ssh 명령입니다.

ssh [email protected] 'touch /tmp/test.pid; ls -ld /tmp/*.pid | grep -- "$(date "+%b %d")"'

GNU!binutils참고: AIX 서버에 설치 했더라도 ssh를 통해 실행되는 명령은 /opt/freeware/bin또는 없이 최소 경로를 사용하므로 /usr/local/bin다음을 사용하려면 전체 경로를 사용하십시오 GNU!ls.

ssh [email protected] 'touch /tmp/test.pid; /usr/local/bin/ls -ld /tmp/*.pid | grep -- "$(date "+%b %e")"'

편집: date%-시퀀스 정보: GNU!ls 와 호환됩니다 AIX!ls.%d%e

관련 정보