-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxssh
More file actions
executable file
·91 lines (74 loc) · 2.79 KB
/
Copy pathxssh
File metadata and controls
executable file
·91 lines (74 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash
#
# ############################################################################
# Project: scripts (none)
# File...: xssh
# Created: Thursday, 2025/01/23 - 18:02:34
# Author.: fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Thursday, 2026/08/20 - 16:59:13
# Modified By..: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 1.0.4.173
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
source "$xSHELL_INIT"
# xs_init
xs_use args colors
xs_require ssh ssh-keyscan ssh-keygen cut mbat
xarg --id -u,--user --var user+r --desc "login with <user>"
xarg --id -H,--host --var host+r --desc "<host> to connect "
xarg --id -k,--known --desc "show ssh known hosts"
xarg --id -r,--remove --var host+r --desc "remove <host> form known_hosts"
xarg --id -g,--generate --var mail+r,filename+r --desc "generate a new ssh-key"
xarg --id -s,--size --var size+r --desc "define password <size>"
xarg --footer " * script files stored on '$APP_HOME'"
xrun --xrequire-one --xversionrc --xapp-data "$@"
__known(){ mbat ~/.ssh/known_hosts; exit 0; }
__remove(){ ssh-keygen -R \"$host\" -f ~/.ssh/known_hosts; exit 0; }
__generate(){
# needed tools
xs_require cat tr fold head date
# no size? set default value
[ -z "$size" ] && size=45
# current date
now=$(date "+%A-%Y%m%d-%H%M%S")
# generate a random password
_PASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9*@$.!\#' | fold -w "$size" | head -n 1)
# set filename's
key_file_name="$APP_HOME/$filename---gen-$now-t-ed25519-ssh-key"
key_file_pass="$APP_HOME/$filename---gen-$now-t-ed25519-ssh-pass"
# save password backup file
echo -n "$_PASS" > "$key_file_pass"
# gen ssh key
ssh-keygen -t ed25519 -C "$mail" -N "$_PASS" -f "$key_file_name"
# show key to user
mbat "$key_file_name"
exit 0;
}
xcase k __known
xcase r __remove
xcase g __generate
# Extrai o usuário e o IP do parâmetro
USER_IP=$1
if [ -n "$user" ] && [ -n "$host" ]; then
ssh_cmd="ssh $user@$host"
IP=$host
else
IP=$(echo "$USER_IP" | cut -d'@' -f2)
ssh_cmd="ssh $USER_IP"
fi
# Verifica se o host está online e adiciona o IP ao known_hosts
if ssh-keyscan -H "$IP" >> ~/.ssh/known_hosts; then
# Conecta via SSH
$ssh_cmd
# Remove o IP do known_hosts após encerrar a conexão SSH
ssh-keygen -R "$IP"
else
echo -e "${BGRED} Error! Host $IP unreachable. ${RESET}" >&2
exit 2
fi