0

I want to execute a script portion after a successful ssh connection has been established. I'd like to make it easier for me to tail a log file with a script I am trying to write.

Here is the script work in progress:

echo "[Log Tunnel]" 
 
if [ "$1" == "foo" ] 
        then 
                echo "connecting to foo.dev.company.net" 
                ssh foo.dev.company.net        
                tail -f var/logs/staff/backend/backend.log # what's the way to do this
fi 
 
if [ "$1" == "bar" ] 
        then 
                echo "connecting to bar.dev.company.net" 
                ssh bar.dev.company.net         
fi 

When I run the script I expect the following:

[Log Tunnel]
connecting to foo.dev.company.net
[email protected]'s password: ***********
# Output of Tail

I wonder if it is possible at all to have the ssh connection established and pass the subsequent shell a new script it should excecute right after startup.

Edit:

My goal is to have a following tail to a log file on a remote server in my shell. The scrip should ease the way to tail those log files my simple typing ./rtail.sh foo.When executing that command I expect the shell to display the particular tail output depending on the remote server I've chosen by the shell argument. I just want a shortcut for:

  • ssh to remote server
  • tail -f path/to/logfile.log
0

1 Answer 1

0

If I understand correctly, you are looking for something like this:


#!/bin/sh

echo "[Log Tunnel]" 
 
if [ "$1" = "foo" ] 
then 
  server="foo.dev.company.net"
  file="var/logs/staff/backend/backend.log"
elif [ "$1" = "bar" ]
then
  server="bar.dev.company.net"
  file="some/other/file"
fi

echo "connecting to $server" 
ssh "$server" tail -f "$file"
2
  • Can you elaborate how this is working? I just see a command after the ssh command. Is that all what's needed to "inject" a command into the subsequent remote shell?
    – xetra11
    Commented Jun 2, 2021 at 22:01
  • @xetra11 You should read the man page of ssh. You don't need to "inject" a command into a running remote shell. ssh can be used for different purposes. Running a specified command or running an interactive shell are two possibilities.
    – Bodo
    Commented Jun 2, 2021 at 22:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .