1. SSH client generates key
1 $ ssh-keygen -t rsa -b 4096
2 Generating public/private rsa key pair.
3 Enter file in which to save the key (/Users/(username)/.ssh/id_rsa): vagrantid_rsa
4 Enter passphrase (empty for no passphrase):
5 Enter same passphrase again:
6 Your identification has been saved in vagrantid_rsa.
7 Your public key has been saved in vagrantid_rsa.pub.
8 The key fingerprint is:
9 ##...
10 $
2. Public key padding
Description: Https://teamtreehouse.com/community/vagrant-the-host-path-of-the-shared-folder-is-missing
When using vagrant, in the virtual machine. SSH folder path: (Linux)/users/yourname/path, (vagrant)/home/yourname/path
or using
[[email protected] /]# find / -name .ssh
/home/vagrant/.ssh
[[email protected]-centos65 /]# cd /home/vagrant/.ssh
Find the. SSH folder location
# client
$SCP./vagrantid_rsa.pub [email protected]192.168.33.10:~/.ssh/
[email protected] 192.168.33.10 's password:
Vagrantid_rsa.pub 100% 758 0.7kb /s 00:00
# used as the vm for the SSH server
[[email protected] - centos65. SSH] # ls
Authorized_keys vagrantid_rsa. Pub
After copying the public key to the SSH server:
[Email protected]. SSH cat vagrantid_rsa.pub >> Authorized_keys
3. Getting started: Hello, Fabric
$vim fabfile.py # creates a file, named fabfile.py, and defines a hello function in the file
$fab hello!
Hello world!
Done.
Fabric website Getting Started Tutorial link: http://docs.fabfile.org/en/1.10/tutorial.html
4. Call the fabric's API for remote login
Reference:
-
- Http://docs.fabfile.org/en/1.8/usage/fab.html#per-task-arguments
- Http://docs.fabfile.org/en/1.8/usage/execution.html#host-lists
#尝试 1:
fabfile.py:
1 from fabric.api import run, env
2 env.hosts = [‘192.168.33.10‘]# as host1
3
4 def taskA():
5 run(‘ls‘)
6 def taskB():
7 run(‘whoami‘)
The Terminal fab command performs 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):
If the user name is not the same (not specified in env.hosts) such a way can not login to specific users.
Modify:
#尝试 2
1 # fabfile. Py
2 from fabric. API import run, env
3 env.hosts = [' [email protected] ']# multiple hosts can be specified here. Multiple host passwords need to be specified separately, not good
4 env. Passwords = {
5 '[email protected]' : "vagrant",
6}
7
8 def taskA () :
9 the run (" ls ")
10 def taskB () :
11 the run (' whoami ')
Terminal Execution Fab command
$ fab taskA taskB
[[email protected]192.168.33.10] Executing task ‘taskA‘ [[email protected]192.168.33.10] run: ls
[[email protected]192.168.33.10] Login password for ‘vagrant‘:
Sorry, you can‘t enter an empty password. Please try again. [[email protected]] Login password for ‘vagrant‘:
[[email protected]192.168.33.10] out: httpd-2.4.17 mod_wsgi-4.4.21 pi Python-2.7.10.tar
[[email protected]192.168.33.10] out: httpd-2.4.17.tar.gz mod_wsgi-4.4.21.tar.gz Python-2.7.10 [[email protected]192.168.33.10] out:
[[email protected]192.168.33.10] Executing task ‘taskB‘ [[email protected]192.168.33.10] run: whoami
[[email protected]192.168.33.10] out: vagrant
[[email protected]192.168.33.10] out:
Done.
Disconnecting from [email protected] done.
Still need to enter the password manually
#try 3: Set in the fab command parameter
$ fab taskA -i /Users/(username)/.ssh/vagrantid_rsa -H [email protected]192.168.33.10:22
[[email protected]192.168.33.10] Executing task ‘taskA‘
[[email protected]192.168.33.10] run: ls
[[email protected]192.168.33.10] out: httpd-2.4.17 mod_wsgi-4.4.21 pi Python-2.7.10.tar
[[email protected]192.168.33.10] out: httpd-2.4.17.tar.gz mod_wsgi-4.4.21.tar.gz Python-2.7.10
[[email protected]192.168.33.10] out:
Done.
Disconnecting from [email protected]192.168.33.10... done.
OK, you do not need to enter the password manually (the client's private key exists /users/(username)/.ssh/vagrantid_rsa)
#尝试 4
Write the key storage path in the fabfile.py, and according to the Http://docs.fabfile.org/en/1.4.0/usage/execution.html#ssh-config and HTTP/ Stackoverflow.com/questions/5327465/using-an-ssh-keyfile-with-fabric recommended, set up SSH login.
Reference:
How does I configure SSH on OS X?Http://docs.paramiko.org/en/latest/api/client.html#paramiko.client.SSHClient.connectHttp://segmentfault.com/a/1190000000497630#articleHeader0
Final code:
1 from fabric.api import run, env
2 import os
3
4 if env.ssh_config_path and 5 os.path.isfile(os.path.expanduser(env.ssh_config_path)):
6 env.use_ssh_config = True
7
8 env.key_filename = [‘/Users/***/.ssh/vagrantid_rsa‘]
9 env.hosts = [‘[email protected]‘]# as host1
10 #not using cache
11
12 def taskA():
13 run(‘ls‘)
14 def taskB():
15 run(‘whoami‘)
END.
Resources:
Build a cross-platform development environment with VagrantAutomation Operations Tool fabric-password management (Env.password and SSH key)
[Python Fabric] [SSH] Mac OS X 10.9 + vagrant Virtual Environment simple experiment using Python fabric for SSH remote login