脚本如下:
# cat myssh.py #!/usr/bin/env pythonimport paramikohostname = '192.168.56.101'port = 22username = '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)
#!/usr/bin/env python# coding: utf-8import paramikossh = paramiko.SSHClient()# # automatic reply yes if the ~/.ssh/known_hosts has no# your machine entryssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect('192.168.56.102', 22, 'root', '111111')stdin, stdout, stderr = ssh.exec_command('df -h')print stdout.read()ssh.close()
运行结果为:
[root@python test]# python paramiko01.py Filesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 36G 3.7G 31G 11% /tmpfs 499M 12K 499M 1% /dev/shm/dev/sda1 485M 33M 427M 8% /boot