0

I am working on a script in linux in which i need to SSH to another server.

I am using this syntax ssh user@ip

When doing a manual SSH when we type "ssh user@ip" a few second, a second prompt will show and ask what environment are we choosing ex 1 to 4

(4) - this is the number of the environment i will be choosing.

But when doing ssh user@ip 4 - error received (bash command not found)

Here is the image on what is the prompt when using ssh

cjpm
  • 1
  • 7
  • Do you have control over the account/environment on the target machine? If you're *always* going to go to the same environment it might be easier to modify the process you get to see on logging in. It's most likely part of `~/.bash_login`, `~/.bash_profile` or `~/.profile` ... – tink Mar 17 '22 at 22:59
  • Hi @tink Yes I do have access on this. Mind to explain the process on SSH on these said files? – cjpm Mar 18 '22 at 07:30
  • If you do `ssh user@ip "bash --noprofile"` you might (depending on whether or not they make use of *forced commands*) be able to get a shell without the prompt. You can then check those files whether they invoke anything that would present you with these options; if they do (if any of them does) you should be able to edit it appropriately. – tink Mar 18 '22 at 08:04

1 Answers1

0

Without knowing what happens after you select the environment, its probably something like:

echo 4 | ssh user@ip

Feed 4 to whatever is prompting for environment, then exit.

If you need to stay connected, then something like

echo 4 | ssh -tt user@ip "ksh -l"

ksh -l could be replaced by some other shell or command like vi foo.txt (here when the command exits, the connection closes).

The above is similar to this previous question: Run ssh and immediately execute command

Anthony C Howe
  • 781
  • 5
  • 6
  • Hello @Anthony C Howe Thanks for the help. Upon trying the echo 4 | ssh user@ip. It doesn't work on my end. I will try to elaborate more on what happens. This is the scenario: I enter ssh user@ip after pressing enter it will ask a input from the user to pick what environment to enter on the server. step 1: ssh user@ip step 2: input what environment (1 to 4) step3: arrive at the home of that environment – cjpm Mar 17 '22 at 21:19
  • Sounds likes a custom script used as your initial "shell" to redirect you to an environment. Are the environments using a known unix shell or something else? Not enough info in OP as to environments. Can you explain more about environment and what your objective is? – Anthony C Howe Mar 18 '22 at 12:29