0

I am using the Python Flask library and trying to connect to a host. The issue is, if i store the hostname in a variable and pass as argument it fails with error.

How do i pass a variable?

uname = request.args.get('username')
pwd = request.args.get('password')
client = request.args.get('client')

print("username entered = ", uname)
print("password entered = ", pwd)
print("FAT client = ", client)

print("Initiating connection to ", client, " and triggering SPU")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(client, username=uname, password=pwd, timeout=900) # This errors as client is a variable. If i hardcode client to a real hostname it works
paramiko.util.log_to_file("filename.log")

Error in brief:

TypeError: getaddrinfo() argument 1 must be string or None

Error stacktrace is:

Traceback (most recent call last):
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\dev\HelloWorld\Flask_POST_PUT_test.py", line 24, in login_page
    client.connect(client, username=uname, password=pwd, timeout=900)
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\paramiko\client.py", line 340, in connect
    to_try = list(self._families_and_addresses(hostname, port))
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\site-packages\paramiko\client.py", line 203, in _families_and_addresses
    addrinfos = socket.getaddrinfo(
  File "C:\Users\rsanpui.ORADEV\AppData\Local\Programs\Python\Python38\Lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
TypeError: getaddrinfo() argument 1 must be string or None
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
  • I see a related answer: https://stackoverflow.com/questions/58683322/getaddrinfo-failed-when-connecting-to-sftp-server-using-paramiko from @Martin Prikryl but even if i use `client.connect(hostname=client` it doesnt work. – RajSanpui Jan 28 '20 at 10:36
  • @Martin Prikryl Any clue? – RajSanpui Jan 28 '20 at 10:40

1 Answers1

1

The first argument of paramiko.client.SSHClient.connect should be hostname, which is a string. You are passing paramiko.SSHClient itself instead. You got the hostname properly with client = request.args.get('client'), but then overwrote it with client = paramiko.SSHClient(). Use different variable names for your hostname and sshclient as the following.

uname = request.args.get('username')
pwd = request.args.get('password')
client = request.args.get('client')

print("username entered = ", uname)
print("password entered = ", pwd)
print("FAT client = ", client)

print("Initiating connection to ", client, " and triggering SPU")
sshclient = paramiko.SSHClient()
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshclient.connect(client, username=uname, password=pwd, timeout=900)
paramiko.util.log_to_file("filename.log")
Hurried-Helpful
  • 1,850
  • 5
  • 15