How to use a jumphost/jump server in a script
Something along the lines of:
ssh -At jumpserver ssh -At server1
should work. The -t
makes sure that a pseudo-tty is allocated where needed (it may not be necessary on the second one, but I don't have a setup where I can test that at the moment, and it won't hurt). This also has the benefit that when you exit from server1
, both sessions go away...
ssh: suppress Banners of Jumphosts
You can create an empty file on the target server in your users home directory called ~/.hushlogin
and it will stop the message of the day from displaying.
Just connect and run touch ~/.hushlogin
and the next time you log in it should be suppressed.
SSH tunnel forwarding with jump host and remote database
I figured it out. It works with a combination of ssh config settings and the SSHTunnelForwarder context manager from the sshtunnel library.
Using the following model and naming conventions:
[A: local host] -> [B: jump host] -> [C: target host] => [D: RDS MySQL host]
I set up my ~/.ssh/config to get from A to C through B:
Host C_ssh_shortcut
HostName C_host
User C_user
Port 22
ForwardAgent yes
ProxyCommand ssh B_user@B_host -W %h:%p
I added the key/keys I used to log in to B and C to my ssh-agent:
ssh-add
And finally I set up SSHTunnelForwarder:
import sqlalchemy
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
"C_ssh_shortcut", # The SSHTunnelForwarder "ssh_address_or_host" argument, which takes care of bypassing B through the ProxyCommand set up in ~/.ssh/config
remote_bind_address=(D_host, 3306), # Points to your desired destination, ie. database host on 3306, which is the MySQL port
local_bind_address=('', 1111) # Gives a local way to access this host and port on your machine. '' is localhost / 127.0.0.1, 1111 is an unused port
) as server:
connection_string = "mysql+pymysql://D_user:D_password@localhost:1111/D_dbname" # note that D_host and D_port were replaced by the host and port defined in "local_bind_address"
engine = sqlalchemy.create_engine(connection_string)
# do your thing
From here, I am able to use my engine as usual to interact with my database.
Related Topics
Where The Structure "Struct Page" Is Stored on The Linux Kernel
Git Clone Using Ssh Failed in Windows Due to Permission Issue
How to Tell Linux to Keep a Page and Not Evict It
How to 'Chmod -R +W' with Ant, Files and Folders
Why Does Munmap Needs a Length as Parameter
How to Run Sh File from Another Sh File
Fedora 28/Glibc 2.27 Libm.So.6 Logf() and Powf() C++
Terraform - Unable to Run Multiple Commands in Local Exec
Adding a Shell Command Inside/Inline of a Systemd Service File
In Linux, How to Do System Calls Through Gnu Arm Assembly
Javafx: Tested/Confirmed Hardware (Gpu) Acceleration on Linux
-Bash: /Usr/Bin/Virtualenvwrapper.Sh: No Such File or Directory
Kill Bash Script Foreground Children When a Signal Comes
Linux/Libusb Get Usb Device Path
Shopify + Ubuntu 12.04Lts + Faraday Issue = Ok to Use Older Openssl