Find the answer to your Linux question:
Results 1 to 3 of 3
Hello I'm running a command using ssh: for ex: ssh xyz-server "for i in 1 2; do ls -ltr /a/b/c/d; echo; done" | tail Pls NOTE: - when im doing ...
  1. #1
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Unhappy ssh command with pipe - how to get return code

    Hello

    I'm running a command using ssh:

    for ex:

    ssh xyz-server "for i in 1 2; do ls -ltr /a/b/c/d; echo; done" | tail


    Pls NOTE:
    - when im doing "ssh xyz-server" then ssh session is taking me to this server
    without giving password.

    Question:
    If i change the server name to any invalid server name i.e. xyz-server111 or xyz-server222 then i'm getting the below error mesg:

    ssh: xyz-server111: Temporary failure in name resolution

    and i was trying to get the return code of the ssh command i.e. if the server is not valid (ssh will give this error / will not work/ perform any ls -ltr..) then it will return a non-zero value. But as I'm using "|" at the end of ssh command i'm not able to know whether ssh command actually ran good or not. With "| tail" at the end of ssh command and wrong server name (with above error mesg), i'm getting return code($?)= 0.

    Any help!

    brgds
    /Arun Sangal
    720 251 9962
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Check bash variable PIPESTATUS. It is an array containg the return status of each command in the string of commands that were piped. The ssh command is the 1st command so check element 0.

    Code:
    echo ${PIPESTATUS[0]}

  3. #3
    Just Joined!
    Join Date
    Sep 2006
    Posts
    2
    use something like this

    ssh -T mylinux.com -l username -c "commands to execute with semicolon separated" `<dev/null >/dev/null 2>&1`

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...