使用netmiko
时,我不知道如何发送sudo密码。
例如,我要运行:
sudo apt-get -y install apache2
Linux会要求输入密码,因此我必须在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()
答案 0 :(得分:0)
您的linux词典需要一个秘密参数。您还需要在connect之后调用enable()方法。
请注意,相对于其他工具,Netmiko不太适合Linux自动化。还有其他一些工具将是更好的选择。
答案 1 :(得分:0)
这个示例对我有用(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()