Transparently connect to SSH machine using expect script - ssh

I have an expect script which runs ssh and eventually results in a shell. Is it possible to run this script whenever someone ssh's to the matching machine, instead of just using ssh? (ideally by setting some config in ~/.ssh/config)
I'd prefer not to create an alias/script for ssh to check what the host is, then run the appropriate shell.

Related

Sending data from client to server vie SSH tunnel

I am fairly new to ssh and still learning it. Recently I have made a tunnel connection with an ssh host and managed to successfully transfer data/files from my machine to the server with the command: scp file.extension user#hostIP:/directory/directory.
While this was successful, I am kinda struggling to reverse it, sending data/files from the server to the client. How would one go about completing that? Do I need to make some changes to ssh_config or just CLI commands are enough?
You need to change the order:
scp user#hostIP:/directory/directory file.extension
that's accomplishing the invert operation, off course, assuming that the address is correct, the file exists and you have the necessary privileges.

How to ssh multiple times by JSch: port forwarding, ssh command or SSH tunnel?

By terminal I am able to SSH multiple times to connect to the server:
(client--->gateway--->server1---->server2---)
But now to do it through JSch library of Java, how to go about it?
First tried portforwarding, but on terminal I am not doing that (not setting -R -L parameters in ssh).
Then I came across question How to SSH to a server behind another SSH server using JSch?, but I don't understand how to create tcp tunnel!
Port-forwarding is the best way to go.
You do not do port forwarding in the terminal, as you connect to the second section manually by typing the ssh command. While you can automate that using JSch, it is not really a reliable way to try to simulate a human being. If you want to replace the first ssh (terminal) step with JSch, for the same reason you do not want to use ssh for the second step. The accepted answer in the question you link to also discourages you from trying that. While when everything goes ok, it might work. But once any problem steps in, your will have troubles dealing with it automatically. For example, you can hardly automate host key verification for the second server.
The SSH tunnel is port forwarding. But maybe the mentioned ProxySSH (which does not seem to exists anymore) did internally without opening a local port, but used the "port forwarding" channel directly by the second session. But that's a way too complicated to implement. Stick with simple port forwarding.
For a complete example, see:
JSch multiple tunnels/jumphosts

SSH forward all X connections from DISPLAY

Right now at work we have a login machine where our home area is located and all tools are run on the compute farm, to run in GUI mode I believe the job is submitted to the farm and the selected machine will run the command with the DISPLAY variable set to what was in our local environment. This seems to only work with vnc right now, is there anyway I can use SSH and use a valid DISPLAY setting?
If you establish a SSH session with X protocol tunneling, you can query the value of the DISPLAY environment variable at the remote side. For example:
client$ ssh -X server
server$ echo $DISPLAY
localhost:17.0
This value is going to be different for each SSH session.
If I'm understanding your environment correctly, you'd need to pre-establish ssh sessions to all of the node in the compute farm. Then, when the job runs on a particular compute farm node, it'd have to set the particular DISPLAY variable that goes back on the ssh session you pre-established.

copy files from a ssh connected machine

I am trying to copy some files, from a computer that is located elsewhere.
Machine A connect to remote Machine B via ssh, pass a script, and this script will have to copy files from Machine B to Machine A.
I can't find a way to make this work, without open another connection to copy the files (tried rsync and scp). I would like to use the same ssh connection used to copy the script on Machine B, since I have keys that allow me to connect without password. This is to automate a process, so I need it to run without intervention.
What is the correct syntax to copy from remote machine B on machine A, using the ssh connection already open?
I can open a connection from Machine A to Machine B and run from Machine A the rsync command, but what I want is the other way: send from Machine B to Machine A, after that I ssh in Machine B.
I suspect the solution is easier than what I am thinking, but I can't really see it.
If by writing a script your goal is to avoid having to authenticate again, consider using the ControlMaster feature of ssh, which will allow further ssh sessions to authenticate over the existing one:
http://puppetlabs.com/blog/speed-up-ssh-by-reusing-connections
Then you could use scp, sftp or rsync over ssh to retrieve the files from the remote machine as Kenster suggests.

Unix: SCP (over SSH) fails due to interactive promot

I have this problem:
I have a server to which I ssh, and it has a special prompt request. The prompt is done by a ?prompt command.
It is fine with SSH, since the prompt I guess gets some input, but when I use SCP, the copy always fails.
So, I was wondering if there is maybe a flag for ssh and scp - so that interactive prompts are ignored.
(By the way, I need the prompt, so removing it isn't an option).
THANKS.
The prompt and response go into SSH stdio channels that are what scp uses to talk to the remote slave scp process, so it breaks the hand-shaking, and the transfer is aborted.
The right way to add additional prompts to SSH connections is through the keyboard-interactive authentication mechanism, probably with the help of some PAM module.
Your current approach just cripples SSH beyond simple usage patterns.