jhegw 发表于 2015-3-16 09:53:28

python 实现远程主机执行命令函数

#!/usr/bin/python
# coding=utf-8
import os
import paramiko

\\定义 h_name 远程主机名 h_cmd 执行的命令
def remote_cmd(h_name,h_cmd):

      sshadd = h_name

      sshusr = 'root'

      sshpwd = 'YourPasswd'

1
<br>





      sshport = 22

      sen1 = 'export JAVA_HOME= YourJdkPath ;'

      sen2 = '命令1;'

      sen3 = '命令2;'

      num = '2' //命令数量

      paramiko.util.log_to_file('ssh.txt')

      ssh = paramiko.SSHClient()

      known_host="/root/.ssh/known_hosts"

      ssh.load_system_host_keys(known_host)

      privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')

      mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile,password='密钥密码')

      ssh.connect(hostname = sshadd, username = sshusr, password = sshpwd, port = sshport)

      stdin, stdout, stderr = ssh.exec_command('n=' + num + ';' + sen1 + sen2 + sen3 )

      return stdout.read().strip('\n')

页: [1]
查看完整版本: python 实现远程主机执行命令函数