- #coding=utf-8
- '''''
- Created on 2012-3-5
- @author: xxx
- '''
- import paramiko
- sshadd = '127.0.0.1'
- sshusr = 'root'
- sshpwd = '123'
- sshport = 13831
- //设置jdk(否则有些语句无法执行)
- sen1 = 'export JAVA_HOME=/usr/local/jdk;'
- //要执行的语句(此处为重启)
- sen2 = "x=`ps x| grep java| grep tomcat$n| awk '{print $1}'`;"
- sen3 = 'kill -9 $x;'
- sen4 = '/app/tomcat$n/bin/startup.sh;'
- num = 3
- //远程登录
- paramiko.util.log_to_file('ssh.txt')
- self.ssh = paramiko.SSHClient()
- self.ssh.set_missing_host_key_to_policy(paramiko.AutoAddPolicy)
- self.ssh.connect(hostname = sshadd, username = sshusr, password = sshpwd, port = sshport)
- stdin, stdout, stderr = ssh.exec_command('n=' + num + ';' + sen1 + sen2 + sen3 + sen4)
- print(stdout.read() + '\n')