Log the output of ssh shell and preserve the exit status

189

Solution 1

checking ${PIPESTATUS[0]} worked for me...

if [ ${PIPESTATUS[0]} -eq 0 ]; then
  echo "successfull"
fi

echo ${PIPESTATUS[*]} prints the exit codes of all the pipeline commands.

Solution 2

The answer to : "how to log the output of ssh shell?" is

  • use a simple redirect >

e.g.

ssh user@remote_host "command" > local_log_file.txt

or

sshpass -p 'psswd' ssh username@server-2 "echo 'testing'; cp dir1/file1 dir2;" > local_machine_ssh_log.txt

If you want to check the result of the cp cmmand on the remote server, I'd suggest running the if statement on the remote host as part of the command you send, immediately after running the cp command

something like:

sshpass -p 'psswd' ssh username@server-2 "echo 'testing'; cp dir1/file1 dir2;if [ $? -eq 0 ]; then   echo "successfully copied"; else   echo "failed copying due to incorrect path"; fi" > local_machine_ssh_log.txt
Share:
189

Related videos on Youtube

Mike.Whitehead
Author by

Mike.Whitehead

Updated on September 18, 2022

Comments

  • Mike.Whitehead
    Mike.Whitehead over 1 year

    I'm building an Event site using Ruby on Rails and I'm not sure what code I can implement to monitor the bookings so as to ensure I don't allow bookings when the number of spaces available have been filled.

    I'm using simple_form to create events, this is the partial -

    <%= simple_form_for(@event) do |f| %>
        <% if @event.errors.any? %>
            <h2><%= pluralize(@event.errors.count, "error") %> prevented this     Event from saving:</h2>
        <ul>
            <% @event.errors.full_message.each do |message| %>
            <li><%= message %></li>
            <% end %>
        </ul>
    
    <%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %>
    <!-- The above code loop assigns a category_id to each event -->
    
    <%= f.input :image, as: :file, label: 'Image' %>
    <%= f.input :title, label: 'Event Title' %>
    <label>Location</label><%= f.text_field :location, id: 'geocomplete' %></br>
    <label>Date</label><%= f.text_field :date, label: 'Date', id: 'datepicker' %>
    <%= f.input :time, label: 'Time' %>
    <%= f.input :description, label: 'Description' %>
    <label>Number of spaces available</label><%= f.text_field :number_of_spaces, label: 'Number of spaces' %>
    <%= f.input :is_free, label: 'Tick box if Event is free of charge' %>
    <%= f.input :price, label: 'Cost per person (leave blank if free of charge)' %>
    <%= f.input :organised_by, label: 'Organised by' %>
    <%= f.input :organiser_description, label: 'Organiser description' %>
    <%= f.input :url, label: "Link to Organiser site" %>
    
    <%= f.button :submit, label: 'Submit' %>
    

    I have an association between the Event and Booking model. What code should I implement to ensure that the bookings for a particular event can be monitored? I'm quite new to Rails so this has me a little stumped.

    • drHogan
      drHogan almost 7 years
      In case you are using bash, see man bash -> PIPESTATUS. Also see pipefail.
  • Mike.Whitehead
    Mike.Whitehead about 8 years
    There isn't a separate booking form, as such. The form above creates the event including a section for stipulating the number of spaces available. I have a number_of_spaces attribute in my Event model. The association between the Event and Booking models is as you describe above.
  • Mike.Whitehead
    Mike.Whitehead about 8 years
    To book an event there is a box at the bottom of the event page which will take a user through to the payment page but, as you suggest I will need to write some code for free events also as over-booking could be an issue there too. The above looks pretty solid.
  • margo
    margo about 8 years
    The same logic applies, if the event is fully booked, don't show the the box that takes users through to the payment page. If the above has helped, please mark my answer as accepted or upvote it. Thx.
  • Mike.Whitehead
    Mike.Whitehead about 8 years
    Hi there, no this doesnt work. For some reason I don't think the database is logging the bookings as they're being made.
  • margo
    margo about 8 years
    You'll need to debug why bookings aren't being saved to the d/b.
  • Mike.Whitehead
    Mike.Whitehead about 8 years
    I'm not sure its a debugging issue, I think I'm missing some code that would save it.
  • margo
    margo about 8 years
    Strictly speaking why bookings aren't being saved is a different question. When you start a new question, show what code you have for creating/saving bookings.
  • Ejjagiri Venkatesh
    Ejjagiri Venkatesh almost 7 years
    But i have few commands to run on this server-1 immediately after cp command.
  • Yaron
    Yaron almost 7 years
    @EjjagiriVenkatesh - what exactly would you like to do, and check using the if statement?
  • Ejjagiri Venkatesh
    Ejjagiri Venkatesh almost 7 years
    in the place of commands i have to send a script file sometimes to execute, in that case how can we check. I have to check if all the commands in the sent script file are executed successfully or not? How can i check and log the output?
  • Yaron
    Yaron almost 7 years
    @EjjagiriVenkatesh - can you check the local_output.txt file in your local machine?
  • Ejjagiri Venkatesh
    Ejjagiri Venkatesh almost 7 years
    edited my question, can you please give me solution for this?