python执行远程linux命令

不及物动词 其他 82

回复

共3条回复 我来回复
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    要在Python中执行远程Linux命令,可以使用paramiko库。paramiko是一个Python实现的SSHv2协议的模块,可以用来连接和操作远程主机。

    首先,需要安装paramiko库。可以使用pip命令在命令行中安装paramiko:pip install paramiko

    接下来,可以使用以下代码实现执行远程Linux命令的功能:

    “`
    import paramiko

    # 远程主机的IP地址、用户名和密码
    hostname = ‘remote_host’ # 远程主机的IP地址或主机名
    username = ‘remote_username’ # 远程主机的用户名
    password = ‘remote_password’ # 远程主机的密码

    # 创建SSH客户端对象
    ssh_client = paramiko.SSHClient()
    # 自动添加远程主机的SSH密钥
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接到远程主机
    ssh_client.connect(hostname, username=username, password=password)

    # 执行远程命令
    command = ‘your_command’ # 要执行的远程命令
    stdin, stdout, stderr = ssh_client.exec_command(command)

    # 获取命令执行结果
    output = stdout.read().decode(‘utf-8’)
    errors = stderr.read().decode(‘utf-8’)

    # 输出命令执行结果
    print(‘命令执行结果:’)
    print(output)

    # 关闭SSH连接
    ssh_client.close()
    “`

    在代码中,首先创建一个SSHClient对象,设置为自动添加远程主机的SSH密钥。然后使用connect()方法连接到远程主机,传入远程主机的IP地址(或主机名)、用户名和密码。接下来,使用exec_command()方法执行远程命令,并通过stdout和stderr对象获取命令执行结果。最后,打印命令执行结果并关闭SSH连接。

    需要注意的是,参数类型都是字符串。如果有需要,可以根据需要修改。

    以上就是使用Python执行远程Linux命令的方法。通过使用paramiko库,可以方便地在Python中执行远程命令,并获取命令执行结果。

    2年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    Python可以通过SSH协议执行远程Linux命令。下面是一些实现远程执行命令的方法:

    1. Paramiko模块:Paramiko是一个Python库,用于通过SSH协议进行远程操作。使用Paramiko可以连接到远程Linux主机,并执行命令。下面是一个使用Paramiko执行远程命令的示例:

    “`python
    import paramiko

    # 创建SSH客户端对象
    client = paramiko.SSHClient()

    # 自动添加策略
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # 连接到远程主机
    client.connect(hostname=’remote_host’, username=’username’, password=’password’)

    # 执行命令
    stdin, stdout, stderr = client.exec_command(‘command’)

    # 获取命令输出
    output = stdout.read().decode()

    # 打印命令输出
    print(output)

    # 关闭SSH连接
    client.close()
    “`

    2. fabric模块:Fabric是Python的一个库,用于执行远程系统管理任务。它构建在Paramiko库之上,并为执行远程命令提供了更简单的接口。下面是一个使用Fabric执行远程命令的示例:

    “`python
    from fabric import Connection

    # 创建连接对象
    c = Connection(host=’remote_host’, user=’username’, connect_kwargs={‘password’: ‘password’})

    # 执行命令
    result = c.run(‘command’, hide=True)

    # 获取命令输出
    output = result.stdout

    # 打印命令输出
    print(output)

    # 关闭连接
    c.close()
    “`

    3. subprocess模块:subprocess模块是Python标准库中用于创建子进程并与之进行通信的模块。可以使用subprocess模块在本地机器上通过SSH连接到远程Linux主机,并执行命令。下面是一个使用subprocess模块执行远程命令的示例:

    “`python
    import subprocess

    # 执行ssh命令连接到远程主机,并执行命令
    result = subprocess.run([‘ssh’, ‘username@remote_host’, ‘command’], capture_output=True, text=True)

    # 获取命令输出
    output = result.stdout

    # 打印命令输出
    print(output)
    “`

    4. pxssh模块:pxssh模块是Python的一个第三方模块,它基于paramiko模块,提供了一个更简单的接口来执行远程命令。下面是一个使用pxssh模块执行远程命令的示例:

    “`python
    from pexpect import pxssh

    # 创建pxssh对象
    s = pxssh.pxssh()

    # 连接到远程主机
    s.login(‘remote_host’, ‘username’, ‘password’)

    # 执行命令
    s.sendline(‘command’)
    s.prompt()

    # 获取命令输出
    output = s.before.decode()

    # 打印命令输出
    print(output)

    # 关闭SSH连接
    s.logout()
    “`

    5. fabric3模块:fabric3是fabric的一个Python 3兼容版本,为执行远程系统管理任务提供了更简单的接口。与fabric模块类似,使用fabric3可以连接到远程Linux主机,并执行命令。下面是一个使用fabric3执行远程命令的示例:

    “`python
    from fabric.api import env, run

    # 设置环境变量
    env.host_string = ‘remote_host’
    env.user = ‘username’
    env.password = ‘password’

    # 执行命令
    output = run(‘command’)

    # 打印命令输出
    print(output)
    “`

    这些方法都可以用来在Python中执行远程Linux命令。选择适合你项目或任务需求的方法,并根据需要进行相应的配置和调整。

    2年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    要在Python中执行远程Linux命令,有几种方法可以实现。下面将逐步介绍这些方法:

    方法1:使用paramiko模块

    1. 安装paramiko模块:
    “`
    pip install paramiko
    “`

    2. 导入paramiko模块:
    “`python
    import paramiko
    “`

    3. 创建SSH客户端对象:
    “`python
    ssh = paramiko.SSHClient()
    “`

    4. 添加hostkey策略(默认策略是拒绝未知的hostkeys):
    “`python
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    “`

    5. 连接到远程主机:
    “`python
    ssh.connect(hostname, port, username, password)
    “`

    6. 执行远程命令:
    “`python
    stdin, stdout, stderr = ssh.exec_command(command)
    “`

    7. 获取命令输出:
    “`python
    output = stdout.read().decode(‘utf-8’)
    “`

    8. 关闭SSH连接:
    “`python
    ssh.close()
    “`

    方法2:使用fabric模块

    1. 安装fabric模块:
    “`
    pip install fabric
    “`

    2. 导入fabric模块:
    “`python
    from fabric import Connection
    “`

    3. 连接到远程主机:
    “`python
    c = Connection(host=hostname, user=username, connect_kwargs={“password”: password})
    “`

    4. 执行远程命令:
    “`python
    result = c.run(command, hide=True)
    “`

    5. 获取命令输出:
    “`python
    output = result.stdout.strip()
    “`

    6. 关闭连接:
    “`python
    c.close()
    “`

    方法3:使用subprocess模块

    1. 导入subprocess模块:
    “`python
    import subprocess
    “`

    2. 执行远程命令:
    “`python
    output = subprocess.check_output([‘ssh’, hostname, command])
    “`

    3. 解码命令输出:
    “`python
    output = output.decode(‘utf-8’)
    “`

    这些方法都可以在Python中执行远程Linux命令。 根据你的需求和偏好选择其中的一种即可。

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部