在使用netmiko时,我不知道如何发送sudo密码。
例如,我想运行:
sudo apt-get -y install apache2Linux将要求输入密码,因此我必须在我的python脚本中指定它。
from netmiko import ConnectHandler
linux = {
'device_type': 'linux',
'ip': '192.168.0.134',
'username': 'u1',
'password': 'testpass',
'port': 229,
'verbose':True
}
connection = ConnectHandler(**linux)
output = connection.send_command('sudo apt-get update && apt-get -y install apache2')
print(output)
connection.disconnect()发布于 2018-10-20 02:50:44
您的linux字典需要一个秘密参数。您还需要在连接后调用enable()方法。
请注意,与其他工具相比,Netmiko并不适合Linux自动化。这里还有其他几个工具将是更好的选择。
发布于 2020-04-28 07:58:22
这个例子适用于我(Linux Ubuntu 16):
params = {
'device_type': 'linux',
'ip': '1.2.3.4',
'username': 'myuser',
'password': 'mypassword',
'secret': 'mypassword',
'port': '22'
}
...
myssh.enable(cmd="sudo su", pattern='password')
...run some root commands...
myssh.exit_enable_mode()https://stackoverflow.com/questions/52886779
复制相似问题