使用python在远程电脑执行多行shell脚本
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程序所在主机的主机名,否则程序执行非常缓慢。
页:
[1]