python ssh 执行shell命令的示例


    # -*- coding: utf-8 -*-
    
    import paramiko
    import threading
    
    def run(host_ip, username, password, command):
      ssh = paramiko.SSHClient()
      try:
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host_ip, 22, username, password)
    
        print('===================exec on [%s]=====================' % host_ip)
    
    
        stdin, stdout, stderr = ssh.exec_command(command, timeout=300)
        out = stdout.readlines()
      for o in out:
          print (o.strip('\n'))
      except Exception as ex:
        print('error, host is [%s], msg is [%s]' % (host_ip, ex.message))
      finally:
        ssh.close()
    
    
    if __name__ == '__main__':
    
      # 将需要批量执行命令的host ip地址填到这里
      # eg: host_ip_list = ['IP1', 'IP2']
    
      host_ip_list = ['147.116.20.19']
      for _host_ip in host_ip_list:
    
        # 用户名,密码,执行的命令填到这里
        run(_host_ip, 'tzgame', 'tzgame@1234', 'df -h')
        run(_host_ip, 'tzgame', 'tzgame@1234', 'ping -c 5 220.181.38.148')

pycrypto,由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto


    pip3 install pycrypto
    pip3 install paramiko
    

(1)基于用户名和密码的连接


    import paramiko
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    # 连接服务器
    ssh.connect(hostname='c1.salt.com', port=22, username='GSuser', password='123')
    
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('ls')
    
    # 获取命令结果
    result = stdout.read()
    
    # 关闭连接
    ssh.close()

(2)基于公钥秘钥连接


    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    # 连接服务器
    ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key)
    
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df')
    
    # 获取命令结果
    result = stdout.read()
    
    # 关闭连接
    ssh.close()

SFTPClient:

用于连接远程服务器并进行上传下载功能。

(1)基于用户名密码上传下载


    import paramiko
    
    transport = paramiko.Transport(('hostname',22))
    transport.connect(username='GSuser',password='123')
    
    sftp = paramiko.SFTPClient.from_transport(transport)
    
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
    
    transport.close()

(2)基于公钥秘钥上传下载


    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='GSuser', pkey=private_key )
    
    sftp = paramiko.SFTPClient.from_transport(transport)
    
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
    
    transport.close()

下面是多线程执行版本


    #!/usr/bin/python
    #coding:utf-8
    import threading
    import subprocess
    import os
    import sys
    
    
    sshport = 13131
    log_path = 'update_log'
    output = {}
    
    def execute(s, ip, cmd, log_path_today):
      with s:   
        cmd = '''ssh -p%s root@%s -n "%s" ''' % (sshport, ip, cmd)
    
        ret = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output[ip] = ret.stdout.readlines()
    
    
    
    
    if __name__ == "__main__":
      if len(sys.argv) != 3:
        print "Usage: %s config.ini cmd" % sys.argv[0]
        sys.exit(1)
                       
      if not os.path.isfile(sys.argv[1]):
        print "Usage: %s is not file!" % sys.argv[1]
        sys.exit(1)
                         
      cmd = sys.argv[2]
                       
      f = open(sys.argv[1],'r')
      list = f.readlines()
      f.close()
      today = datetime.date.today()
      log_path_today = '%s/%s' % (log_path,today)
      if not os.path.isdir(log_path_today):
        os.makedirs(log_path_today)
                       
      threading_num = 100
      if threading_num > len(list):
        threading_num = len(list)
    
      s = threading.Semaphore(threading_num)
                       
      for line in list:
        ip = line.strip()
        t = threading.Thread(target=execute,args=(s, ip,cmd,log_path_today))
        t.setDaemon(True)
        t.start()
                         
      main_thread = threading.currentThread()
      for t in threading.enumerate():
        if t is main_thread:
          continue
        t.join()
                         
      for ip,result in output.items():
        print "%s: " % ip
        for line in result:
          print "  %s" % line.strip()
                       
      print "Done!"

以上脚本读取两个参数,第一个为存放IP的文本,第二个为shell命令

执行效果如下


    # -*- coding:utf-8 -*-
    import requests
    from requests.exceptions import RequestException
    import os, time
    import re
    from lxml import etree
    import threading
    
    lock = threading.Lock()
    def get_html(url):
    
      response = requests.get(url, timeout=10)
      # print(response.status_code)
      try:
        if response.status_code == 200:
    
          # print(response.text)
          return response.text
        else:
           return None
      except RequestException:
        print("请求失败")
        # return None
    
    
    def parse_html(html_text):
    
      html = etree.HTML(html_text)
    
      if len(html) > 0:
        img_src = html.xpath("//img[@class='photothumb lazy']/@data-original") # 元素提取方法
        # print(img_src)
        return img_src
    
      else:
        print("解析页面元素失败")
    
    def get_image_pages(url):
      html_text = get_html(url) # 获取搜索url响应内容
      # print(html_text)
      if html_text is not None:
        html = etree.HTML(html_text) # 生成XPath解析对象
        last_page = html.xpath("//div[@class='pages']//a[last()]/@href") # 提取最后一页所在href链接
        print(last_page)
        if last_page:
          max_page = re.compile(r'(\d+)', re.S).search(last_page[0]).group() # 使用正则表达式提取链接中的页码数字
          print(max_page)
          print(type(max_page))
          return int(max_page) # 将字符串页码转为整数并返回
        else:
          print("暂无数据")
          return None
      else:
        print("查询结果失败")
    
    
    def get_all_image_url(page_number):
      base_url = 'https://imgbin.com/free-png/naruto/'
      image_urls = []
    
      x = 1 # 定义一个标识,用于给每个图片url编号,从1递增
      for i in range(1, page_number):
        url = base_url + str(i) # 根据页码遍历请求url
        try:
          html = get_html(url) # 解析每个页面的内容
          if html:
            data = parse_html(html) # 提取页面中的图片url
            # print(data)
            # time.sleep(3)
            if data:
              for j in data:
                image_urls.append({
                  'name': x,
                  'value': j
                })
                x += 1 # 每提取一个图片url,标识x增加1
        except RequestException as f:
          print("遇到错误:", f)
          continue
      # print(image_urls)
      return image_urls
    
    def get_image_content(url):
      try:
        r = requests.get(url, timeout=15)
        if r.status_code == 200:
          return r.content
        return None
      except RequestException:
        return None
    
    def main(url, image_name):
      semaphore.acquire() # 加锁,限制线程数
      print('当前子线程: {}'.format(threading.current_thread().name))
      save_path = os.path.dirname(os.path.abspath('.')) + '/pics/'
      try:
        file_path = '{0}/{1}.jpg'.format(save_path, image_name)
        if not os.path.exists(file_path): # 判断是否存在文件,不存在则爬取
          with open(file_path, 'wb') as f:
            f.write(get_image_content(url))
            f.close()
    
            print('第{}个文件保存成功'.format(image_name))
    
        else:
          print("第{}个文件已存在".format(image_name))
    
        semaphore.release() # 解锁imgbin-多线程-重写run方法.py
    
      except FileNotFoundError as f:
        print("第{}个文件下载时遇到错误,url为:{}:".format(image_name, url))
        print("报错:", f)
        raise
    
      except TypeError as e:
        print("第{}个文件下载时遇到错误,url为:{}:".format(image_name, url))
        print("报错:", e)
    
    class MyThread(threading.Thread):
      """继承Thread类重写run方法创建新进程"""
      def __init__(self, func, args):
        """
    
        :param func: run方法中要调用的函数名
        :param args: func函数所需的参数
        """
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
    
      def run(self):
        print('当前子线程: {}'.format(threading.current_thread().name))
        self.func(self.args[0], self.args[1])
        # 调用func函数
        # 因为这里的func函数其实是上述的main()函数,它需要2个参数;args传入的是个参数元组,拆解开来传入
    
    
    if __name__ == '__main__':
      start = time.time()
      print('这是主线程:{}'.format(threading.current_thread().name))
    
      urls = get_all_image_url(5) # 获取所有图片url列表
      thread_list = [] # 定义一个列表,向里面追加线程
      semaphore = threading.BoundedSemaphore(5) # 或使用Semaphore方法
      for t in urls:
        # print(i)
    
        m = MyThread(main, (t["value"], t["name"])) # 调用MyThread类,得到一个实例
    
        thread_list.append(m)
    
      for m in thread_list:
    
        m.start() # 调用start()方法,开始执行
    
      for m in thread_list:
        m.join() # 子线程调用join()方法,使主线程等待子线程运行完毕之后才退出
    
    
      end = time.time()
      print(end-start)
      # get_image_pages(<https://imgbin.com/free-png/Naruto>)

以上就是python ssh 执行shell命令的示例的详细内容,更多关于python ssh 执行shell命令的资料请关注脚本之家其它相关文章!

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Python可以通过paramiko库实现远程执行shell脚本。具体步骤如下: 1. 安装paramiko库:使用pip install paramiko命令进行安装。 2. 导入paramiko库:在Python脚本中使用import paramiko语句导入paramiko库。 3. 创建SSHClient对象:使用paramiko库中的SSHClient类创建SSHClient对象。 4. 连接远程主机:使用SSHClient对象的connect方法连接远程主机。 5. 执行shell脚本:使用SSHClient对象的exec_command方法执行shell脚本。 6. 关闭连接:使用SSHClient对象的close方法关闭连接。 示例代码如下: ``` import paramiko # 创建SSHClient对象 ssh = paramiko.SSHClient() # 自动添加主机名和密钥到本地主机的HostKeys对象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接远程主机 ssh.connect(hostname='remote_host', port=22, username='username', password='password') # 执行shell脚本 stdin, stdout, stderr = ssh.exec_command('sh /path/to/script.sh') # 输出执行结果 print(stdout.read().decode()) # 关闭连接 ssh.close() ``` ### 回答2: Python可以通过调用Paramiko库来实现远程执行shell脚本。 Paramiko是一个基于PythonSSH协议库,可以实现SSH客户端和SSH服务器。以下是一个示例代码,用于远程执行shell脚本: ```python import paramiko # 远程主机信息 hostname = 'localhost' username = 'root' password = 'password' # 建立SSH客户端连接 client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname, username=username, password=password) # 远程执行shell脚本 stdin, stdout, stderr = client.exec_command('sh /path/to/script.sh') # 输出执行结果 print(stdout.read()) # 断开SSH连接 client.close() ``` 在这个代码中,我们首先通过Paramiko库建立了一个SSH客户端连接。然后,使用exec_command()方法来运行远程主机上的shell脚本。最后,我们利用stdout.read()方法读取了执行结果并进行输出。最后,我们使用close()方法关闭了与远程主机的连接。 值得注意的是,虽然使用Paramiko库可以方便地实现远程执行shell脚本,但为了安全和授权等原因,远程主机的安全设置可能会要求进行额外的调整。此外,如果要使用SSH密钥进行身份验证,可能需要编写其他代码。 ### 回答3: Python 是一种强大的编程语言,可以轻松地执行操作系统上的命令和脚本。如果你需要在远程服务器上运行一些 shell 脚本,Python 也可以实现。在这里,我们将探讨如何在 Python 中远程执行 shell 脚本。 首先,我们需要使用 PythonSSH 库进行远程登录到服务器。Python 的 Paramiko 库提供了 SSH 客户端的实现,可以帮助我们连接到服务器上的 SSH 服务。我们可以使用 pip 或 conda 安装 Paramiko 库: ``` pip install paramiko ``` 或 ``` conda install paramiko ``` 在安装 Paramiko 库之后,我们需要导入库并使用 ssh 客户端连接到远程服务器。以下是连接到服务器的 Python 代码示例: ```python import paramiko # Create an SSH client client = paramiko.SSHClient() # Automatically add the server key to the client client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the remote server client.connect('your_server_ip', username='your_username', password='your_password') ``` 这将创建一个 SSH 客户端,并使用提供的用户名和密码连接远程服务器。 现在,我们可以使用 ssh 客户端执行 shell 命令和脚本。以下是使用 Python 远程执行 shell 脚本的代码示例: ```python import paramiko # Create an SSH client client = paramiko.SSHClient() # Automatically add the server key to the client client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the remote server client.connect('your_server_ip', username='your_username', password='your_password') # Execute a shell script on the remote server stdin, stdout, stderr = client.exec_command('bash /path/to/your/script.sh') # Print the output of the command print(stdout.read().decode('utf-8')) # Close the SSH connection client.close() ``` 在这个示例中,我们使用 exec_command() 方法执行 shell 脚本。该方法返回三个文件流,分别是标准输入、标准输出和标准错误输出。我们可以使用 stdout.read() 方法获取输出,并使用 decode() 方法将其转换为 UTF-8 字符串。 最后,我们需要关闭 SSH 连接,以便释放资源并避免连接泄漏。 总之,使用 Python 执行远程 shell 脚本十分简单,我们只需要使用 Paramiko 库连接到服务器并使用 exec_command() 方法远程执行脚本即可。这种方法可以在远程服务器上执行任何类型的 shell 脚本,从而可以实现更高效和自动化的部署。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值