Added an SSH host adder script, which allows you to add hosts to your ssh config file. (#76)

Co-authored-by: NinoCosmic <nikola.dokoski@cosmicdevelopment.com>
Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com>
This commit is contained in:
Nikola Dokoski (Nino) 2020-03-25 12:09:56 +01:00 committed by GitHub
parent 587a083db6
commit 09b7c22c26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 0 deletions

View File

@ -72,6 +72,7 @@ So far, the following projects have been integrated to this repo:
|[IMDB TV Series Info Extractor](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/imdb_episode_ratings)|[Yash Raj Sarrof](https://github.com/yashYRS) |
|[PX to REM](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/PX-to-REM)|[Atthaphon Urairat](https://github.com/uatthaphon) |
|[Yoda-speak Translator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/speak_like_yoda)|[sonniki](https://github.com/sonniki) |
|[SSH Host adder](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/SSH_Host_Adder)|[NinoDoko](https://github.com/NinoDoko)|
|[Wikipedia-Search](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Wikipedia-Search)|[Nissaar](https://github.com/Nissaar) |
|[Instagram Video Downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/insta_video_downloader)|[Shobhit Bhosure](https://github.com/shobhit99) |
|[Medium Article Downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/medium_article_downloader)|[coolsonu39](https://github.com/coolsonu39)|

33
SSH_Host_Adder/README.md Normal file
View File

@ -0,0 +1,33 @@
# SSH Host adder
This is a fairly simple script which adds hosts to an ssh config file.
SSH allows you to add hosts to a config file, so you don't have to remember ip addresses or hostnames. So if you add:
```
HOST test
HostName 192.168.80.1
User root
Port 22
```
to `~/.ssh/config`, you can just do `ssh test` instead of writing the address / user / port.
But when you constantly get new servers to ssh to, it's helpful to have a script!
## Usage:
```
./ssh_adder my_host 192.168.80.1 [--user myuser] [--port 2200]
```
`--user` and `--port` are optional and default to `root` and `22` respectively.
If you aren't using the default ssh config path, there is an argument for that as well:
```
./ssh_adder my_host 192.168.80.1 --conf /path/to/config
```
`-conf` defaults to `~/.ssh/config`
SSH configs allow you to make more complex operations, like adding different keys and whatnot, which I don't support here mostly because I haven't had a need to yet. If I get to updating my script some time, I'll update it here too.

8
SSH_Host_Adder/ssh_adder Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/python
import sys
import ssh_adder
def main():
ssh_adder.main(sys.argv)

View File

@ -0,0 +1,36 @@
import argparse, os
ssh_template = """
HOST {name}
HostName {hostname}
User {user}
Port {port}
"""
def args_to_obj(args):
obj = ssh_template.format(**args)
return obj
def add_to_conf(conf, obj):
conf = os.path.expanduser(conf)
with open(conf, 'a') as f:
f.write(obj)
def main():
# create the top-level parser
parser = argparse.ArgumentParser(prog = "Adds ssh hosts to the ssh config file. Is kind of a simple script which doesn't support all the options. May update with more stuff. \nExample usage: ./ssh_adder myhost 192.168.80.1 --user someuser --port 2200 --conf /path/to/non-default/ssh/config")
# create the parser for the "a" command
parser.add_argument('name', help = "This is the name of the Host to add to the config. For instance, if you want to do `ssh somehost`, then name should be `somehost`")
parser.add_argument('hostname', help = "This is the hostname/ip address of the host. If `somehost`'s address is 192.168.80.1, then hostname=192.168.80.1")
parser.add_argument('--user', default = 'root', help="The user to connect with. Defaults to root")
parser.add_argument('--port', default = 22, type = int, help = "The port to connect to. Defaults to 22")
parser.add_argument('--conf', default = '~/.ssh/config', help = "The path to the ssh config file. Defaults to ~/.ssh/config, which is the default location. ")
args = parser.parse_args()
obj = args_to_obj(args.__dict__)
add_to_conf(args.conf, obj)
main()