????linux???????????????У????????????????н????????????????????????????£?ssh???????????????????????????????????????У??????shell?????????????????????????

?????????????????????????????

????1?????????????ssh???ι??

????2????python??????scp?????????????????????????????????

???????????ν??н????

????1?????????????ssh???ι?????????????????

?????????????????usr1@localhost??usr2@remote????????????????????????ι??????????3????

????1??????usr1@localhost??authentication key

[usr1@localhost~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/usr1/.ssh/id_rsa): [?????????]
Enter passphrase (empty for no passphrase): [??????????????ssh???ι????????????????????????ι??????ε??????????????????????]
Enter same passphrase again: [?????]
Your identification has been saved in /home/usr1/.ssh/id_rsa.
Your public key has been saved in /home/usr1/.ssh/id_rsa.pub.
The key fingerprint is:
b4:de:66:2c:c1:04:ad:7c:48:7c:f7:94:71:09:85:21 usr1@localhost

???????????????localhost??????~/.ssh???£????????id_rsa??id_rsa.pub?????????????????????????????public authentication key??

????????????????????????????-t rsa????????????RSA??????????????????????????man ssh-keygen???в???

????2?????????????id_rsa.pub?????????usr2@remote??~/.ssh/authorized_keys?????

[usr1@localhost~]$ cat ~/.ssh/id_rsa.pub | ssh usr2@remote 'cat >> .ssh/authorized_keys'
usr2@remote's password: [????????usr2@remote????????????]

???????????????usr2@remote??~/.ssh/authorized_keys??????????????usr1@localhost??public key?????????????β??

???????????????????????е??????У???usr2@remote??~/.ssh/authorized_keysд????????????>>??????????>????д???????????????remote????????????????????????????ι????

??????????????????????????????????ssh-copy-id?????????????????????????authorized_keys??????????÷???ο??????????????????ò?????ssh-copy-id???????

????3????????

?????????????????usr2@remote?????usr1@localhost???????ι???б????????usr1@localhost???usr2@remote???????????????

???????????????shell?????????????????

????С????????????ν?????????????ι?????????????????????????????д??.sh???????????????ι??????????client?????????????н???????????????????????

????2?????python??????scp???????????

????????pexpect module?????scp???????????????????pexpect??????????ο?python????????????????????

????pexpect???÷????????????????δ??????????У?????????????ɡ?

#!/bin/env python
#-*- encoding: utf-8 -*-
import pexpect

def remote_ssh(remote_ip?? user?? passwd?? cmd):
    try:
        remote_cmd = "ssh %s@%s '%s'"  % (user?? remote_ip?? cmd)
        try:
            child = pexpect.spawn(remote_cmd)
            reply = ['password: '?? 'continue connecting (yes/no)?']
            idx = child.expect(reply)
            if 0 == idx:
                child.sendline(passwd)
            elif 1 == idx:
                child.sendline('yes')
        except pexpect.EOF:
            child.close()
            return 0
        else:
            resp = child.read()
            child.expect(pexpect.EOF)
            child.close()
            print resp
            return 0
    except Exception?? e:
        print 'execute remote cmd failed?? remote_ip=%s?? user=%s?? cmd=%s?? errmsg=%s' % (remote_ip?? user?? cmd?? e)
        return -1

def main():
    ip = '127.0.0.1'
    user = 'test'
    passwd = 'test@passwd'
    cmd = 'df -h'
    remote_ssh(ip?? user?? passwd?? cmd)

if __name__  == '__main__':
    main()