Avatar of weblocked
weblocked
 asked on

Automatically run a second command everytime the "ssh" command is run. Bash.

My request may be simple, but I'm not very familiar with bash so any detailed help would be nice. I want to run the "curl" command everytime any user on this server runs the "ssh" command. The users all run bash, is there a way this can be achieved? Perhaps through bashrc or some other method?
Shell ScriptingLinuxLinux OS Dev

Avatar of undefined
Last Comment
weblocked

8/22/2022 - Mon
RitBit

You can make an system wide alias for ssh:

alias ssh "curl <URL> ; ssh $@"
weblocked

ASKER
RitBit,

Thank you, can you please explain why the following doesn't recognize the $@ in the echo?
alias ssh='echo "$@"; ssh $@'

I want to use the arguments passed to SSH in the curl URL but its not working. With the above alias, SSH still works. And I can change it to echo a random letter, for example. But, it won't echo the argument. I can also pass the entire command to CURL if there is a variable for that. Is there?
RitBit

I'm not sure if I understand what you are trying to achive, but if you want to use the entire set of parameters into and url you have to encode it first so  it contains valid characters for an url...  In that case a little wrapper-script might become handier that an alias.

In that case you can encode the parameters and add them to the url, if you like I'm happy to generate such a script for you but please confirm if that is what you are looking for.

Please note you can also select a specific parameter by number, by using $1 for the first parameter, $2 for the second parameter and so on... ($0 will contain the command you executed, ssh in this case).
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
simon3270

An alias is used to replace a single text word at the start of the command line with some expanded text - it doesn't handle parameters at all.  What is happening here is that the "ssh" alias is expanding to "echo ; ssh", and the parameters are then added to the end of the command line*after* the alias has been expanded, to give "echo ; ssh arg1 arg2 arg3...."  It only appears as if the alias has handled the parameters.

You would be better off with a function.
weblocked

ASKER
Hi simon3270, that makes sense cause nothing was allowing the "Echo" to actually echo the arguments. I've decided due to the complexity and my lack of knowledge on bash to put this in the backburner. But if it is easy for you to make an example function, I'd love to see it.
ASKER CERTIFIED SOLUTION
RitBit

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
RitBit

agreed, putting the full path is best to avoid looping or running an other executable in the path... :-)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
weblocked

ASKER
Thanks guys! This is great! :)