-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauto-ssh.py
executable file
·119 lines (95 loc) · 3.8 KB
/
auto-ssh.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
import os
import subprocess
filename = ".ssh/config"
if not os.path.exists(".ssh"):
os.makedirs(".ssh")
print("ssh folder created\n")
if os.path.exists(filename):
print("""
**Config File exists**\n
**Now we editing the same Config file**\n
""")
config = open(filename, 'a')
else:
print("**Creating The ssh config File**\n")
config = open(filename, 'w')
if input("Do you want to create ssh key [y/N]. You can skip with Enter: \n") == 'y':
os.system("ssh-keygen -t rsa")
print("\n")
if input("Do you want to show the current IP addresses with (ifconfig) command: [y/N] " ) == 'y':
os.system("ifconfig | grep ' inet '")
print("\n")
if input("Do you want to show the current IP addresses with (ip addr) command: [y/N]") == 'y':
os.system("ip addr | grep ' inet '")
print("\n")
print("Proceeding to Nmap scan")
print("\n")
scan_nmap = input("Enter your IP to scan your network for open ssh ports: ")
subnet_mask = int(input("Enter the subnet mask: "))
scan_option = input("Enter scan option - Recommended (-v): ")
os.system("nmap " + scan_option + " " + str(scan_nmap) + "/" + str(subnet_mask) + "| grep " + " 'ssh'")
print("\n")
scan_ip_address = input("Enter IP address to scan your network for IP Addresses: ")
subnet_mask2 = int(input("Enter the subnet mask: "))
scan_option2 = input("Enter scan option - Recommended (-v): ")
process = subprocess.Popen(
"nmap " + scan_option2 + " " + str(scan_ip_address) + "/" + str(subnet_mask2) + " | grep 'port 22'",
shell=True,
stdout=subprocess.PIPE,
)
stdout_list = process.communicate()[0].decode('utf-8')
command = stdout_list.split('\n')
def ask_for_input(text):
user_input = None
while not user_input:
user_input = input(text)
return user_input
def ask_for_input_root(text):
user_input_root = None
while not user_input_root:
user_input_root = input(text)
return user_input_root
template = """
Host {machine_name}
Hostname {selected_ip}
Port {port}
User {user}
"""
try:
for ip_string in command:
if ip_string:
ip = ip_string.split()[-1]
if input("Discovered IP: " + "(" + ip + ")" + "\n" + "Do you want to select this IP Address [y/N]:") == 'y':
selected_ip = ip
machine_name = ask_for_input("Please Enter Machine Name: ")
root_user = ask_for_input_root("This for the root user:")
port = input("Please Enter The Port Number. Skip with Enter: ") or '22'
user = ask_for_input("Please Enter The User: ")
ssh_to_machine = input("Copy the ssh id to the machine. Do you want to continue (y) ").lower()
ssh_to_root = input("Copy the ssh id to root. Do you want to continue (y):").lower()
normal_config = template.format(
machine_name=machine_name,
root_user=root_user,
port=port,
user=user,
selected_ip=selected_ip,
)
config.write(normal_config)
if ssh_to_root == 'y':
root_config = template.format(
machine_name=root_user,
root_user=root_user,
port=port,
user='root',
selected_ip=selected_ip,
)
config.write(root_config)
if ssh_to_machine:
os.system("ssh-copy-id " + user + "@" + selected_ip)
if ssh_to_root:
os.system("ssh -t " + user + "@" + selected_ip + " " + " " " 'sudo cp --parents .ssh/authorized_keys /root/' ")
print("Finished and starting with the new machine\n\n")
except KeyboardInterrupt:
config.close()
print("Information saved")