Home > Operation and Maintenance > Linux Operation and Maintenance > How does Python implement remote execution of linux commands through the paramiko library?

How does Python implement remote execution of linux commands through the paramiko library?

WBOY
Release: 2023-05-17 10:55:50
forward
1599 people have browsed it

(1) First install the paramiko library

pip install paramiko
Copy after login

(2) The following classes are encapsulated and can be used directly

import paramiko

class SSHClient(object):
    def __init__(self,host,username,password,port=22):
        self.__host=host
        self.__username=username
        self.__password=password
        self.__port=port
        self.__ssh=None
        self.connect()

    def __del__(self):
        self.close()

    def connect(self):
        self.__ssh = paramiko.SSHClient()
        self.__ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.__ssh.connect(hostname=self.__host,port=self.__port,username=self.__username,password=self.__password)

    def exec(self,cmd):
        print(f"begin to run remote cmd: {cmd}")
        stdin, stdout, stderr = self.__ssh.exec_command(cmd,timeout=1800)
        returncode = stdout.channel.recv_exit_status()
        output=stdout.read().decode('utf-8')
        return output

    def close(self):
        self.__ssh.close()
Copy after login

(3) For example, prepare an ip address of 192.168.1.12 linux virtual machine, and then directly use the above encapsulated class as follows to achieve remote execution of linux commands

ssh=SSHClient(host="192.168.1.12",username="root",password="xxxxxx")
output=ssh.exec("ifconfig")
print(output)
Copy after login

(4) The execution results are as follows

begin to run remote cmd: ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 24 0e:3a1:da7:6590:b39f:e15: 6b3d:7e7 prefixlen 64 scopeid 0x0
ether 00:0c:29:58:d8:4 c txqueuelen 1000 (Ethernet )
RX packets 195340 bytes 148862388 (141.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 163425 bytes 20837281 (19.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536

inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099 mtu 1500

inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122 .255
ether 52:54:00 :e8:3f:5c txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

The above is the detailed content of How does Python implement remote execution of linux commands through the paramiko library?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template