1
+ import time
2
+ import requests
3
+ import json
4
+ import os
5
+
6
+ # Set your API key and Pod ID
7
+ API_KEY = "1WD030TPD0WW71AKO7SMAB0Q2WIQSYWWUK9T2JVI"
8
+ POD_ID = "bs8jyvfzklc0tk"
9
+
10
+ URL = f"https://api.runpod.io/graphql?api_key={ API_KEY } "
11
+ print (URL )
12
+ HEADERS = {
13
+ 'Content-Type' : 'application/json' ,
14
+ }
15
+
16
+ # GraphQL query to get all running pods with their IP address and port
17
+ def get_all_running_pods ():
18
+ query = '''
19
+ query Pods { myself { pods { id name runtime { uptimeInSeconds ports { ip isIpPublic privatePort publicPort type } gpus { id gpuUtilPercent memoryUtilPercent } container { cpuPercent memoryPercent } } } } }
20
+ '''
21
+
22
+ response = requests .post (
23
+ URL ,
24
+ json = {'query' : query },
25
+ headers = HEADERS
26
+ )
27
+ print (response .json ())
28
+ if response .status_code == 200 :
29
+ pods = response .json ()['data' ]['myself' ]['pods' ]
30
+ for pod in pods :
31
+ if 'manav' not in pod ['name' ]:
32
+ continue
33
+ ip = None
34
+ port = None
35
+ try :
36
+ ports = pod ['runtime' ]['ports' ]
37
+ for p in ports :
38
+ if p ['isIpPublic' ]:
39
+ ip = p ['ip' ]
40
+ port = p ['publicPort' ]
41
+ break
42
+ yield (pod ['id' ], (ip , port ))
43
+ except TypeError :
44
+ pass
45
+ else :
46
+ print (f"Failed to get running pods: { response .status_code } " )
47
+
48
+
49
+ # Fetch and print all running pods with their IP address and port
50
+ pods = dict (get_all_running_pods ())
51
+
52
+ config = """Host %s
53
+ HostName %s
54
+ Port %s
55
+ User root
56
+ IdentitiesOnly yes
57
+ IdentityFile /Users/manav/.ssh/run_pod
58
+ ForwardX11 yes
59
+ """
60
+
61
+ configs = []
62
+ i = 1
63
+ for pod , (ip , port ) in pods .items ():
64
+ if ip is not None :
65
+ print (f"{ pod } - pod{ i } - { ip } :{ port } " )
66
+ configs .append (config % (f"pod{ i } " , ip , port ))
67
+ i += 1
68
+
69
+ with open ('/Users/manav/.ssh/config' , 'w' ) as f :
70
+ f .write ('\n ' .join (configs ))
71
+
0 commit comments