Linux remotely executes shell commands on Python using SSH

Source: Internet
Author: User
Tags stdin

Single Sign-on:

import paramikossh = paramiko.SSHClient()key = paramiko.AutoAddPolicy()ssh.set_missing_host_key_policy(key)ssh.connect(‘127.0.0.1‘, 22, ‘user‘, ‘passwd‘ ,timeout=5)stdin, stdout, stderr = ssh.exec_command(‘ls -l‘)for i in stdout.readlines():    print(i)

For I in Stdout.readlines ():
Print (i)
These two paragraphs are output screen commands.

If you have added a secret key login, leave passwd blank.
Here are more than one server login:

#!/usr/bin/python# -*- coding: utf-8 -*-import time,shutil,osimport paramikoappname = "root"ssh = paramiko.SSHClient()key = paramiko.AutoAddPolicy()ssh.set_missing_host_key_policy(key)uptime = time.strftime("%Y%m%d")warpath="/var/tmp/cslc/aw/update/"+uptime+"/aw.war"def update_all_aw():    for line in open("awip.conf"):        print line        ssh.connect(line, 22, appname, ‘‘ ,timeout=5)        stdin, stdout, stderr = ssh.exec_command(‘python /var/tmp/awpy/createcopy.py‘)        for i in stdout.readlines():            print(i)        print("+++++++下一个++++++")    return

awip.conf file prevents multiple IP addresses.

Reference:
PYTHONSSH Remote Execute shell command
Python calls the Paramiko module for remote management of multiple servers
Python implements SSH remote login, and executes commands!
Python implements SSH remote login, and executes commands!

Linux remotely executes shell commands on Python using SSH

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.