1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#!/usr/bin/env python import paramiko
hostname = 'was1'
port = 22
username = 'mqm'
password = 'redhat'
cmd = '''
function printline { echo "-------------------------------$1-------------------------------"
}
if [ -d /tmp/test ] then printline check_test_dir echo "/tmp/test exist" else mkdir /tmp/test fi cat /etc/fstab>/tmp/test/fstab.bk
printline ls_test ls /tmp/test printline cat_fstab.bk cat /tmp/test/fstab.bk printline ip_check /sbin/ifconfig ''' if __name__ = = '__main__' :
paramiko.util.log_to_file( 'paramiko.log' )
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #s.load_system_host_keys() s.connect(hostname,port,username,password) stdin,stdout,stderr = s.exec_command(cmd)
print stdout.read()
print stderr.read()
s.close() |
注:远程主机必须能解析python程序所在主机的主机名,否则程序执行非常缓慢。
本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1773269,如需转载请自行联系原作者