1. ssh客户端生成key

 $ ssh-keygen -t rsa -b
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa): vagrantid_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in vagrantid_rsa.
Your public key has been saved in vagrantid_rsa.pub.
The key fingerprint is:
##...
$

2. 公钥填充

说明:https://teamtreehouse.com/community/vagrant-the-host-path-of-the-shared-folder-is-missing

使用vagrant的时候,虚拟机中.ssh文件夹路径:(Linux)/Users/yourname/path -> (vagrant)/home/yourname/path

或者采用

[root@vagrant-centos65 /]# find / -name .ssh
/home/vagrant/.ssh
[root@vagrant-centos65 /]# cd /home/vagrant/.ssh

找到.ssh文件夹位置

#客户机
$ scp ./vagrantid_rsa.pub vagrant@192.168.33.10:~/.ssh/
vagrant@192.168.33.10's password:
vagrantid_rsa.pub % .7KB/s : #用作ssh服务器的vm
[root@vagrant-centos65 .ssh]# ls
authorized_keys vagrantid_rsa.pub

拷贝公钥到ssh服务器之后:

[root@vagrant-centos65 .ssh]# cat vagrantid_rsa.pub >> authorized_keys

3. 上手:Hello, Fabric

$ vim fabfile.py #创建文件,命名为fabfile.py,在文件中定义一个hello函数
$ fab hello
Hello world! Done.

Fabric官网入门教程链接:http://docs.fabfile.org/en/1.10/tutorial.html

4. 调用Fabric的API进行远程登录

参考:

#尝试 1:

fabfile.py:

 from fabric.api import run, env
env.hosts = ['192.168.33.10']# as host1 def taskA():
run('ls')
def taskB():
run('whoami')

终端fab命令执行tasks:

$ fab taskA taskB
[192.168.33.10] Executing task 'taskA'
[192.168.33.10] run: ls
[192.168.33.10] Login password for (username):

如果用户名不相同(在env.hosts里没有指定)这样的方式登陆不了特定用户。

修改:

#尝试 2

 #fabfile.py
from fabric.api import run, env
env.hosts = ['vagrant@192.168.33.10']# 这里可以指定多个主机。多个主机密码需要分别指定,不好
env.passwords = {
'vagrant@192.168.33.10': 'vagrant',
} def taskA():
run('ls')
def taskB():
run('whoami')

终端执行fab命令

$ fab taskA taskB
[vagrant@192.168.33.10] Executing task 'taskA'
[vagrant@192.168.33.10] run: ls
[vagrant@192.168.33.10] Login password for 'vagrant':
Sorry, you can't enter an empty password. Please try again.
[vagrant@192.168.33.10] Login password for 'vagrant':
[vagrant@192.168.33.10] out: httpd-2.4.17 mod_wsgi-4.4.21 pi Python-2.7.10.tar
[vagrant@192.168.33.10] out: httpd-2.4.17.tar.gz mod_wsgi-4.4.21.tar.gz Python-2.7.10
[vagrant@192.168.33.10] out: [vagrant@192.168.33.10] Executing task 'taskB'
[vagrant@192.168.33.10] run: whoami
[vagrant@192.168.33.10] out: vagrant
[vagrant@192.168.33.10] out: Done.
Disconnecting from vagrant@192.168.33.10... done.

还是需要手动输入密码

#尝试 3:在fab命令参数里设置

$ fab taskA -i /Users/(username)/.ssh/vagrantid_rsa -H vagrant@192.168.33.10:
[vagrant@192.168.33.10] Executing task 'taskA'
[vagrant@192.168.33.10] run: ls
[vagrant@192.168.33.10] out: httpd-2.4. mod_wsgi-4.4. pi Python-2.7..tar
[vagrant@192.168.33.10] out: httpd-2.4..tar.gz mod_wsgi-4.4..tar.gz Python-2.7.
[vagrant@192.168.33.10] out: Done.
Disconnecting from vagrant@192.168.33.10... done.

ok,不需要手动输入密码了(客户机的私钥存在/Users/(username)/.ssh/vagrantid_rsa里)

#尝试 4

在fabfile.py里写好密钥存放路径,并且根据http://docs.fabfile.org/en/1.4.0/usage/execution.html#ssh-confighttp://stackoverflow.com/questions/5327465/using-an-ssh-keyfile-with-fabric建议,设置好ssh登陆

参考:

How do I configure SSH on OS X?
http://docs.paramiko.org/en/latest/api/client.html#paramiko.client.SSHClient.connect
http://segmentfault.com/a/1190000000497630#articleHeader0

最终代码:

 from fabric.api import run, env
import os if env.ssh_config_path and \
os.path.isfile(os.path.expanduser(env.ssh_config_path)):
env.use_ssh_config = True env.key_filename = ['/Users/***/.ssh/vagrantid_rsa']
env.hosts = ['vagrant@192.168.33.10']# as host1
#not using cache def taskA():
run('ls')
def taskB():
run('whoami')

END.

参考资料:

使用 Vagrant 打造跨平台开发环境
自动化运维工具Fabric - 密码管理(env.password and ssh key)
 
 

[Python Fabric] [SSH] Mac OS X 10.9 + Vagrant虚拟环境使用Python Fabric进行SSH远程登录的简单实验的更多相关文章

  1. VMware 11完全安装Mac OS X 10.10

    本文已迁移到我的个人网站 http://www.wshunli.com 文章地址: http://www.wshunli.com/2016/03/17/VMware-12安装Mac-OS-X-10-1 ...

  2. Python:版本升级(Mac OS X)

    Mac OS X 10.8及以后的版本都预装了Python 2.7,但是在Mac上(Unix-like OS)上修改Python的版本并不如Windows方便.这篇文章的目标是要将Mac自带的Pyth ...

  3. 虚拟机安装苹果系统 VMware 12安装Mac OS X 10.10

    工具/原料 VMware Workstation Pro 12 (这个可以自己下载,并激活,你懂得) Unlocker 207 (链接:http://pan.baidu.com/s/1i43obDb ...

  4. 在 Mac OS X 10.9 搭建 Python3 科学计算环境

    安装 Homebrew 使用 Homebrew 管理 Python 版本.在 Terminal/iTerm2 输入: $ ruby -e "$(curl -fsSL https://raw. ...

  5. VMware Workstation 11 安装MAC OS X 10.10 Yosemite(14B25)图解 2015-01-13 12:26:01|

    VMware Workstation 11 安装MAC OS X 10.10 Yosemite(14B25)图解 2015-01-13 12:26:01|  分类: 网络互联 |  标签:10.10  ...

  6. 【转】windows环境下安装win8.1+Mac OS X 10.10双系统教程

    先要感谢远景论坛里的各位大神们的帖子  没有他们的分享我也不能顺利的装上Mac OS X 10.10! 写这篇随笔主要是为了防止自己遗忘,同时给大家分享下我的经验. 本教程适用于BIOS+MBR分区的 ...

  7. VMWare 安装 Mac OS X10.10 Yosemite

    OS X Yosemite 新功能特性 Mac OS X10.10 GM3|OS X 10.10 Yosemite 正式版下载 如何在虚拟机中安装苹果mac系统图示说明 vm10虚拟机安装Mac OS ...

  8. [MAC]用beamoff给VMware的Mac OS X 10.10.x加速

    MAC OS X 10.10.x Yosemite在VMWare中实在是太慢了,卡出翔!好在高人多,请装beamoff!详见:https://github.com/JasF/beamoff.git C ...

  9. windows环境下安装win8.1+Mac OS X 10.10双系统教程

    首先要感谢远景论坛里的各位大神们的帖子  没有他们的分享我也不能顺利的装上Mac OS X 10.10! 写这篇随笔主要是为了防止自己遗忘,同时给大家分享下我的经验. 本教程适用于BIOS+MBR分区 ...

随机推荐

  1. 遇到 java.io.EOFException 异常的解决办法

    可以试着clean项目后再启动!原因未明

  2. SPSS数据分析—典型相关分析

    我们已经知道,两个随机变量间的相关关系可以用简单相关系数表示,一个随机变量和多个随机变量的相关关系可以用复相关系数表示,而如果需要研究多个随机变量和多个随机变量间的相关关系,则需要使用典型相关分析. ...

  3. css样式大全

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  4. Convert Sorted List to Binary Search Tree [LeetCode]

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  5. Flatten Binary Tree to Linked List [LeetCode]

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  6. Socket通信 简单实现私聊、群聊(dos命令下)

    很久以前的一个Demo,这里服务器只做转发功能,根据ID地址和端口号来标识身份,群聊和私聊只是简单实现, 服务器代码如下: import java.util.*; import java.io.*; ...

  7. flex 添加svn插件

    http://blog.csdn.net/gangan1345/article/details/7926848

  8. Spring Boot中的注解

    文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...

  9. SetProcessAffinityMask的问题

    BOOL WINAPI SetProcessAffinityMask( _In_ HANDLE hProcess, _In_ DWORD_PTR dwProcessAffinityMask );//M ...

  10. HTML 中级

    abbr(表示它所包含的文本是一个更长的单词或短语的缩写形式): <p>This web site is about <abbr title="HyperText Mark ...