脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 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()
|
执行结果为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 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)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/usr/bin/env python
# coding: utf-8
import
paramiko
ssh
=
paramiko.SSHClient()
#
# automatic reply yes if the ~/.ssh/known_hosts has no
# your machine entry
ssh.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()
|
运行结果为:
1
2
3
4
5
|
[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
|
版权声明:原创作品,如需转载,请注明出处。否则将追究法律责任