Python3 将变量传递给 paramiko
Python3 pass variable to paramiko
所以我正在获取 1.1.1.0 到 1.1.1.10 范围内的所有 ip,并使用每个 ip 单独连接到 ssh。当我 运行 "print(host1)" 它给了我 ip 但是当我在 ssh.connect 中使用变量 host1 时我得到错误 "getaddrinfo() argument 1 must be string or None ”,如果我将变量 host1 放入引号中,我会收到错误消息“只能将 str(不是“IPv4Address”)连接到 str”
start_ip = ipaddress.IPv4Address('1.1.1.0')
end_ip = ipaddress.IPv4Address('1.1.1.10')
for ip_int in range(int(start_ip), int(end_ip)):
host1 = ipaddress.IPv4Address(ip_int)
print(ipaddress.IPv4Address(ip_int))
print(host1)
def ssh_connect(password, code=0):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host1, port=22, username=username, password=password, timeout=5)
Python 取决于制表符。所以当你编写你的控制结构时,你必须小心。缩进告诉 Python 哪些语句应该包含在你的循环中。我的建议是删除函数定义,直到你让这段代码工作,然后如果你想在你更了解 Python 之后概括功能,最好有一段代码比一段代码有效你不太明白。但行首的间距用于指示控制结构的范围。在我开始使用 Python 编码之前,我从未见过这一点,这对我来说是最难掌握的。看来您也可能遇到了问题。
如果您查找副标题 'Using IP Addresses with other modules',您会发现您必须使用 str(...)
将 IPv4Address
返回的主机名类型转换为字符串
所以我正在获取 1.1.1.0 到 1.1.1.10 范围内的所有 ip,并使用每个 ip 单独连接到 ssh。当我 运行 "print(host1)" 它给了我 ip 但是当我在 ssh.connect 中使用变量 host1 时我得到错误 "getaddrinfo() argument 1 must be string or None ”,如果我将变量 host1 放入引号中,我会收到错误消息“只能将 str(不是“IPv4Address”)连接到 str”
start_ip = ipaddress.IPv4Address('1.1.1.0')
end_ip = ipaddress.IPv4Address('1.1.1.10')
for ip_int in range(int(start_ip), int(end_ip)):
host1 = ipaddress.IPv4Address(ip_int)
print(ipaddress.IPv4Address(ip_int))
print(host1)
def ssh_connect(password, code=0):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host1, port=22, username=username, password=password, timeout=5)
Python 取决于制表符。所以当你编写你的控制结构时,你必须小心。缩进告诉 Python 哪些语句应该包含在你的循环中。我的建议是删除函数定义,直到你让这段代码工作,然后如果你想在你更了解 Python 之后概括功能,最好有一段代码比一段代码有效你不太明白。但行首的间距用于指示控制结构的范围。在我开始使用 Python 编码之前,我从未见过这一点,这对我来说是最难掌握的。看来您也可能遇到了问题。
如果您查找副标题 'Using IP Addresses with other modules',您会发现您必须使用 str(...)
IPv4Address
返回的主机名类型转换为字符串