Link to home
Start Free TrialLog in
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?
Avatar of RitBit
RitBit
Flag of Netherlands image

You can make an system wide alias for ssh:

alias ssh "curl <URL> ; ssh $@"
Avatar of weblocked
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?
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).
Avatar of 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.
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
Avatar of RitBit
RitBit
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
agreed, putting the full path is best to avoid looping or running an other executable in the path... :-)
Thanks guys! This is great! :)