Running a command on the remote host with interactive SSH (conditionally)

69

Solution 1

You could try something like this:

ssh server -t "do.sh; bash --login"

as suggested here: https://serverfault.com/questions/167416/change-directory-automatically-on-ssh-login

Or you could try using the 'LocalCommand' option in sshd_conf (or ~/.ssh/config) as described in the official man page: http://unixhelp.ed.ac.uk/CGI/man-cgi?ssh+1

Solution 2

One part of the puzzle: assuming OpenSSH, the ssh client will set the following environment variable:

SSH_CONNECTION Identifies the client and server ends of the connection. The variable contains four space-separated values: client IP address, client port number, server IP address, and server port number.

So you can use this information to find out where your connection comes from.

Share:
69

Related videos on Youtube

Jim Farkas
Author by

Jim Farkas

Updated on September 18, 2022

Comments

  • Jim Farkas
    Jim Farkas almost 2 years

    I'm going to attempt to create an open project which compares the most common MP3 download providers.

    This will require a user to enter a track/album/artist name i.e. Deadmau5 this will then pull the relevant prices from the API's.

    I have a few questions that some of you may have encountered before:

    1. Should I have one server side page that requests all the data and it is all loaded simultaneously. If so, how would you deal with timeouts or any other problems that may arise. Or should the page load, then each price get pulled in one by one (ajax). What are your experiences when running a comparison check?
    2. The main feature will to compare prices, but how can I be sure that the products are the same. I was thinking running time, track numbers but I would still have to set one source as my primary.

    I'm making this a wiki, please add and edit any issues that you can think of. Thanks for your help. Look out for a future blog!

    • Coren
      Coren over 12 years
      why you don't simply run ssh user@remotehost /path/to/do.sh ? If it works, you can even alias it in .ssh/config or your personnal .bashrc
    • Mat
      Mat over 12 years
      So you want to run "do.sh" when you log in to remotehost from somewhere, but not when you log in to remotehost from somehwere else? Which hosts do you connect from should trigger "do.sh", and which others shouldn't?
    • Jim Farkas
      Jim Farkas over 12 years
      Yes, I want to run it sometimes, but not all the time, the reason for this is that I have a keyboard shortcut from a launcher on my Mac that gives me what I want most of the time. I hit a shortcut and I want to ssh into the box and then reattach to a screen session, but I don't want to do that if I use a regular terminal like "ssh user@host". The solution mentioned below by Patkos does exactly what I need.
  • Jim Farkas
    Jim Farkas over 12 years
    Thanks so much!! ssh server -t "/home/user/do.sh; bash --login" worked for me. köszönöm szépen!
  • Patkos Csaba
    Patkos Csaba over 12 years
    Szivesen (You are welcome)
  • Vlastimil Burián
    Vlastimil Burián over 7 years
    @PatkosCsaba Please avoid other languages than English here. This applies also to comments.