0

This question:

https://superuser.com/questions/355029/linux-how-to-automatically-run-commands-on-ssh-login#355030

Only tells me how to run static commands. What it does not tell me is how I can feed it parameters. So say for example, I would like something like this to happen:

$ ssh [email protected] -p 2222 --parameter1 "boat programming"
Last login: Sat Dec 10 03:59:37 2016 from some place
boat programming

# .bashrc executes something like this:
echo $parameter1

If I have to use a language like expect, fine, but ideally I would like to keep this straightforward and simple.

1
  • @EricRenouf No; I'll be scripting this with a #!/bin/bash script. I tried doing what you said, having an auto ssh login followed by an echo command, however the command only executed after I exited my ssh session. Commented Dec 10, 2016 at 13:02

3 Answers 3

1

Save this short script (on your remote system) with name run in a directory which is part of your $PATH variable (on your remote system) and make it executable.

#!/bin/bash

echo "$2"

Use it this way:

ssh [email protected] -p 2222 run --parameter1 "'boat programming'"

Output:

boat programming
Sign up to request clarification or add additional context in comments.

3 Comments

I am having some issues getting this to execute. I made the executable on my remote system, placed the script in one of the $path's (/home/akiva/bin) but i'm getting this error regardless: bash: run: command not found
I looked at it; my presumption is that certain $PATH's will not work. However, providing a full absolute path did work. I also think that --parameter1 is more misleading than useful, as that will count as a parameter itself. I am trying to think of a way to qualify that parameter.
I suppose it does not work in directories which added to your $PATH variable by your .bashrc or .bash_profile. It works for me when I move run to /usr/local/bin.
1

The way, in theory, to do this is to put parameter1 in your environment, and let the remote process inherit it from ssh.

$ parameter1="boat programming" ssh [email protected] -p 2222

In practice, this probably will not work, because it requires the remote sshd agent to be running with the PermitUserEnvironment option enable, which is not true by default.

The only way to force this would be to run bash explicitly on the remote host with the appropriate environment.

$ ssh [email protected] -p 2222 'parameter1="boat programming" bash -i'

3 Comments

I'm playing around with your method as we speak. I'll let you know if I have any success, and accept if I do.
I'm not 100% sure the first line would work; I know ssh will consult ~/.ssh/environment for assignments to use on the remote end if sshd allows; I'm only guessing that it exports the local in-memory environment as well.
sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=no [email protected] -p 2222 parameter1="'happy day'" /home/akiva/bin/run -- can confirm that this works.
0

Here is my working solution:

0) If you want to automate password entry (Completely optional):

sudo apt install sshpass

1) SSH into your server, and create a file called run

#!/bin/bash
echo $parameter1

2) Make it executable

chmod +x run

3) Handle the parameters like this (Concatenate these lines.):

Avoid manual input of the password. (Be Careful!)
sshpass -p "$YOURPASSWORD"

Standard SSH login
ssh -o StrictHostKeyChecking=no [email protected] -p 2222

Parameters must go first. Use single quotes within double quotes otherwise day will be returned as an unknown command error:
parameter1="'happy day'"

Give an absolute path to the program you wrote.
/path/to/run

Alltogether:

 sshpass -p "$YOURPASSWORD" ssh -o StrictHostKeyChecking=no [email protected] -p 2222 parameter1="'happy day'" /path/to/run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.