Skip to content

Instantly share code, notes, and snippets.

@ak2766
Forked from rgerganov/vmrc_attach.sh
Created October 10, 2021 04:38
Show Gist options
  • Save ak2766/81b0d3e2fcaee403e9c3ab88658bf2d0 to your computer and use it in GitHub Desktop.
Save ak2766/81b0d3e2fcaee403e9c3ab88658bf2d0 to your computer and use it in GitHub Desktop.
Virtual USB Drive
#!/usr/bin/env sh
if [ "$#" -ne 5 ]; then
echo "Usage: $0 <host> <user> <pwd> <vm-id> <dev-id>"
exit 1
fi
echo "attach $1 $2 $3 $4 $5" | nc localhost 9999
#!/usr/bin/env sh
echo 'detach' | nc localhost 9999
#!/usr/bin/env python
import select
import socket
import subprocess
import SocketServer
def recvall(sock):
resp = ''
# the first response may take several seconds
read_ready = select.select([sock], [], [], 5)[0]
if not read_ready:
return resp
resp += sock.recv(1024)
while True:
read_ready = select.select([sock], [], [], 1)[0]
if not read_ready:
return resp
resp += sock.recv(1024)
class VmrcHandler(SocketServer.BaseRequestHandler):
def lsdev(self):
self.server.dev_mgr_sock.sendall('getPhysicalDevices 4 unused\n')
resp = recvall(self.server.dev_mgr_sock)
self.request.sendall(resp)
def attach(self, args):
host, user, pwd, vm_id, dev_id = args
# TODO remove hardcoded thumbprint
thumbprint = '8D:DE:D2:89:1B:22:A0:26:A1:FA:6B:90:D6:5C:8F:C1:8B:E1:92:A0'
cmd = "connect {} {} true {} {} {} 0 \n".format(host, thumbprint, user, pwd, vm_id, dev_id)
self.server.dev_mgr_sock.sendall(cmd)
resp = recvall(self.server.dev_mgr_sock)
self.request.sendall(resp)
cmd = "attachDevice {} 2\n".format(dev_id)
self.server.dev_mgr_sock.sendall(cmd)
resp = recvall(self.server.dev_mgr_sock)
self.request.sendall(resp)
def detach(self, args):
cmd = "disconnect\n"
self.server.dev_mgr_sock.sendall(cmd)
resp = recvall(self.server.dev_mgr_sock)
self.request.sendall(resp)
def handle(self):
req = self.request.recv(1024)
args = req.strip().split(' ')
if args[0] == 'lsdev':
self.lsdev()
elif args[0] == 'attach':
self.attach(args[1:])
elif args[0] == 'detach':
self.detach(args[1:])
elif args[0] == 'quit':
self.server.shutdown()
else:
self.request.sendall('Unknown command {}\n'.format(args[0]))
if __name__ == '__main__':
fd = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
p = subprocess.Popen(['/usr/lib/vmware-vmrc/5.5/bin/vmware-deviceMgr',
'--ipc', str(fd[1].fileno()),
'--modes', '4',
'--message-mode', '2',
'--features', '0'])
dev_mgr_sock = fd[0]
print(recvall(dev_mgr_sock))
host, port = 'localhost', 9999
server = SocketServer.ThreadingTCPServer((host, port), VmrcHandler)
server.dev_mgr_sock = dev_mgr_sock
print('Starting vmrc_server on {}:{}'.format(host, port))
try:
server.serve_forever()
finally:
p.kill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment