-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxssh
More file actions
executable file
·102 lines (82 loc) · 3.23 KB
/
Copy pathxssh
File metadata and controls
executable file
·102 lines (82 loc) · 3.23 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
92
93
94
95
96
97
98
99
100
101
102
#!/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: Monday, 2026/09/14 - 21:48:07
# Modified By..: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 1.0.5.273
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
# D=3
# source "$xSHELL_INIT"
xs_init
xs_use args colors
xs_require ssh ssh-keyscan ssh-keygen cut mbat
# gparted dig ls mcedit
know_hosts="~/.ssh/known_hosts"
xs_arg --id -u,--user --desc "login with <user>" --var user+r
xs_arg --id -p,--password --desc "login with <password>" --var password+r
xs_arg --id -H,--host --desc "<host> to connect " --var host+r
xs_arg --id -g,--generate --desc "generate a new ssh-key pair" --var name+r,comment+r,password_size+o
xs_arg --id -k,--known --desc "show '$know_hosts'"
xs_arg --id -r,--remove --desc "remove <host> form '$know_hosts'" --var host+r
xs_arg --id -t,--temp --desc "temp session. Dont save host on '$know_hosts'"
xs_arg --header "ssh tools"
xs_arg --footer " * generated keys stored on '$APP_HOME'"
xs_run --xrequire-one --xversionrc --xapp-data "$@"
__known(){ D=0; mbat ~/.ssh/known_hosts; exit 0; }
__remove(){ ssh-keygen -R \"$host\" -f ~/.ssh/known_hosts; exit 0; }
__generate(){
local now generated_pass sshkey_name sshkey_pass
# needed tools
xs_require cat tr fold head date
# load function lcase (lower case)
xs_func --local lcase
# no password_size? set default value
[ -z "$password_size" ] && password_size=65
# current date
now=$(lcase $(date "+%A-%Y%m%d-%H%M%S"))
# generate a random password
generated_pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9*@$.!\#' | fold -w "$password_size" | head -n 1)
# set name's
sshkey_name="$APP_HOME/$name---gen-$now-t-ed25519-ssh-key"
sshkey_pass="$sshkey_name-pass"
# save password backup file
echo -n "$generated_pass" > "$sshkey_pass"
# generate ssh key with provided data
ssh-keygen -t ed25519 -C "$comment" -N "$generated_pass" -f "$sshkey_name"
# show key to user
mbat "$sshkey_name"
exit 0;
}
xs_case k __known
xs_case r __remove
xs_case 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
xs_case t "ssh-keygen -R '$IP'"
else
echo -e "${BGRED} Error! Host $IP unreachable. ${RESET}" >&2
exit 2
fi