-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote-host.py
More file actions
69 lines (54 loc) · 1.95 KB
/
remote-host.py
File metadata and controls
69 lines (54 loc) · 1.95 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
#!/usr/bin/python
import requests
IP_QUERY_API_S1 ="https://ifconfig.co/ip"
IP_QUERY_API_S2 = "https://api-ipv4.ip.sb/ip"
def get_public_ip_from_api(api):
query_ip_api_url = api.strip()
headers= {
'Cache-Control': "no-cache",
"User-Agent":"curl/7.55.1",
}
data =""
try:
response = requests.request("GET",query_ip_api_url,
headers=headers,timeout=10,5))
if response.ok:
data = response.text.strip()
else:
print(
"API {api} status code: {code}".format(api=api,
code=response.status_code))
except Exception as _:
del _
return data
def get_public_ip():
ip1, ip2 = map(get_public_ip_from_api,(
IP_QUERY_API_S1, IP_QUERY_API_S2 ))
ip = ip1 if ip1 != "" and ":" not in ip1
else ip2
if ip =="":
raise AssertionError(
"can not get IP from API's terminated")
return ip
def execute_commands_on_remote_host(host,
command,**kwargs):
import paramiko
port = kwargs.get("port") or 22
username = kwargs.get("username") or 'root'
key_filename = kwargs.get("key_filename")
timeout =kwargs.get("timeout") or 5
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
stdin, stdout, stderr = client.exec_command(command=command,
get_pty=True)
for line in stdout:
print "Stdout:", line,
client.close()
for line in stdout:
print "Stderr: ", line,client.close()
if __name__ =='_main_':
internet_ip = get_public_ip()
print("Current public ip address is {}",
format(internet_ip))
command_remove_old_rule = '''firewall-cmd --permanent --zone=[ublic'
]'''