Notes on ssh connection using paramiko with the following settings in ssh_config and ssh connection to app_host via fumidai_host
Host fumidai_host
User user_name
Hostname xx.xx.xx.xx (<= GIP)
IdentityFile ~/.ssh/hoge.pem
Host app_host
HostName 192.168.0.1 (<= private ip)
User user_name
IdentityFile ~/.ssh/hoge.pem
ProxyCommand ssh -W %h:%p fumidai_host
You can connect like this. Sample to sftp for the time being
import os
import paramiko
#lookup ssh config file
config_file = os.path.join(os.getenv('HOME'), '.ssh/config')
ssh_config = paramiko.SSHConfig()
ssh_config.parse(open(config_file, 'r'))
lkup = ssh_config.lookup(hostname)
#Connect using ProxyCommand settings
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(
lkup['hostname'],
username=lkup['user'],
key_filename=lkup['identityfile'],
sock=paramiko.ProxyCommand(lkup['proxycommand'])
)
sftp = ssh.open_sftp()
#The rest is normal
# sftp.put(src, dist)Or
# sftp.get(src, dist)Or
sftp.close()
ssh.close()
Recommended Posts