mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2025-09-09 14:20:39 +02:00
Compare commits
No commits in common. "670e41f7269b26c57f28a6ff9122d14bd8c2f321" and "0f87aff94dfa46e5c2ca75966464ca5e27d9f321" have entirely different histories.
670e41f726
...
0f87aff94d
48
README.md
48
README.md
@ -1,6 +1,9 @@
|
|||||||
|
Voici le contenu complet du fichier `README.md` sous format code :
|
||||||
|
|
||||||
|
```markdown
|
||||||
# SSH Manager (sshm)
|
# SSH Manager (sshm)
|
||||||
|
|
||||||
SSH Manager (sshm) is a bash script that simplifies and automates the management of SSH hosts through the SSH configuration file (`~/.ssh/config`). It provides functionalities to list, connect, view, add, edit, and delete SSH host configurations, check the availability of hosts using pings, and manage different SSH configuration contexts.
|
SSH Manager (sshm) is a bash script that simplifies and automates the management of SSH hosts through the SSH configuration file (`~/.ssh/config`). It provides functionalities to list, connect, view, add, edit, and delete SSH host configurations, as well as to check the availability of hosts using pings.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@ -11,7 +14,6 @@ SSH Manager (sshm) is a bash script that simplifies and automates the management
|
|||||||
- Edit an existing SSH host configuration.
|
- Edit an existing SSH host configuration.
|
||||||
- Delete an SSH host configuration.
|
- Delete an SSH host configuration.
|
||||||
- Check the availability of an SSH host using ping.
|
- Check the availability of an SSH host using ping.
|
||||||
- Manage multiple SSH configuration contexts.
|
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
@ -90,40 +92,6 @@ sshm delete <name>
|
|||||||
sshm ping <name>
|
sshm ping <name>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Manage SSH Contexts
|
|
||||||
|
|
||||||
#### List Available Contexts
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sshm context list
|
|
||||||
```
|
|
||||||
|
|
||||||
This will list all available SSH configuration contexts and highlight the currently active one.
|
|
||||||
|
|
||||||
#### Switch to a Different Context
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sshm context use <context_name>
|
|
||||||
```
|
|
||||||
|
|
||||||
Switches the active SSH configuration to the specified context.
|
|
||||||
|
|
||||||
#### Create a New Context
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sshm context create <context_name>
|
|
||||||
```
|
|
||||||
|
|
||||||
Creates a new SSH configuration context.
|
|
||||||
|
|
||||||
#### Delete a Context
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sshm context delete <context_name>
|
|
||||||
```
|
|
||||||
|
|
||||||
Deletes the specified SSH configuration context.
|
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
### Adding a New SSH Host
|
### Adding a New SSH Host
|
||||||
@ -159,14 +127,6 @@ sshm view myhost
|
|||||||
sshm ping myhost
|
sshm ping myhost
|
||||||
```
|
```
|
||||||
|
|
||||||
### Switching to a Different SSH Context
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sshm context use myconfig
|
|
||||||
```
|
|
||||||
|
|
||||||
Switches to the `myconfig` SSH configuration context.
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details.
|
This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details.
|
||||||
|
160
sshm.bash
160
sshm.bash
@ -9,34 +9,24 @@
|
|||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law ou agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License est distributed on an "AS IS" BASIS,
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
# WITHOUT WARRANTIES OU CONDITIONS OF ANY KIND, either express ou implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
set -eo pipefail; [[ $TRACE ]] && set -x
|
set -eo pipefail; [[ $TRACE ]] && set -x
|
||||||
|
|
||||||
readonly VERSION="2.0.0"
|
readonly VERSION="1.0.0"
|
||||||
readonly CONFIG_DIR="${HOME}/.config/sshm"
|
CONFIG_FILE=${SSHM_CONFIG:-~/.ssh/config}
|
||||||
readonly DEFAULT_CONFIG="${HOME}/.ssh/config"
|
|
||||||
readonly CURRENT_CONTEXT_FILE="${CONFIG_DIR}/.current_context"
|
|
||||||
|
|
||||||
mkdir -p "$CONFIG_DIR"
|
|
||||||
|
|
||||||
if [[ -f "$CURRENT_CONTEXT_FILE" ]]; then
|
|
||||||
CONFIG_FILE=$(cat "$CURRENT_CONTEXT_FILE")
|
|
||||||
else
|
|
||||||
CONFIG_FILE="$DEFAULT_CONFIG"
|
|
||||||
fi
|
|
||||||
|
|
||||||
ssh_manager_version() {
|
ssh_manager_version() {
|
||||||
echo "ssh_manager $VERSION"
|
echo "ssh_manager $VERSION"
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_manager_help() {
|
ssh_manager_help() {
|
||||||
echo "Usage: ssh_manager command <command-specific-options>"
|
echo "Usage: ssh_manager command [--config <file>|-c <file>] <command-specific-options>"
|
||||||
echo
|
echo
|
||||||
echo "Commands:"
|
echo "Commands:"
|
||||||
cat<<EOF | column -t -s $'\t'
|
cat<<EOF | column -t -s $'\t'
|
||||||
@ -46,17 +36,23 @@ ssh_manager_help() {
|
|||||||
view <name> Check configuration of host
|
view <name> Check configuration of host
|
||||||
delete <name> Delete an SSH host from the configuration
|
delete <name> Delete an SSH host from the configuration
|
||||||
add Add an SSH host to the configuration
|
add Add an SSH host to the configuration
|
||||||
context list List available contexts
|
|
||||||
context use <name> Use a specific context
|
|
||||||
context create <name> Create a new context
|
|
||||||
context delete <name> Delete an existing context
|
|
||||||
help Displays help
|
help Displays help
|
||||||
version Displays the current version
|
version Displays the current version
|
||||||
|
EOF
|
||||||
|
echo
|
||||||
|
echo "Flags:"
|
||||||
|
cat<<EOF | column -t -s $'\t'
|
||||||
|
-c, --config Select a specific ssh config file
|
||||||
|
EOF
|
||||||
|
echo
|
||||||
|
echo "Environment Variables:"
|
||||||
|
cat<<EOF | column -t -s $'\t'
|
||||||
|
SSHM_CONFIG Specify the path of an ssh config file"
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_manager_list() {
|
ssh_manager_list() {
|
||||||
local config_file="$CONFIG_FILE"
|
local config_file="$1"
|
||||||
echo -e "\nList of SSH hosts:"
|
echo -e "\nList of SSH hosts:"
|
||||||
grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl
|
grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl
|
||||||
|
|
||||||
@ -77,18 +73,17 @@ ssh_manager_connect() {
|
|||||||
echo "Error: please provide a host number or name." 1>&2
|
echo "Error: please provide a host number or name." 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$host" =~ ^[0-9]+$ ]]; then
|
if [[ "$host" =~ ^[0-9]+$ ]]; then
|
||||||
local host_name
|
local host_name
|
||||||
host_name=$(grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sed -n "${host}p")
|
host_name=$(grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sed -n "${host}p")
|
||||||
if [ -n "$host_name" ]]; then
|
if [ -n "$host_name" ]; then
|
||||||
ssh -F "$config_file" "$host_name"
|
ssh "$host_name"
|
||||||
else
|
else
|
||||||
echo "Invalid number." 1>&2
|
echo "Invalid number." 1>&2
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
ssh -F "$config_file" "$host"
|
ssh "$host"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,91 +245,23 @@ ssh_manager_edit() {
|
|||||||
echo "Configuration for host $host updated successfully."
|
echo "Configuration for host $host updated successfully."
|
||||||
}
|
}
|
||||||
|
|
||||||
context_list() {
|
|
||||||
local current_context
|
|
||||||
current_context=$(cat "$CURRENT_CONTEXT_FILE" 2>/dev/null || echo "$DEFAULT_CONFIG")
|
|
||||||
|
|
||||||
echo "Available contexts:"
|
|
||||||
if [[ "$current_context" == "$DEFAULT_CONFIG" ]]; then
|
|
||||||
echo "* default"
|
|
||||||
else
|
|
||||||
echo " default"
|
|
||||||
fi
|
|
||||||
|
|
||||||
for context in "$CONFIG_DIR"/*; do
|
|
||||||
if [[ -f "$context" ]]; then # Vérifie que c'est bien un fichier existant
|
|
||||||
local context_name
|
|
||||||
context_name=$(basename "$context")
|
|
||||||
if [[ "$CONFIG_DIR/$context_name" == "$current_context" ]]; then
|
|
||||||
echo "* $context_name"
|
|
||||||
else
|
|
||||||
echo " $context_name"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
context_use() {
|
|
||||||
local context="$1"
|
|
||||||
if [[ -z "$context" ]]; then
|
|
||||||
echo "Error: please provide a context name." 1>&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$context" == "default" ]]; then
|
|
||||||
echo "$DEFAULT_CONFIG" > "$CURRENT_CONTEXT_FILE"
|
|
||||||
echo "Switched to default context."
|
|
||||||
elif [[ ! -f "$CONFIG_DIR/$context" ]]; then
|
|
||||||
echo "Error: context '$context' does not exist." 1>&2
|
|
||||||
exit 1
|
|
||||||
else
|
|
||||||
echo "$CONFIG_DIR/$context" > "$CURRENT_CONTEXT_FILE"
|
|
||||||
echo "Switched to context '$context'."
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
context_create() {
|
|
||||||
local context="$1"
|
|
||||||
if [[ -z "$context" ]]; then
|
|
||||||
echo "Error: please provide a context name." 1>&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -f "$CONFIG_DIR/$context" ]]; then
|
|
||||||
echo "Error: context '$context' already exists." 1>&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
touch "$CONFIG_DIR/$context"
|
|
||||||
echo "Context '$context' created."
|
|
||||||
}
|
|
||||||
|
|
||||||
context_delete() {
|
|
||||||
local context="$1"
|
|
||||||
if [[ -z "$context" ]]; then
|
|
||||||
echo "Error: please provide a context name." 1>&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ ! -f "$CONFIG_DIR/$context" ]]; then
|
|
||||||
echo "Error: context '$context' does not exist." 1>&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
rm -f "$CONFIG_DIR/$context"
|
|
||||||
echo "Context '$context' deleted."
|
|
||||||
|
|
||||||
if [[ "$(cat "$CURRENT_CONTEXT_FILE")" == "$CONFIG_DIR/$context" ]]; then
|
|
||||||
echo "$DEFAULT_CONFIG" > "$CURRENT_CONTEXT_FILE"
|
|
||||||
echo "Switched to default context."
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
ssh_manager_main() {
|
ssh_manager_main() {
|
||||||
local config_file="$CONFIG_FILE"
|
local config_file="$CONFIG_FILE"
|
||||||
local command="$1"
|
local command="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
|
while [[ "$#" -gt 0 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
--config|-c)
|
||||||
|
config_file="$2"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
if [[ -z $command ]]; then
|
if [[ -z $command ]]; then
|
||||||
ssh_manager_version
|
ssh_manager_version
|
||||||
echo
|
echo
|
||||||
@ -364,29 +291,6 @@ ssh_manager_main() {
|
|||||||
"edit")
|
"edit")
|
||||||
ssh_manager_edit "$config_file" "$@"
|
ssh_manager_edit "$config_file" "$@"
|
||||||
;;
|
;;
|
||||||
"context")
|
|
||||||
local subcommand="$1"
|
|
||||||
shift
|
|
||||||
case "$subcommand" in
|
|
||||||
"list")
|
|
||||||
context_list "$@"
|
|
||||||
;;
|
|
||||||
"use")
|
|
||||||
context_use "$@"
|
|
||||||
;;
|
|
||||||
"create")
|
|
||||||
context_create "$@"
|
|
||||||
;;
|
|
||||||
"delete")
|
|
||||||
context_delete "$@"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Error: invalid context subcommand." 1>&2
|
|
||||||
ssh_manager_help
|
|
||||||
exit 3
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
;;
|
|
||||||
"version")
|
"version")
|
||||||
ssh_manager_version
|
ssh_manager_version
|
||||||
;;
|
;;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user