使用python在自动化中创建和登录Linux虚拟机

时间:2013-11-15 04:10:08

标签: python linux ssh virtual-machine paramiko

我目前有一个工作的python脚本,可以通过SSH连接到远程Linux机器并在该机器上执行命令。我正在使用paramiko来处理ssh连接。以下是执行hostname -s命令的代码:

blade = '192.168.1.15'
username='root'
password=''
# now, connect 
try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())
    print '*** Connecting...'
    client.connect(blade, 22, username, password)
# print hostname for verification
    stdin, stdout, stderr  = client.exec_command('hostname --short')
    print stdout.readlines()
except Exception, e:
    print '*** Caught exception: %s: %s' % (e.__class__, e)
    traceback.print_exc()
    try:
        client.close()
    except:
        pass
    sys.exit(1)

这很好用,但我实际上要做的更复杂。我真正想做的是SSH进入同一个Linux机器,就像我上面所做的那样,然后在其上创建一个临时虚拟机,并在该虚拟机上执行命令。这是我的(非工作)尝试:

blade='192.168.1.15'
username='root'
password=''
# now, connect 
try:
   # client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())
    print '*** Connecting...'
    client.connect(blade, 22, username, password)
   # create VM, log in, and print hostname for verification
    stdin, stdout, stderr = client.exec_command('sudo kvm -m 1024 -drive file=/var/lib/libvirt/images/oa4-vm$
    time.sleep(60) #delay to allow VM to initialize
    stdin.write(username + '\n') #log into VM
    stdin.write(password + '\n') #log into VM
    stdin, stdout, stderr  = client.exec_command('hostname --short')
    print stdout.readlines()
except Exception, e:
    print '*** Caught exception: %s: %s' % (e.__class__, e)
    traceback.print_exc()
    try:
        client.close()
    except:
        pass
    sys.exit(1)

当我运行时,我得到以下内容:

joe@computer:~$ python automata.py
*** Connecting...
/home/joe/.local/lib/python2.7/site-packages/paramiko/client.py:95: UserWarning: Unknown ssh-rsa host key for 192.168.1.15: 25f6a84613a635f6bcb5cceae2c2b435
  (key.get_name(), hostname, hexlify(key.get_fingerprint())))
*** Caught exception: <class 'socket.error'>: Socket is closed
Traceback (most recent call last):
  File "automata.py", line 32, in function1
stdin.write(username + '\n') #log into VM
  File "/home/joe/.local/lib/python2.7/site-packages/paramiko/file.py", line 314, in write
self._write_all(data)
  File "/home/joe/.local/lib/python2.7/site-packages/paramiko/file.py", line 439, in _write_all
    count = self._write(data)
  File "/home/joe/.local/lib/python2.7/site-packages/paramiko/channel.py", line 1263, in _write
    self.channel.sendall(data)
  File "/home/joe/.local/lib/python2.7/site-packages/paramiko/channel.py", line 796, in sendall
    raise socket.error('Socket is closed')
error: Socket is closed

我不确定如何解释这个错误 - “socket is closed”让我觉得SSH连接正在终止我尝试创建VM。有没有人有任何指针?

更新

我正在尝试使用pexpect包装器并且无法使其与un / pw提示进行交互。我正在通过ssh进入远程计算机并运行test.py脚本来测试进程,该脚本会提示我输入用户名,然后将用户名保存在文本文件中。这是我的fab文件:

env.hosts = ['hostname']
env.user = 'userame'
env.password = 'password'


def vm_create():
        run("python test.py")

远程计算机上的test.py内容为:

#! /usr/bin/env python

uname = raw_input("Enter Username: ")

f = open('output.txt','w')
f.write(uname + "\n")
f.close

因此,我可以在本地计算机上执行“fab vm_create”并成功建立SSH连接并提示我输入test.py定义的用户名。但是,如果我使用pexpect包装器在本地机器上执行第三个python文件,如下所示:

import pexpect
child = pexpect.spawn('fab vm_create')
child.expect ('Enter Username: ')
child.sendline ('password')

似乎没有发生任何事情。我没有错误,并且在远程计算机上没有创建output.txt。我错误地使用了pexpect吗?

2 个答案:

答案 0 :(得分:2)

尽管我喜欢paramiko,但这可能更适合使用Fabric

以下是fabfile.py示例:

from fabric.api import run
from fabric.api import sudo
from fabric.api import env

env.user = 'root'
env.password = ''
env.host = ='192.168.1.15'

def vm_up():
  sudo("kvm -m 1024 -drive file=/var/lib/libvirt/images/oa4-vm$...")
  run("hostname --short")

然后运行它,使用

$ fab vm_up

如果您没有在fabfile本身中设置主机和密码(正确地说是这样),那么您可以在命令行中设置它们:

$ fab -H 192.168.1.15 -p PASSWORD vm_up

但是,您的kvm行仍然期待输入。要发送输入(并等待预期的提示),请编写另一个使用pexpect来调用fab的脚本:

child = pexpect.spawn('fab vm_up')
child.expect('username:') # Put this in the format you're expecting
child.send('root')

答案 1 :(得分:0)

使用面料http://docs.fabfile.org/en/1.8/

  

Fabric是一个Python(2.5或更高版本)库和命令行工具,用于简化SSH在应用程序部署或系统管理任务中的使用

from fabric.api import run

def host_name():
    run('hostname -s')