Compare commits

..

21 Commits

Author SHA1 Message Date
58ea1d8d79 noop to test hooks 2020-01-02 10:36:32 -08:00
c660fbeec5 Merge branch 'dev' 2018-10-02 02:27:14 -07:00
e86f96d16d Exit more gracefully when the action command isn't "up" 2018-10-02 02:26:42 -07:00
c7ab0bcb5d Track down bug where default section didn't work without SSIDs
(also increase verbosity)
2018-10-02 02:21:40 -07:00
19f3be3c91 Merge branch 'dev' 2018-05-27 04:35:36 -07:00
966f5d48ab Uhhh 2018-05-27 04:32:53 -07:00
e590ea5aaa Removed test file 2018-05-27 04:25:45 -07:00
6f4cff383f Just testing 2018-05-27 04:25:05 -07:00
47a725ed4d Merge branch 'dev' 2018-05-27 04:04:18 -07:00
720c96f494 Updates 2018-05-27 04:03:47 -07:00
e00775cd67 Updates 2018-05-27 04:03:47 -07:00
f7bbc52cfb Merge branch 'master' into dev 2018-05-27 03:56:03 -07:00
d26f7169ec Merge branch 'master' of https://github.com/mikeperalta1/connection-specific-ssh-config 2018-05-27 03:55:51 -07:00
ec7d5c5fcc Update README.md 2018-05-27 03:54:54 -07:00
0ff18ea0de Update README.md 2018-05-27 03:50:45 -07:00
ce5dbc6886 Update README.md 2018-05-27 03:49:48 -07:00
2487c4141a Update README.md 2018-05-27 03:45:02 -07:00
71125cd072 Update README.md 2018-05-27 03:28:41 -07:00
43dab05942 Add exec perms 2018-05-27 02:41:12 -07:00
0bcbf12212 Add files via upload 2018-05-27 02:37:12 -07:00
c1fcebb5e5 Initial commit 2018-05-27 02:36:31 -07:00
2 changed files with 51 additions and 10 deletions

View File

@ -40,11 +40,13 @@ You can also try using this script with a different daemon (or your own custom s
6. Create a short bash script inside the dispatcher directory that will launch connection-specific-ssh-config. For instance, you might name this file ```/etc/NetworkManager/dispatcher.d/99-launch-connection-specific-ssh-config```
7. Inside your launcher script, put the following contents:
```
#!/bin/bash
/path/to/connection-specific-ssh-config "$1" "$2" "/path/to/connection-specific-ssh-config.ini"
```
This will take the two important variables which NetworkManager has passed along to your launcher script, and pass them along to connection-specific-ssh-config
8. Repeat steps 2-7 for each additional user who would like connection based ssh configuration files.
@ -101,4 +103,6 @@ ssh_config_name = config-home
The above configuration file will instruct the script to use the ssh configuration file */home/mike/.ssh/config-work* while you're connected to one of your work connections *Work Connection - Main Office* or *Work Connection - Garys Office*, on either of your wireless adapters *wlan0* or *wlan1*. When you get home and connect to your *My Home Connection (Oh Joy)* connection on *wlan0*, the config file */home/mike/.ssh/config-home* will be used instead. Finally, the configuration file */home/mike/.ssh/config-default* will be used when *wlan0* connects to some undefined network.
The script works by simply creating a symlink where your original ssh configuration file was (typically *~/.ssh/config*), pointing to the ssh configuration determined to be the one you want active. Note that this of course means the script will fail (for safety reasons), if your original ssh configuration file still exists as a normal file.

View File

@ -52,12 +52,22 @@ class SSHConfiger:
self.complain(s)
raise Exception(s)
def quit(self, s):
#
self.log("Quitting because: " + s)
sys.exit(0)
#
def run(self):
#
self.log("Running for interface \""+ self._action_interface +"\": " + self._action_command)
# Only run if an interface is coming up
if ( self._action_command != "up" ):
self.quit("We don't need to run for action command: " + str(self._action_command))
# Parse the config
self.parse_config()
@ -65,9 +75,13 @@ class SSHConfiger:
if ( "ssh_dir" not in self._config.keys() ):
self.die("Config file needs key \"ssh_dir\" inside \"config\" section")
ssh_dir = self._config["ssh_dir"]
self.log("SSH Dir: " + str(ssh_dir))
# Determine which ssh config file we should use
ssh_config_name = self.determine_ssh_config_name()
self.log("Determined ssh config name: " + str(ssh_config_name))
if not ssh_config_name:
self.die("Unable to determine appropriate ssh config name; Quitting")
# Make paths
ssh_config_path_link = os.path.join(ssh_dir, "config")
@ -101,9 +115,8 @@ class SSHConfiger:
#
def determine_ssh_config_name(self):
# Only run if an interface is coming up
if ( self._action_command != "up" ):
return None
#
self.log("Attempting to determine SSH config name")
# Check each section
found_ssh_config_name = None
@ -111,23 +124,45 @@ class SSHConfiger:
#
section = self._targets[section_name]
self.log("Examining section: " + str(section_name))
# Must match at least one interface
if ( self._action_interface not in section["adapters"] ):
# Don't examine default if anything is picked already
if section_name == "default" and found_ssh_config_name:
self.log("Skipping default section because we've already found at least one match: " + str(found_ssh_config_name))
continue
#
interface_ssid = self.get_interface_ssid(self._action_interface)
# Check the interface
interface_matched = False
if (
# Matches, if current interface found in adapters
self._action_interface in section["adapters"]
# Can also match if we're in section "default"
or section_name == "default"
):
interface_matched = True
if not interface_matched:
self.log("Section \"" + str(section_name) + "\" didn't match any interfaces; Skipping")
continue
# Must also match at least one SSID
if ( interface_ssid not in section["ssids"] ):
# Grab the SSID this adapter is currently connected to
interface_ssid = self.get_interface_ssid(self._action_interface)
if not interface_ssid:
self.log("Interface \"" + str(interface_ssid) + "\" isn't connected to anything ... ")
self.log("Interface \"" + str(self._action_interface) + "\" is currently connected to: \"" + str(interface_ssid) + "\"")
# Must also match at least one SSID,
# OR we're in the default section
if interface_ssid not in section["ssids"] and section_name != "default":
self.log("Did not find SSID \"" + interface_ssid + "\" in section ssids: " + str(section["ssids"]))
continue
# Found a match!
found_ssh_config_name = section["ssh_config_name"]
self.log("Found matching ssh config name: " + str(found_ssh_config_name))
# Didn't find anything? Go default ...
if (found_ssh_config_name == None):
if (not found_ssh_config_name):
if ( "default" in self._targets.keys() ):
if ( "ssh_config_name" in self._targets["default"].keys() ):
found_ssh_config_name = self._targets["default"]["ssh_config_name"]
@ -172,6 +207,7 @@ class SSHConfiger:
continue
#
self.log("Parsing config target: \"" + section + "\"")
target = {
"adapters" : [],
"ssids" : []
@ -197,6 +233,7 @@ class SSHConfiger:
#
targets[section] = target
self.log("Parsed config for config target \"" + section + "\": " + str(target))
#
self._config = config