2

I frequently connect to ephemeral servers. I'd like to be able to set up some commonly-used aliases on those servers upon connection to them. I thought maybe I could create a bash function/alias which took the target machine's hostname as an argument, then did the following

  • open an ssh session to that server
  • copy a file of aliases I'd like to use on that server to the server and source them so they are usable immediately

Is this straight-forward to do? I could probably figure this out but since an answer didn't come up in search it's no harm to ask here and give someone the points, right? :)

conorgriffin
  • 459
  • 1
  • 6
  • 25

2 Answers2

1

You had the right idea with copying an aliases file. The tricky bit is loading them in the current context. The only way I've found is by running a new instance of bash with the file passed in:

bash --rcfile uploaded_aliases_file

This should work:

function ssh.test {
  TMP_RC=server_aliases
  scp rc_file user@0.0.0.0:${TMP_RC}
  ssh user@0.0.0.0 "bash --rcfile ${TMP_RC} && rm ${TMP_RC}"
}

Or with a kubernetes pod where you pass the pod name:

function kube.bash {
  TMP_RC=server_aliases
  kubectl cp server_rc $0:$TMP_RC
  kubectl exec -it $0 -- bash -c "bash --rcfile ${TMP_RC} && rm ${TMP_RC}"
}

Note that I'm adding a command to delete the aliases, which you may or may not want, and is a bit flaky as it runs after your bash session, and may not run if the connection is interrupted etc...

You could probably make the file delete itself.

andyhasit
  • 173
  • 1
  • 7
0

everything has already been done before us.

there is such a funny script as the ssh layer.

In fact, it transfers itself and other files to the server in the same session, which does not require additional confirmation of the use of ssh-key.

https://github.com/cdown/sshrc