Getting Started with the Paramiko module in python one: SSH login and execute command

Source: Internet
Author: User
Tags log ssh in python

Recently busy learning Python, see a Paramiko module, seemingly very powerful, learn to write a small program, you can log on to Linux directly under Windows, execute and return execution results.

The following is directly attached to the code:

#!/usr/bin/env python    
import paramiko    
        
#hostname = ' 192.168.0.102 '    
hostname= ' 172.28.102.250 '
Username= ' root '
password= ' abc '
        
#port =22    
if __name__== ' __main__ ':    
        paramiko.util.log_to_file (' Paramiko.log ')    
        S=paramiko. Sshclient ()    
        #s. Load_system_host_keys () S.set_missing_host_key_policy () (    
        Paramiko. Autoaddpolicy ())    
        s.connect (hostname = hostname,username=username, Password=password)    
        stdin,stdout, Stderr=s.exec_command (' ifconfig;free;df-h ')    
        print stdout.read ()    
        s.close ()

Below look at the effect, hehe:

S\501914252.hca-fw9cx2x\desktop\ Learning materials \python paramiko-ssh.py "eth0 Link encap:ethernet hwaddr inet addr:172.32.34.240 bcast:172.32.255.255 mask:255.255.255.0 up broadcast RUNNING MTU:
          1500 metric:1 RX packets:1428 errors:0 dropped:0 overruns:0 frame:0 TX packets:2 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:190928 (186 .4 KiB) TX bytes:128 (128.0 b) interrupt:20 eth1 Link encap:ethernet hwaddr 00:1f:29:03:54:3 D inet addr:172.28.102.250 bcast:172.28.255.255 mask:255.255.0.0 up broadcast RUNNING MTU
          : 1500 metric:1 RX packets:99698 errors:0 dropped:0 overruns:0
          TX packets:12305 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:36420324 (34.7 MiB) TX bytes:923630 (901.9 KiB) Lo Link encap:local loopback   
          inet addr:127.0.0.1 mask:255.0.0.0 up loopback RUNNING mtu:16436 RX metric:1:
          0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:0  (0.0 b) TX bytes:0 (0.0 b) peth1 Link encap:ethernet hwaddr FE:FF:FF:FF:FF:FF up broadcast RUNNING Noarp MTU
          : 1500 metric:1 RX packets:99686 errors:0 dropped:0 overruns:0 TX packets:12333 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX txqueuelen:1000  5 (35.1 MiB) TX bytes:1006230 (982.6 KiB) interrupt:20 memory:f3000000-f3010000 Link Encap:ethernet hwaddr FE:FF:FF:FF:FF:FF up broadcast RUNNING noarp mtu:1500 RX metric:1
          s:12305 errors:0 dropped:0 overruns:0 frame:0
      TX packets:99702 errors:0 dropped:0 overruns:0 carrier:0    collisions:0 txqueuelen:0 RX bytes:923630 (901.9 KiB) TX bytes:36420811 (34.7 MiB) vif1.0 Link encap:ethernet hwaddr fe:ff:ff:ff:f F:ff up broadcast RUNNING noarp mtu:1500 metric:1 RX packets:5 errors:0 dropped:0 overruns:0 fra
          me:0 TX packets:85175 errors:0 dropped:542 overruns:0 carrier:0 collisions:0 txqueuelen:500 RX bytes:140 (1 40.0 b) TX bytes:20377326 (19.4 MiB) vif2.0 Link encap:ethernet hwaddr FE:FF:FF:FF:FF:FF U
          P broadcast RUNNING noarp mtu:1500 metric:1 RX packets:5 errors:0 dropped:0 overruns:0 frame:0 TX packets:84859 errors:0 dropped:411 overruns:0 carrier:0 collisions:0 txqueuelen:500 RX bytes:140 (1 40.0 b) TX bytes:20301953 (19.3 MiB) vif3.0 Link encap:ethernet hwaddr FE:FF:FF:FF:FF:FF U P broadcast RUNNING noarp mtu:1500 metric:1 RX packets:5 errors:0 dropped:0 Overruns: 0 frame:0 TX packets:84337 errors:0 dropped:546 overruns:0 carrier:0 collisions:0 txqueuelen:500 RX bytes:140 (1 40.0 b) TX bytes:20174994 (19.2 MiB) virbr0 Link encap:ethernet hwaddr 00:00:00:00:00:00 inet 
          addr:192.168.122.1 bcast:192.168.122.255 mask:255.255.255.0 up broadcast RUNNING multicast MTU:1500 Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:0  (0.0 b) TX bytes:0 (0.0 b) xenbr1 Link encap:ethernet hwaddr FE:FF:FF:FF:FF:FF up broadcast RUNNING Noarp MTU
          : 1500 metric:1 RX packets:86405 errors:0 dropped:0 overruns:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:19498121 (18.5 MiB) TX bytes:0 (0.0 b) Total used FRee shared buffers Cached mem:3359744 464724 2895020 0 31096 246296-/+ buf fers/cache:187332 3172412 swap:5406712 0 5406712 filesystem Size Used Avail use% Mou   nted on/dev/mapper/volgroup00-logvol00 221G 25G 185G 12%//dev/sda1 99M   22M 73M 23%/boot tmpfs 1.8G 0 1.8G 0%/dev/shm none 1.8G 152K 1.8G 1%/var/lib/xenstored

The code is very simple, we are interested, you can experiment, hope to your study and work helpful.

This article from "You are a passer-by or fearless" blog, please be sure to keep this source http://world77.blog.51cto.com/414605/668107

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.