linux:vpn_clients:auto_vpn_and_ssh_sctript

Script: Connect to VPN, and then SSH into remote server

Puting passwords in scripts is insecure! Use ssh keys instead.

Assumption: You are using network manager, and have a VPN connection named VPN1 already set up

Create a script called conn and make it executable

conn
#!/bin/bash
if [ $1 = 'srv1' ]; then
	nmcli con up id VPN1
	sshpass -p 'remotepassword' ssh remoteuser@192.168.2.20
	nmcli con down id VPN1
fi

Calling the script with ./conn srv1 will up the VPN interface, and connect to remote server via SSH, and down the interface when SSH exists.

You can move the script to /usr/bin directory to be able to call it from anywhere, and without using ./

For better (any) security, don't use password in a script, or better yet, use a ssh key pair. Tutorial here Using SSH keys to authenticate users, and here Setup SSH public/private keys and disabling password login

Copy the if/fi block for each remote server and/or vpn interface

  • linux/vpn_clients/auto_vpn_and_ssh_sctript.txt
  • Last modified: 2019/10/31 09:05
  • by 127.0.0.1