脚本如下:
# cat myssh.py #!/usr/bin/env python import paramiko hostname = ‘192.168.56.101‘ port = 22 username = ‘root‘ password = ‘111111‘ if __name__ == "__main__": paramiko.util.log_to_file(‘paramiko.log‘) s = paramiko.SSHClient() s.load_system_host_keys() s.connect(hostname, port, username, password) stdin, stdout, stderr = s.exec_command(‘ifconfig‘) print stdout.read() s.close()
执行结果为:
# python myssh.py eth0 Link encap:Ethernet HWaddr 08:00:27:29:46:94 inet addr:192.168.56.101 Bcast:192.168.56.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe29:4694/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:602 errors:0 dropped:0 overruns:0 frame:0 TX packets:381 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:64585 (63.0 KiB) TX bytes:61809 (60.3 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:14 errors:0 dropped:0 overruns:0 frame:0 TX packets:14 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1294 (1.2 KiB) TX bytes:1294 (1.2 KiB)
本文出自 “天道酬勤” 博客,请务必保留此出处http://lavenliu.blog.51cto.com/5060944/1689169
原文地址:http://lavenliu.blog.51cto.com/5060944/1689169