如何遍历 IP 地址列表并通过 SSH 访问每个地址?
How can I loop through a list of IP addresses and SSH to each?
我是 python 的新手。我有一个包含 Raspberry Pi 个具有默认凭据的设备的列表(文本文件)。我想连接到每个,登录并更改密码。我可以使用以下代码对单个设备执行此操作:
import os
import socket
from ssh2.session import Session
host = '172.16.1.119'
user = 'pi'
passw = 'raspberry'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect ((host, 22))
session = Session()
session.handshake(sock)
session.userauth_password(user, passw)
channel = session.open_session()
channel.shell()
channel.write("echo raspberry | sudo -S su && echo 'pi:newpwhere' | sudo chpasswd\n")
channel.write("exit\n")
size, data = channel.read()
while size > 0:
print(data.decode())
size, data = channel.read()
print ("Exit status: {0}".format(channel.get_exit_status()))
当我尝试使用类似的代码读取具有多个 IP 地址的文件并执行相同的操作时,我收到错误消息:
import os
import socket
from ssh2.session import Session
user = 'pi'
passw = 'raspberry'
file = input("please select the file that contains the hosts: ")
with open(file) as f:
hosts = f.readlines()
def chgpw():
channel = session.open_session()
channel.shell()
channel.write("echo raspberry | sudo -S su && echo 'pi:newpwhere' | sudo chpasswd\n")
channel.write("exit\n")
for i in hosts:
print(i)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((i,22))
session = Session()
session.handshake(sock)
session.userauth_password(user, passw)
chgpw()
print ("Exit status: {0}".format(channel.get_exit_status()))
我收到的错误是:
Traceback (most recent call last):
File "stack-ov.py", line 21, in <module>
sock.connect((i,22))
socket.gaierror: [Errno -2] Name or service not known
打印语句 print(i) 确实在第 21 行发生错误之前打印出列表中的第一个 IP 地址。
readlines
在其 returns 所在的每一行中包含换行符。在使用它之前,您需要 rstrip
i
。
for i in hosts:
stripped = i.rstrip()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((stripped, 22))
换行符使每个地址无效。
我是 python 的新手。我有一个包含 Raspberry Pi 个具有默认凭据的设备的列表(文本文件)。我想连接到每个,登录并更改密码。我可以使用以下代码对单个设备执行此操作:
import os
import socket
from ssh2.session import Session
host = '172.16.1.119'
user = 'pi'
passw = 'raspberry'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect ((host, 22))
session = Session()
session.handshake(sock)
session.userauth_password(user, passw)
channel = session.open_session()
channel.shell()
channel.write("echo raspberry | sudo -S su && echo 'pi:newpwhere' | sudo chpasswd\n")
channel.write("exit\n")
size, data = channel.read()
while size > 0:
print(data.decode())
size, data = channel.read()
print ("Exit status: {0}".format(channel.get_exit_status()))
当我尝试使用类似的代码读取具有多个 IP 地址的文件并执行相同的操作时,我收到错误消息:
import os
import socket
from ssh2.session import Session
user = 'pi'
passw = 'raspberry'
file = input("please select the file that contains the hosts: ")
with open(file) as f:
hosts = f.readlines()
def chgpw():
channel = session.open_session()
channel.shell()
channel.write("echo raspberry | sudo -S su && echo 'pi:newpwhere' | sudo chpasswd\n")
channel.write("exit\n")
for i in hosts:
print(i)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((i,22))
session = Session()
session.handshake(sock)
session.userauth_password(user, passw)
chgpw()
print ("Exit status: {0}".format(channel.get_exit_status()))
我收到的错误是:
Traceback (most recent call last):
File "stack-ov.py", line 21, in <module>
sock.connect((i,22))
socket.gaierror: [Errno -2] Name or service not known
打印语句 print(i) 确实在第 21 行发生错误之前打印出列表中的第一个 IP 地址。
readlines
在其 returns 所在的每一行中包含换行符。在使用它之前,您需要 rstrip
i
。
for i in hosts:
stripped = i.rstrip()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((stripped, 22))
换行符使每个地址无效。