美文网首页
Paramiko_ssh访问

Paramiko_ssh访问

作者: 薛东弗斯 | 来源:发表于2024-06-01 07:35 被阅读0次
    # paramiko_pwd.py
    import paramiko
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    ssh.connect('192.168.0.114', port=22, username='test', password='123456')
    
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('ls -ltr /etc')
    # 获取命令结果
    res, err = stdout.read(), stderr.read()
    result = res if res else err
    print(result.decode())
    # 再执行另一条命令并获取命令结果
    stdin, stdout, stderr = ssh.exec_command('df')
    res, err = stdout.read(), stderr.read()
    result = res if res else err
    print(result.decode())
    # 关闭连接
    ssh.close()
    
    total 868
    -rw-r--r-- 1 root root        887 Apr  1  2013 rpc
    -rw-r--r-- 1 root root       2932 Apr  1  2013 protocols
    -rw-r--r-- 1 root root        280 Jun 20  2014 fuse.conf
    -rw-r--r-- 1 root root       3663 Jun 20  2016 screenrc
    -rw-r--r-- 1 root root        604 Sep 15  2018 deluser.conf
    -rw-r----- 1 root daemon      144 Nov 12  2018 at.deny
    -rw-r--r-- 1 root root       1260 Dec 14  2018 ucf.conf
    -rw-r--r-- 1 root root        533 Jan 21  2019 logrotate.conf
    -rw-r--r-- 1 root root      14867 Feb  1  2019 ltrace.conf
    -rw-r--r-- 1 root root       2969 Aug  3  2019 debconf.conf
    -rw-r--r-- 1 root root       5060 Aug 21  2019 hdparm.conf
    -rw-r--r-- 1 root root        642 Sep 24  2019 xattr.conf
    -rw-r--r-- 1 root root      24546 Oct 18  2019 mime.types
    -rw-r--r-- 1 root root        449 Oct 18  2019 mailcap.order
    -rw-r--r-- 1 root root        581 Dec  5  2019 profile
    -rw-r--r-- 1 root root         91 Dec  5  2019 networks
    -rw-r--r-- 1 root root        267 Dec  5  2019 legal
    -rw-r--r-- 1 root root         92 Dec  5  2019 host.conf
    -rw-r--r-- 1 root root         13 Dec  5  2019 debian_version
    -rw-r--r-- 1 root root        552 Dec 17  2019 pam.conf
    -rw-r--r-- 1 root root       1816 Dec 27  2019 ethertypes
    -rw-r--r-- 1 root root        111 Jan 16  2020 magic.mime
    -rw-r--r-- 1 root root        111 Jan 16  2020 magic
    -rw-r--r-- 1 root root         45 Jan 26  2020 bash_completion
    -rw-r--r-- 1 root root       2584 Feb  1  2020 gai.conf
    -r--r----- 1 root root        755 Feb  3  2020 sudoers
    -rw-r--r-- 1 root root      10037 Feb  7  2020 nanorc
    -rw-r--r-- 1 root root      10550 Feb  7  2020 login.defs
    -rw-r--r-- 1 root root       1523 Feb 10  2020 usb_modeswitch.conf
    -rw-r--r-- 1 root root       1382 Feb 11  2020 rsyslog.conf
    -rw-r--r-- 1 root root       1042 Feb 13  2020 crontab
    -rw-r--r-- 1 root root       2351 Feb 13  2020 sysctl.conf
    -rw-r--r-- 1 root root        808 Feb 14  2020 mke2fs.conf
    -rw-r--r-- 1 root root        685 Feb 14  2020 e2scrub.conf
    -rw-r--r-- 1 root root      14464 Feb 16  2020 services
    -rw-r--r-- 1 root root        191 Feb 18  2020 libaudit.conf
    drwxr-xr-x 2 root root       4096 Feb 24  2020 usb_modeswitch.d
    -rw-r--r-- 1 root root       1748 Feb 25  2020 inputrc
    -rw-r--r-- 1 root root       2319 Feb 25  2020 bash.bashrc
    -rw-r--r-- 1 root root       5215 Feb 25  2020 manpath.config
    -rw-r--r-- 1 root root       2996 Apr 14  2020 locale.alias
    -rw-r--r-- 1 root root         34 Apr 14  2020 ld.so.conf
    -rw-r--r-- 1 root root        367 Apr 14  2020 bindresvport.blacklist
    drwxr-xr-x 2 root root       4096 Apr 22  2020 binfmt.d
    -rw-r--r-- 1 root root       6920 Nov  2  2021 overlayroot.conf
    -rw-r--r-- 1 root root       4942 Nov 12  2021 wgetrc
    -rw-r--r-- 1 root root        477 Feb 10  2022 zsh_command_not_found
    drwxrwxr-x 2 root landscape  4096 Feb 14  2022 landscape
    -rw-r--r-- 1 root root      10593 Mar 31  2022 sensors3.conf
    -rw-r--r-- 1 root root         41 Oct 31  2022 multipath.conf
    drwxr-xr-x 2 root root       4096 Jan 20  2023 update-notifier
    -rw-r--r-- 1 root root        104 Mar 14  2023 lsb-release
    lrwxrwxrwx 1 root root         21 Mar 14  2023 os-release -> ../usr/lib/os-release
    -rw-r--r-- 1 root root         19 Mar 14  2023 issue.net
    -rw-r--r-- 1 root root         26 Mar 14  2023 issue
    drwxr-xr-x 5 root root       4096 Mar 14  2023 systemd
    drwxr-xr-x 2 root root       4096 Mar 14  2023 opt
    drwxr-xr-x 2 root root       4096 Mar 14  2023 selinux
    -rw-r--r-- 1 root root          0 Mar 14  2023 subuid-
    -rw-r--r-- 1 root root          0 Mar 14  2023 subgid-
    lrwxrwxrwx 1 root root         39 Mar 14  2023 resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
    lrwxrwxrwx 1 root root         19 Mar 14  2023 mtab -> ../proc/self/mounts
    -rw-r--r-- 1 root root       3028 Mar 14  2023 adduser.conf
    drwxr-xr-x 3 root root       4096 Mar 14  2023 ca-certificates
    drwxr-xr-x 4 root root       4096 Mar 14  2023 dbus-1
    drwxr-xr-x 8 root root       4096 Mar 14  2023 networkd-dispatcher
    drwxr-xr-x 4 root root       4096 Mar 14  2023 xdg
    drwxr-xr-x 2 root root       4096 Mar 14  2023 cron.monthly
    drwxr-xr-x 2 root root       4096 Mar 14  2023 cron.hourly
    -rw-r--r-- 1 root root        510 Mar 14  2023 nsswitch.conf
    -rw-r--r-- 1 root root        195 Mar 14  2023 modules
    drwxr-xr-x 2 root root       4096 Mar 14  2023 newt
    drwxr-xr-x 4 root root       4096 Mar 14  2023 iproute2
    drwxr-xr-x 2 root root       4096 Mar 14  2023 python3
    lrwxrwxrwx 1 root root         23 Mar 14  2023 vtrgb -> /etc/alternatives/vtrgb
    drwxr-xr-x 2 root root       4096 Mar 14  2023 skel
    -rw-r--r-- 1 root root        106 Mar 14  2023 environment
    drwxr-xr-x 2 root root       4096 Mar 14  2023 modules-load.d
    drwxr-xr-x 4 root root       4096 Mar 14  2023 dhcp
    drwxr-xr-x 2 root root       4096 Mar 14  2023 depmod.d
    drwxr-xr-x 4 root root       4096 Mar 14  2023 udev
    -rw-r--r-- 1 root root       6824 Mar 14  2023 ca-certificates.conf.dpkg-old
    drwxr-xr-x 2 root root       4096 Mar 14  2023 ubuntu-advantage
    drwxr-xr-x 3 root root       4096 Mar 14  2023 perl
    drwxr-xr-x 3 root root       4096 Mar 14  2023 gss
    -rw-r--r-- 1 root root         54 Mar 14  2023 crypttab
    drwxr-xr-x 3 root root       4096 Mar 14  2023 dconf
    drwxr-xr-x 4 root root       4096 Mar 14  2023 network
    drwxr-xr-x 4 root root       4096 Mar 14  2023 pki
    drwxr-xr-x 3 root root       4096 Mar 14  2023 libblockdev
    drwxr-xr-x 4 root root       4096 Mar 14  2023 polkit-1
    drwxr-xr-x 3 root root       4096 Mar 14  2023 ModemManager
    drwxr-xr-x 4 root root       4096 Mar 14  2023 logcheck
    drwxr-xr-x 3 root root       4096 Mar 14  2023 pm
    drwxr-xr-x 3 root root       4096 Mar 14  2023 NetworkManager
    -rw-r--r-- 1 root root        350 Mar 14  2023 popularity-contest.conf
    drwxr-xr-x 2 root root       4096 Mar 14  2023 cron.d
    drwxr-xr-x 3 root root       4096 Mar 14  2023 apparmor
    drwxr-xr-x 2 root root       4096 Mar 14  2023 calendar
    drwxr-xr-x 2 root root       4096 Mar 14  2023 mdadm
    drwxr-xr-x 2 root root       4096 Mar 14  2023 byobu
    drwxr-xr-x 2 root root       4096 Mar 14  2023 tmpfiles.d
    -rw-r--r-- 1 root root        146 Mar 14  2023 shells
    drwxr-xr-x 6 root root       4096 Mar 14  2023 sos
    drwxr-xr-x 2 root root       4096 Mar 14  2023 groff
    drwxr-xr-x 5 root root       4096 Mar 14  2023 initramfs-tools
    drwxr-xr-x 3 root root       4096 Mar 14  2023 ufw
    drwxr-xr-x 2 root root       4096 Mar 14  2023 cryptsetup-initramfs
    drwxr-xr-x 2 root root       4096 Mar 14  2023 udisks2
    drwxr-xr-x 3 root root       4096 Mar 14  2023 update-manager
    drwxr-xr-x 3 root root       4096 Mar 14  2023 lvm
    drwxr-xr-x 3 root root       4096 Mar 14  2023 fwupd
    drwxr-xr-x 2 root root       4096 Mar 14  2023 update-motd.d
    drwxr-xr-x 2 root root       4096 Mar 14  2023 rc6.d
    drwxr-xr-x 2 root root       4096 Mar 14  2023 rc1.d
    drwxr-xr-x 2 root root       4096 Mar 14  2023 rc0.d
    drwxr-xr-x 2 root root       4096 Mar 14  2023 PackageKit
    drwxr-xr-x 2 root root       4096 Mar 14  2023 cron.weekly
    lrwxrwxrwx 1 root root         13 Dec  5 05:16 rmt -> /usr/sbin/rmt
    drwxr-xr-x 2 root root       4096 Apr 16 13:47 netplan
    -r--r--r-- 1 root root         33 Apr 16 13:47 machine-id
    drwxr-xr-x 6 root root       4096 Apr 16 13:48 kernel
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 libnl-3
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 UPower
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 grub.d
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 thermald
    -rw-r--r-- 1 root root        630 Apr 16 13:48 fstab
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 pollinate
    -rw-r--r-- 1 root adm         219 Apr 16 13:48 hosts
    -rw-r--r-- 1 root adm           5 Apr 16 13:48 hostname
    -rw-r--r-- 1 root root        711 Apr 16 13:48 hosts.deny
    -rw-r--r-- 1 root root        411 Apr 16 13:48 hosts.allow
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 rc5.d
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 rc4.d
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 rc3.d
    drwxr-xr-x 2 root root       4096 Apr 16 13:48 rc2.d
    drwxr-xr-x 7 root root       4096 Apr 16 13:49 apparmor.d
    drwxr-xr-x 5 root root       4096 Apr 16 13:50 cloud
    drwxr-xr-x 2 root root       4096 Apr 16 13:50 rsyslog.d
    drwxr-xr-x 2 root root       4096 Apr 16 13:50 sudoers.d
    drwxr-xr-x 2 root root       4096 Apr 16 13:50 python3.8
    drwxr-xr-x 2 root root       4096 Apr 16 13:50 vim
    drwxr-xr-x 2 root root       4096 Apr 16 13:51 iscsi
    drwxr-xr-x 4 root root       4096 Apr 16 13:51 vmware-tools
    -rw-r--r-- 1 root root          8 Apr 16 13:51 timezone
    lrwxrwxrwx 1 root root         27 Apr 16 13:51 localtime -> /usr/share/zoneinfo/Etc/UTC
    drwxr-xr-x 4 root root       4096 Apr 16 13:52 security
    drwxr-xr-x 2 root root       4096 Apr 16 13:52 terminfo
    drwxr-xr-x 2 root root       4096 Apr 16 13:52 profile.d
    drwxr-xr-x 2 root root       4096 Apr 16 13:53 ldap
    drwxr-xr-x 2 root root       4096 Apr 16 13:53 sysctl.d
    drwxr-xr-x 4 root root       4096 Apr 16 13:53 ssl
    drwxr-xr-x 3 root root       4096 Apr 16 13:54 apport
    drwxr-xr-x 2 root root       4096 Apr 16 13:54 cron.daily
    drwxr-xr-x 2 root root       4096 Apr 16 13:54 logrotate.d
    -rw-r--r-- 1 root root       7545 Apr 16 13:54 ca-certificates.conf
    drwxr-xr-x 2 root root       4096 Apr 16 13:54 pam.d
    -rw-r----- 1 root shadow      634 Apr 16 13:55 gshadow-
    -rw-r--r-- 1 root root        759 Apr 16 13:55 group-
    drwx------ 2 root root       4096 Apr 16 13:55 multipath
    drwxr-xr-x 2 root root       4096 Apr 16 13:55 console-setup
    -rw-r----- 1 root shadow     1068 Apr 16 13:55 shadow-
    -rw-r--r-- 1 root root       1846 Apr 16 13:55 passwd-
    -rw-r--r-- 1 root root        797 Apr 16 13:55 group
    -rw-r----- 1 root shadow      668 Apr 16 13:55 gshadow
    -rw-r--r-- 1 root root         18 Apr 16 13:55 subuid
    -rw-r--r-- 1 root root         18 Apr 16 13:55 subgid
    drwxr-xr-x 4 root root       4096 Apr 16 13:55 ssh
    -rw-r--r-- 1 root root       1897 Apr 16 13:55 passwd
    -rw-r----- 1 root shadow     1086 Apr 16 13:55 shadow
    drwxr-xr-x 7 root root       4096 May 19 13:45 apt
    drwxr-xr-x 4 root root       4096 May 19 13:46 dpkg
    drwxr-xr-x 3 root root       4096 May 26 13:01 glvnd
    drwxr-xr-x 5 root root       4096 May 26 13:01 vulkan
    drwxr-xr-x 2 root root       4096 May 26 13:01 sensors.d
    drwxr-xr-x 3 root root       4096 May 26 13:01 pulse
    drwxr-xr-x 2 root root       4096 May 28 22:10 bash_completion.d
    drwxr-xr-x 2 root root       4096 Jun  1 00:17 ld.so.conf.d
    drwxr-xr-x 3 root root       4096 Jun  1 00:17 default
    drwxr-xr-x 2 root root       4096 Jun  1 00:17 modprobe.d
    -rw-r--r-- 1 root root       9440 Jun  1 00:17 locale.gen
    drwxr-xr-x 6 root root       4096 Jun  1 01:48 X11
    drwxr-xr-x 2 root root       4096 Jun  1 01:48 init.d
    drwxr-xr-x 2 root root       4096 Jun  1 01:48 rcS.d
    drwxr-xr-x 4 root root       4096 Jun  1 01:48 fonts
    drwxr-xr-x 2 root root       4096 Jun  1 01:48 environment.d
    drwxr-xr-x 2 root root       4096 Jun  1 01:48 alternatives
    -rw-r--r-- 1 root root       3893 Jun  1 01:48 mailcap
    drwxr-xr-x 2 root root       4096 Jun  1 01:48 gtk-3.0
    -rw-r--r-- 1 root root      42480 Jun  1 01:48 ld.so.cache
    
    Filesystem                        1K-blocks    Used Available Use% Mounted on
    udev                                1960752       0   1960752   0% /dev
    tmpfs                                401432    1140    400292   1% /run
    /dev/mapper/ubuntu--vg-ubuntu--lv   8408452 4033596   3926140  51% /
    tmpfs                               2007152       0   2007152   0% /dev/shm
    tmpfs                                  5120       0      5120   0% /run/lock
    tmpfs                               2007152       0   2007152   0% /sys/fs/cgroup
    /dev/loop0                            94080   94080         0 100% /snap/lxd/24061
    /dev/loop2                            39680   39680         0 100% /snap/snapd/21465
    /dev/loop1                            64896   64896         0 100% /snap/core20/1828
    /dev/sda2                           1768056  214512   1445412  13% /boot
    tmpfs                                401428       0    401428   0% /run/user/1000
    /dev/loop4                            65536   65536         0 100% /snap/core20/2318
    /dev/loop5                            39808   39808         0 100% /snap/snapd/21759
    
    # paramiko_transport.py
    import paramiko
    # 创建Transport对象
    transport = paramiko.Transport(('192.168.0.114', 22))
    # 建立SSH连接
    transport.connect(username='test', password='123456')
    # 创建SSHClient对象并将其transport变量指定为上述Transport对象
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    # 使用SSHClient对象的方法进行远程操作
    stdin, stdout, stderr = ssh.exec_command('ls -ltr /etc')
    print (stdout.read().decode())
    # 可以创建SFTPClient对象,继续利用Transport对象及其连接执行文件传输操作
    # 关闭Transport对象及其连接
    
    # stdin, stdout, stderr = ssh.exec_command('sudo -S df')
    # stdin.write('abc123\n')
    # stdin.flush()
    # print (stdout.read().decode())
    
    transport.close()
    
    # paramiko_tftp.py
    
    import paramiko
    # 创建Transport对象
    transport = paramiko.Transport(('192.168.0.114', 22))
    # 建立SSH连接
    transport.connect(username='test', password='123456')
    # 创建SSHClient对象并将其transport变量指定为上述Transport对象
    ssh = paramiko.SSHClient()
    sftp = paramiko.SFTPClient.from_transport(transport)
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('./paramiko_pwd.py', '/tmp/paramiko_pwd.py')
    dir = sftp.listdir(path='/tmp')
    print(dir)
    # 将remove_path 下载到本地 local_path
    sftp.get('/etc/hosts', 'centos_host1')   # 将'/etc/hosts' 下载到本地,并重新命名为'centos_host1'
    
    transport.close()
    
    
    # paramiko_bykey.py
    import paramiko
    private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    ssh.connect(hostname='192.168.0.114', port=22, username='test', pkey=private_key)
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df')
    # 获取命令结果
    res,err = stdout.read(),stderr.read()
    result = res if res else err
    print(result.decode())
    # 关闭连接
    ssh.close()
    
    # paramiko_sudo.py
    import paramiko
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 使用非root用户连接服务器
    ssh.connect('192.168.0.114', port=22, username='test', password='123456')
    # 执行sudo命令时加上-S选项
    stdin, stdout, stderr = ssh.exec_command('sudo -S cat /etc/shadow')
    # 通过标准输入提供用户密码,注意\n表示回车换行
    stdin.write('123456\n')
    # 刷新标准输入的内部缓冲区,将其中内容立即提供给标准输入
    res, err = stdout.read(), stderr.read()
    result = res if res else err
    print(result.decode())
    # 关闭连接
    ssh.close()
    
    # paramiko_tty.py
    import paramiko
    import sys
    import socket
    import select
    #from paramiko.py3compat import u
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    ssh.connect('192.168.0.114', port=22, username='test', password='123456')
    # 启动交互式Shell会话返回一个新的通道
    channel = ssh.invoke_shell()
    # 通过循环监控用户输入和服务器的回显数据
    while True:
        # 通过select模块的select方法监听终端的输入输出,一旦变化,就将数据发送给服务器
        # 其中sys.stdin用于处理用户的输入,channel用于接收服务器返回的数据
        readable, writeable, error = select.select([channel, sys.stdin, ],[],[],1)
        # 如果服务器有数据返回(通常是命令执行结果),则在终端进行显示
        if channel in readable:
            try:
                #data = u(channel.recv(1024))   # 获取服务器的回显数据
                data = (channel.recv(1024))   # 获取服务器的回显数据
                data = str(data, encoding='utf-8')  
                if len(data) == 0:
                    print('\r\n*** EOF\r\n')
                    break
                sys.stdout.write(data)         # 写入标准输出的缓冲区
                sys.stdout.flush()              # 刷新缓冲区,将缓冲区内容显示出来
            except socket.timeout:
                pass
        # 如果用户在终端输入内容(通常是Shell命令),则将内容发送到服务器
        if sys.stdin in readable:
            input = sys.stdin.readline()
            channel.sendall(input)
    # 关闭通道和连接
    channel.close()
    ssh.close()
    
    
    

    相关文章

      网友评论

          本文标题:Paramiko_ssh访问

          本文链接:https://www.haomeiwen.com/subject/uokdqjtx.html