R
R
Roma2019-04-24 17:39:19
linux
Roma, 2019-04-24 17:39:19

How to execute a command on the server when connected via ssh?

Good afternoon, colleagues.
When connecting to some servers, the first thing I do is run the command

sudo screen -xR admin_name -c /root/.screenrc_admin_name
to use my screen config, which already pulls up my bashrc and so that my config doesn't get in the way of my co-workers. This is not very convenient. Enter every time
ssh server1 "sudo screen -xR admin_name -c /root/.screenrc_admin_name"
I also don't want to. I would like to make this command run automatically. But here's how to implement it?
Option 1. Write to ~/.ssh/authorized_keys on all servers
command="sudo screen -xR admin_name -c /root/.screenrc_admin_name" ssh-rsa ....
not suitable, because after that scp stops working, etc. Yes, and it is inconvenient if there are a lot of servers.
Option 2: Use ~/.ssh/config config
I didn't find an option. Although, ideally, I want to implement it through it.
Option 3. Make an alias
alias server23='ssh [email protected] "sudo screen -xR admin_name -c /root/.screenrc_admin_name"'
Also not an option. Personally, it's not convenient for me to enter just the server name. Get into the habit of writing "ssh server1" (all servers are in ~/.ssh/config).
Option 4: Make a function in ~/.bashrc
ssh() {
  ssh "$1" -F ~/.ssh/config "sudo screen -xR admin_name -c /root/.screenrc_admin_name"
}
This is an option. At the moment it has been done.
But maybe there is another, better way?
PS: I am authorized on servers by keys (for everyone).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2019-04-25
@saboteur_kiev

In principle, you can try to add a check to the client's IP in .bashrc, since you always log in from your computer.
Take the $SSH_CLIENT variable, cut off the first column, compare it with the IP of your computer. If it matches, then it was you who connected, and you can turn on your configs.

CLIENT_IP="${SSH_CLIENT%% *}"
if ; then
  source ~/.bashrc_Angel2S2
fi

This way your settings won't be loaded by colleagues and .bashrc won't break scp.
Well, you can list a few of your cars.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question