I want to ssh to another node on my network as part of a larger python script, I am using pexpect which works when I do something like this:
session=spawn('ssh root#172.16.210.254')
I want to replace the address with a variable so I can cycle through addresses in a list however when I try:
address = "172.16.210.253"
session=spawn('ssh root#'address)
It doesn't work as using address in this way is invalid syntax. What is the correct syntax for this?
session=spawn('ssh root#' + address) to concatenate the strings
Related
I'm trying to get the Kubernetes service IP using env variable to an endpoint inside a python app.
For example in NodeJs:
(`http://${process.env.AUTH_ADDRESS}/hashed-password/` + password);
using backticks and ${} IP address is fetched from the env variable
In Python I tried
opc.tcp://"+str(os.environ.get('USER_SERVICE_SERVICE_HOST'))+":4840/
but the result is actually something like ('10.98.191.127', 4840). I need it in a format like '10.98.191.127:4840' I have tried concatenate strings individually
Try to convert the port 4840 to str type. Something like:
str(os.environ.get('USER_SERVICE_SERVICE_HOST'))+":"+str(4840)
Something I would do is to use f-string format, something like:
f"{os.environ.get('USER_SERVICE_SERVICE_HOST')}:4840"
I am trying to make a DNS Server and Client in python. Where Server will have stored data like:
qtsdatacenter.aws.com 128.64.3.2 A
ww.ibm.com 64.42.3.4 A
www.google.com 8.6.4.2 A
localhost - NS
Basically Hostname IPaddress Type.
What would be the best datastructure to implement that will make searching for queries and outputting referenced data easy.
For example: send a String saying www.google.com from client, the server searches in its stored data table for the string match for hostname, returns in format
www.google.com 8.6.4.2 A.
Keep it simple like this. And use dictionary. Looks like your keys are going to be hashable and dictionaries possess O(1) average complexity. See this example:
dct = {"www.google.com" : "www.google.com 8.6.4.2 A",
"www.ibm.com" : " ww.ibm.com 64.42.3.4 A"}
import netifaces as ni
ip = ni.ifaddresses("eth0")[ni.AF_INET]['addr']
error
ip = ni.ifaddresses("eth0")[ni.AF_INET]['addr']
ValueError: You must specify a valid interface name.
ip = ni.ifaddresses("en0")[ni.AF_INET]['addr']
error
ip = ni.ifaddresses("en0")[ni.AF_INET]['addr']
TypeError: list indices must be integers or slices, not str
Does anyone know why the mac is giving such errors?
The first error means that there is no interface named eth0. Indeed, this is a common interface name on Linux, but not on MacOS.
The second error means that you are trying to extract a field which doesn't exist. There is information about en0 but it is an array, not a dict. This is like saying "hello"["addr"], there is no way to access the "addr":th element of a sequence. You apparently mean something like
ip = ni.ifaddresses("en0")[ni.AF_INET][0]['addr']
though there is no way out of context to tell whether getting only one address is actually what you want. The array you get represents a number of bindings; perhaps you want all of them?
addrs = ni.ifaddresses('en0')
ips = [x['addr'] for x in addrs[ni.AF_INET]]
The netifaces documentation actually explains this in quite some detail.
I know that I can use email.utils.parseaddr to parse out an email address properly, even a tricksy one:
>>> parseaddr('Bad Horse <bad.horse#example(no one expects the #-ish inquisition!).com')
('Bad Horse (no one expects the #-ish inquisition!)', 'bad.horse#example.com')
(So good, Python! So good!)
However, that's just a string:
>>> type(parseaddr('Bad Horse <bad.horse#example(no one expects the #-ish inquisition!).com')[-1])
<class 'str'>
In the typical case I can just do .rsplit('#', maxsplit=1)[-1] to get the domain. But what if I'm just sending local mail without a domain?
>>> parseaddr('Wayne <wayne>')[-1].rsplit('#', maxsplit=1)[-1]
'wayne'
That's not quite what I want - I'd prefer maybe None or 'localhost'.
Does anything like that come in Python's included batteries?
I haven't been able to find anything yet, so my current approach is to make a slight adjustment:
try:
domain = parseaddr('Wayne <wayne>')[-1].rsplit('#', maxsplit=1)[1]
except IndexError:
# There was no '#' in the email address
domain = None # or 'localhost'
In the absence of a better way, this works and gets me what I need.
Is there a function in Python that determines if a hostname is a domain name or an IP(v4) address?
Note, the domain name may look like: alex.foo.bar.com or even (I think this is valid): 1.2.3.com.
One of the visual differences between IP and domain address is when you delete dots in the IP address the result would be a number. So based on this difference we can check if the input string is an IP or a domain address. We remove dots in the input string and after that check if we can convert the result to an integer or we get an exception.
def is_ip(address):
return address.replace('.', '').isnumeric()
Although in some IP-Representations like dotted hexadecimal (e.g. 0xC0.0x00.0x02.0xEB) there can be both letters and numbers in the IP-Address. However the top-level-domain (.org, .com, ...) never includes numbers. Using the function below will work in even more cases than the function above.
def is_ip(address):
return not address.split('.')[-1].isalpha()
I'd use IPy to test if the string is an IP address, and if it isn't - assume it's a domain name. E.g.:
from IPy import IP
def isIP(str):
try:
IP(str)
except ValueError:
return False
return True