У меня есть функция: connect_dev()
, и я хочу вызвать ее из класса config_static()
, но появляется ошибка:
имя 'ssh_client' не определено
Итак, это мой файл view.py:
class config_static(View):
def post(self, request, *args, **kwargs):
formm = NacmForm(request.POST or None)
ipform = IpFormset(request.POST)
upform = UploadForm(request.POST,request.FILES)
userValue = formm['username'].value()
passValue = formm['password'].value()
destination = str(request.POST['destination'])
prefix = str(request.POST['prefix'])
gateway = str(request.POST['gateway'])
if ipform.is_valid() and formm.is_valid():
simpanForm = formm.save()
for form in ipform:
ipaddr = form.cleaned_data.get('ipaddr')
vendor = form.cleaned_data.get('vendor')
networks = str(destination+"/"+prefix)
netmask = IPNetwork(networks).netmask
collect_config = "<b>Configure on "+str(ipaddr)+" | vendor = "+str(vendor)+"</b></br>"
try:
connect_dev(self, ipaddr, userValue, passValue)
config_read = str(vendor.sett_static_routing)
array_read = config_read.split('\r')
output_line = ""
for line in array_read:
new_line = re.sub(r'\n','',line)
if new_line != '':
config_send = eval(new_line)
collect_config = collect_config + config_send+"</br>"
print(config_send+" ini config send")
try:
stdin, stdout, stderr = ssh_client.exec_command(config_send+"\n")
time.sleep(1)
results = stdout.read()
print (str(results))
except:
try:
remote_conn.send(config_send+"\n")
time.sleep(1)
results = remote_conn.recv(65535)
print (results.decode('ascii'))
# print (results)
except:
print("error paramiko")
messages.success(request, collect_config)
ssh_client.close()
simpanIp = form.save(commit=False)
simpanIp.connect_id = simpanForm
print (simpanIp)
simpanIp.save()
simpanForm.save()
except Exception as e:
error_conf(request, collect_config, "</br>Exception in connecting to the server")
print (e)
return HttpResponseRedirect('routing_static')
def get(self, request, *args, **kwargs):
formm = NacmForm()
ipform = IpFormset()
return render(request, 'config/routing_static.html', {'form': formm, 'logins': Connect.objects.all(), 'ipform': ipform, 'status': self.status})
и вот моя функция:
def connect_dev(self, ipaddr,userValue,passValue):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ipaddr,username=userValue,password=passValue,look_for_keys=False, allow_agent=False, timeout=5)
remote_conn=ssh_client.invoke_shell()
shell = remote_conn.recv(65535)
Когда я запускаю его, я получаю сообщение об ошибке:
имя 'ssh_client' не определено
Я пытался с добавленным параметром:
def connect_dev(self, ssh_client, ipaddr,userValue,passValue):
и в классе config_static (View):
connect_dev(self, ssh_client, ipaddr, userValue, passValue)
Результат все тот же.