3

how can we write a simple regular function which i can put in my bashprofile which can be used to secure console to any host i want.

but my secure console has to go through a jump host. that is the issue.

function func_name () {
 ssh jumphostname; 
 sc $hostname # from jump host secure console to another host given as input from terminal
}

this function only making to login in to jump host but not to secureconsole in to another host from there.

-bash-4.1$func_name host.me.com should give me console to host.me.com via jumphost

is function for this not possible? do i have to write a script?

stackuser
  • 101
  • 2
  • 7
  • 3
    No you need to be much more specific, and I suggest you re-word your question to be more appropriate, as this isn't so much about how to write a bash function, but more about ssh remote control from a local station. – hmedia1 Mar 29 '17 at 00:51
  • When I try to put together what you're trying to do, I end up with wondering why you can't just `ssh user@host` and work from there ? Like I said, if you are more specific, and perhaps give a usage case scenario, then people might be able to help. For versatile remote control and exception handling, I often use `expect` wrappers – hmedia1 Mar 29 '17 at 00:53
  • yes. i made the question more specific. – stackuser Apr 05 '17 at 23:04
  • There is an easy to follow process I explained on my other answer [here](https://stackoverflow.com/a/64092588/7551807) – Mussa Charles Sep 27 '20 at 19:43

5 Answers5

1

Here's how I do it.

  1. Create a functions folder at home
  2. Write my function as a shell script
  3. Reference the file as an alias in my bash_profile
  4. Reset the source

Example

mkdir ~/.functions
echo '#!/bin/bash
      echo $1' > ~/.functions/ekho
echo 'alias ekho="sh ~/.functions/ekho"' >> ~/.bash_profile
source ~/.bash_profile

Now you can call your method from any location for ever ever.

ekho "Wow"
Proximo
  • 6,235
  • 11
  • 49
  • 67
  • I was looking for something like this and it helped me creating an alias that also takes parameters. The only difference is I added the alias to ~/.bashrc instead of ~/.bash_profile, which is what I needed. – Lucas Camino Jun 25 '22 at 19:10
0

You should not use commands in a test [ ] unless you simulate a variable with $( ) arround the commands. Still not sure SSH will return something to the test. SSH needs the TTY you like connect to, and not the TTY you in at. This will causes problems!

An example without SSH ...

suleiman@antec:~$ if [ "$(cat ~/test.txt)" ]; then echo "Hello World"; else echo "failed"; fi
Hello World
suleiman@antec:~$ if [ "$(cat /this/file/dont/exsist 2>/dev/null)" ]; then echo "Hello World"; else echo "failed"; fi
failed

Addition:

-bash: sc: command not found

This means you have NOT installed the spreadsheet on the host.

This function only making to login in to jump host but not to secureconsole in to another host from there.

What you trying to do ?

Do you know what SSH does ? It opens remote TTYs, or with other words: it opens a remote secure console.

You cant run a script and put somewhere a ssh login in it, and then think all code after that will be in the new console, neither will that happen.

You can run ssh on a console, so you get your own TTY and put some commands in it. Or you use ssh in combination with some commands in a script, like

ssh user@host echo "Hello World!"

You can also pass some variables or text though ssh via

echo "Hello World!" | ssh user@host cat

There isnt much more you can do with it and you shouldn't!

Mario
  • 679
  • 6
  • 10
  • we create alias in bash_profile. it goes through. But after logging in i want to run sc command on that host from that jump host. – stackuser Mar 29 '17 at 00:30
  • `ssh user@host sc` is what you looking for ?.. I updated my post. – Mario Mar 29 '17 at 10:48
0

Below link would help you to go ahead

ssh username@host_address "command to execute"

For example output:

arul@OA2:~/work/images$ ssh arul@localhost echo "hai"
arul@localhost's password: 
hai
arul@OA2:~/work/images$

ssh arul@localhost command will login and "echo hai" command printed in currently logged in prompt"

Citation: https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/

  • Welcome to StackOverflow. Please note that link-only answers can usually be improved by actually explaining your intended answer yourself, including anything directly relevant from the link, and linking only for further information. See e.g. https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – kojiro Mar 29 '17 at 19:46
  • Thanks for your valuable inputs @kojiro – Arulpandiyan Vadivel Mar 30 '17 at 09:49
0

I would write this

con.sole() {
    if ! ssh -T jumphostname true; then
        printf 'Jump host "%s" not available.\n' jumphostname >&2
        return 1
    fi
    sc "$@"
}

The square brace isn't part of the if statement syntax. It is a separate command, the same as test.

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • above script gets me till jumphost. it cant sc after that con.sole hostname.com Warning: Permanently added 'jumphost.com (RSA) to the list of known hosts. -bash: sc: command not found – stackuser Mar 31 '17 at 18:22
-2

Its because you dont leave a whitespace between the if and the [... The the correct sintax you want is...

function con.sole
{
    if [ ssh jumphostname ]; then
        sc $1;
    else 
        echo "host not available"
    fi 
}

Greetings from Mexico!

  • after leaving space also it had -bash: [: ssh: unary operator expected so i added single quotes. it works now but real problem still exists. – stackuser Mar 29 '17 at 00:37
  • What are trying to do? Do you want to connect a server and then connect to another?... local->server1->server2? – Mr Octavious Mar 29 '17 at 00:42
  • I think you need to learn what `[` means in shell; it's not a grouping operator. – chepner Mar 29 '17 at 01:35