我目前有一个工作 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
我不确定如何解释这个错误——“套接字已关闭”让我认为 SSH 连接正在终止我尝试创建 VM 的连接。有没有人有任何指示?
更新
我正在尝试使用 pexpect 包装器,但无法让它与 un/pw 提示符交互。我正在通过 ssh 连接到远程计算机并运行 test.py 脚本来测试该过程,该脚本提示我输入用户名,然后将用户名保存在文本文件中。这是我的工厂文件:
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?