Skip to content

Commit

Permalink
Merge pull request #25 from GovTechSG/v1.4.0
Browse files Browse the repository at this point in the history
V1.4.0
  • Loading branch information
Salihan04 authored Jul 31, 2019
2 parents 74fe49d + bb73249 commit 32a4a3d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# slist
![](https://img.shields.io/github/release/GovTechSG/slist.svg?style=flat)
![version](https://img.shields.io/github/release/GovTechSG/slist.svg?style=flat)

slist is a tool to list your servers in ssh config and ssh into it.<br/>
This only works on Unix machines.<br/>
slist aims to solve the problem of users having to remember aliases or IP addresses of all their servers.<br/>
slist reads the aliases in the ~/.ssh/config file and list them in the terminal.

## Setting it up
```

```bash
cd <path_of_choice>
git clone https://github.com/GovTechSG/slist.git
chmod +x slist.sh
Expand All @@ -17,26 +18,31 @@ slist
```

## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

## Screenshots

![Optional Text](../master/screenshots/slist.png)
![Optional Text](../master/screenshots/filter.png)

## Usage

Usage: slist [-fhl]
[--add-host host_name --ip-adr ip_address [--ssh-user user --port port_number --keypath keyname_with_path]]
[--del-host host_name]
```

```bash
-f <keyword> Keyword to filter
-h Display help
-l List servers with ip addresses
-l -f <keyword> Filter list work <keyword>
-e Open and edit ~/.ssh/config
--add-host <host_name> Add a new host to the SSH config file. Must be used together with --ip-adr option
--ip-adr <ip_address> Add a new IP address to the SSH config file. Must be used together with --add-host option
--ssh-user <user> Add a new SSH user to SSH config file. Must be used together with --add-host and --ip-adr options
--port <port_number> Add a new port number to SSH config file. Must be used together with --add-host and --ip-adr options
--keypath <keyname_with_path> Add a new key file to SSH config file. Must be used together with --add-host and --ip-adr options
--del-host <host_name> Delete a host from the SSH config file
--config-file To use other config file
```
44 changes: 31 additions & 13 deletions slist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Usage: slist [-hl]|[-f <keyword>]
-h Display help
-l List servers with ip addresses
-l -f <keyword> Filter list work <keyword>
-e Open ~/.ssh/config
Using other conf instead of default ~/.ssh/conf
Usage: slist --config-file /tmp/config
Adding new host to ssh config
Usage: slist --add-host <host name> --ip-adr <ip address> --ssh-user <user> --port <port number> --keypath < keyname with path >
Expand Down Expand Up @@ -202,7 +206,7 @@ check_config_file_exists(){
# Function to check if last line of conf is empty
check_last_line(){
last_line=$(tail -1 ${config_file})
if [ ! -z "$last_line" ]; then
if [ -n "$last_line" ]; then
echo "" >> "$config_file"
fi
}
Expand All @@ -221,15 +225,6 @@ check_host_exists(){
done
}

# Start of slist
check_config_file_exists

# Loading main slist page if no argument found
if [ $# -eq 0 ]; then
clear
main
fi

# Function to check if argument is nil
check_arg(){
val="$1"
Expand All @@ -238,6 +233,8 @@ check_arg(){
fi
}

# Start of slist
check_config_file_exists

list=false
filter=false
Expand All @@ -251,16 +248,26 @@ ssh_user=false
port=false
key_path=false

while getopts ':hlf:-:' c
while getopts ':hlef:-:' c
do
case $c in
-)
case "$OPTARG" in
config-file)
configfile=true
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
# Exit program if host is empty
if [[ $value == "false" ]]; then
printf "%s\n" "${red}Host cannot be empty ${end}"
exit 1
fi
config_file=$val
;;
add-host)
add_host=true
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
check_arg "$val"
# Exist program if host is empty
# Exit program if host is empty
if [[ $value == "false" ]]; then
printf "%s\n" "${red}Host cannot be empty ${end}"
exit 1
Expand Down Expand Up @@ -326,16 +333,27 @@ do
filter=true
keyword=$OPTARG
;;
e)
edit=true
;;
*)
echo "Invalid parameter!"
echo ""
help
;;
esac
done


# Loading main slist page if no argument found
if [ $# -eq 0 ] || [[ $configfile == "true" && $3 == "" ]]; then
clear
main
fi

if [[ $help == "true" ]]; then
help
elif [[ $edit == "true" ]]; then
vi ~/.ssh/config
elif [[ $add_host == "true" ]] && [[ $del_host == "true" ]]; then
printf "%s\n" "${red}--add-host and --del-host cannot be use at the same time ${end}"
exit 3
Expand Down

0 comments on commit 32a4a3d

Please sign in to comment.