-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbo_online.py
201 lines (183 loc) · 7.68 KB
/
sbo_online.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python3.4
import psycopg2
import paramiko
import sys
from paramiko.client import SSHClient, SSH_PORT
from time import sleep
import sys
VPN_NAME = input("Enter a client VPN name: \n")
sip_peer=input("Enter SIP PEER of gateway \n")
gate_ip=input("Enter gateway IP adress \n")
client_name=input("Enter CLIENT NAME\n")
password=VPN_NAME.replace('-','')+'pwd'
client = SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
connection = psycopg2.connect(host='172.16.0.220', user='gofraud', password='password', database='main')
except psycopg2.OperationalError.diag as e:
print(e)
C = connection.cursor()
C.execute(
"select db_name, realm.name, realm.ip, db_server.host from instance join realm on instance.realm_id = realm.id join db_server on instance.db_server_id = db_server.id where instance.name = '{client}'".format(client=client_name))
record = C.fetchone()
db_name = record[0]
realm_name = record[1]
realm_ip = record[2]
db_server_ip = record[3]
print("Client data_base: ",db_name, "\n", "Server_name: ",realm_name, "\n", "Server_IP: ",realm_ip,"\n", "Database_IP: ", db_server_ip)
sleep(2)
C.close()
connection.close()
def change_db(db_name,sip_peer, VPN_NAME):
try:
connection = psycopg2.connect(host=db_server_ip, user='gofraud', password='kXLILCrAPp2yjEgH3xCp', database=db_name)
except psycopg2.OperationalError.diag as e:
print(e)
C = connection.cursor()
C.execute(
"update sip_conf set peer_name = '{peer_name}' where name like '{peer}%'".format(peer_name=VPN_NAME, peer=sip_peer))
connection.commit()
C.execute(
"update gate set protocol = 'IAX2' where simserver_password = (select secret from sip_conf where name like '{peer}%' limit 1)".format(peer=sip_peer))
connection.commit()
C.execute(
"update sip_conf set lastms = '50' where name like '{peer}%'".format(peer=sip_peer))
connection.commit()
C.execute(
"select sip_conf.name as sip_peer, sip_conf.peer_name as iax_peer, gate.protocol as protocol, sip_conf.lastms as lastms from sip_conf join gate on sip_conf.secret = gate.simserver_password where sip_conf.name like '{peer}%'order by sip_conf.name".format(peer=sip_peer))
check_result = C.fetchall()
colnames = [desc[0] for desc in C.description]
print(colnames,'\n')
for i in range(len(check_result)):
print(check_result[i])
C.close()
connection.close()
def send_sip(sip_peer, gate_ip):
try:
client.connect(VPN_NAME, username='root', port=SSH_PORT, key_filename='/root/.ssh/authorized_keys')
except paramiko.BadHostKeyException as e:
print(e)
except paramiko.AuthenticationException as e:
print(e)
except paramiko.SSHException as e:
print(e)
client.exec_command('echo -e '
'\'\n[{peer}]\n'
'type=friend\n'
'host={ip}\n'
'allow=g729\n'
'allow=g723\n'
'allow=alaw\n'
'allow=ulaw\n'
'qualify=yes\n'
'context=out\n'
'progressinband=never\' >> /etc/asterisk/sip.conf'.format(peer=sip_peer, ip=gate_ip))
client.exec_command('asterisk -rx \'sip reload\'')
client.close()
def send_iax(VPN_NAME, password, realm_ip):
try:
client.connect(VPN_NAME, username='root', port=SSH_PORT, key_filename='/root/.ssh/authorized_keys')
except paramiko.BadHostKeyException as e:
print(e)
except paramiko.AuthenticationException as e:
print(e)
except paramiko.SSHException as e:
print(e)
client.exec_command('cp /etc/asterisk/iax.conf /etc/asterisk/iax_backup.conf')
client.exec_command('rm /etc/asterisk/iax.conf')
client.exec_command('echo -e '
'\'\n[general]\n'
'bindport=4571\n'
'disallow=all\n'
'allow=g729\n'
'bandwidth=low\n'
'jitterbuffer=yes\n'
'forcejitterbuffer=no\n\' >> /etc/asterisk/iax.conf')
client.exec_command('echo -e '
'\'\nregister => {peer}:{sec}@{server_ip}\' >> /etc/asterisk/iax.conf'.format(peer=VPN_NAME, sec=password,server_ip=realm_ip))
client.exec_command('echo -e '
'\'\n[{peer}]\n'
'type=friend\n'
'secret={sec}\n'
'trunk=yes\n'
'username={peer}\n'
'host={server_ip}\n'
'disallow=all\n'
'allow=g729\n'
'allow=g723\n'
'allow=alaw\n'
'allow=ulaw\n'
'autokill=no\n'
'requirecalltoken=no\n'
'context=out\n'
'qualify=yes\' >> /etc/asterisk/iax.conf'.format(peer=VPN_NAME, sec=password, server_ip=realm_ip))
client.exec_command('asterisk -rx \'iax2 reload\'')
client.close()
try:
client.connect(realm_ip, username='root', port=SSH_PORT, key_filename='/root/.ssh/id_rsa')
except paramiko.BadHostKeyException as e:
print(e)
except paramiko.AuthenticationException as e:
print(e)
except paramiko.SSHException as e:
print(e)
client.exec_command('echo -e '
'\'\n[{peer}]\n'
'type=friend\n'
'secret={sec}\n'
'trunk=yes\n'
'username={peer}\n'
'host=dynamic\n'
'disallow=all\n'
'allow=g729\n'
'allow=g723\n'
'allow=alaw\n'
'allow=ulaw\n'
'autokill=no\n'
'requirecalltoken=no\n'
'context=out\n'
'directmedia=no\n'
'jitterbuffer=no\n'
'qualify=yes\' >> /etc/asterisk/iax.conf'.format(peer=VPN_NAME, sec=password))
client.exec_command('asterisk -rx \'iax2 reload\'')
client.close()
def send_dialplan(VPN_NAME, sip_peer):
try:
client.connect(VPN_NAME, username='root', port=SSH_PORT, key_filename='/root/.ssh/authorized_keys')
except paramiko.BadHostKeyException as e:
print(e)
except paramiko.AuthenticationException as e:
print(e)
except paramiko.SSHException as e:
print(e)
client.exec_command('echo -e '
'\'\n[out]\n'
'exten => _{sip}.,1,Dial(SIP/{sip}/${{EXTEN}})\n'
' same => 2, NoOp(DIALSTATUS=${{DIALSTATUS}})\n'
' same => 3, NoOp(HANGUPCAUSE=${{HANGUPCAUSE}})\n'
' same => 4, NoOP(DIALEDTIME=${{DIALEDTIME}})\n'
' same => 5, NoOP(ANSWEREDTIME=${{ANSWEREDTIME}})\n\n'
'exten => _RX.,1,Dial(IAX2/{peer_name}/${{EXTEN}})\n'
'exten => _RX.,n,Hangup()\n'
'exten => _GX.,n,Hangup()\' >> /etc/asterisk/extensions.conf'.format(sip=sip_peer, peer_name=VPN_NAME))
client.exec_command('asterisk -rx \'dialplan reload\'')
client.close()
def main():
change_db(db_name,sip_peer, VPN_NAME)
send_sip(sip_peer, gate_ip)
send_iax(VPN_NAME, password, realm_ip)
send_dialplan(VPN_NAME, sip_peer)
try:
choice = sys.argv[1]
except IndexError:
main()
try:
if choice.lower() == 'sip':
send_sip(sip_peer, gate_ip)
change_db(db_name,sip_peer, VPN_NAME)
elif choice.lower() == 'iax':
send_iax(VPN_NAME, password, realm_ip)
else:
main()
except:
pass