File transfer through VM - ji-it/CloudTides GitHub Wiki
Installing
To install the tools, download with the command
git clone https://github.com/vmware/pyvmomi-community-samples
Then, after the tool is installed we can run a program in python to execute some simple scripts in the VM.
Executing a Program
In the test, the ls command is used. And the script, or program of this command is in /bin.
By running the code that executes program in vm the configuration is as follows:
-s (the address of database) -u (username) -p (password) -i (address of VM) -r (username of VM) -w (password of VM) -l (the script to run,with path) -f "(other arguments)"
After running the program,the python program will output a success/failure message. And we can find the output in the VM.
Upload and Download file with Commands
scp
is a helpful command that send file between VMs. And the usage is
scp file destination
For example: If a file, ~/1.txt
is to be uploaded to a VM at 10.11.16.140 as a file named 'test.txt' in directory '/home/ve450', and the username of that VM is ve450
scp ~/1.txt [email protected]:/home/ve450/text.txt
And if we want to download test.txt back in directory '~':
scp [email protected]:/home/ve450/1.txt ~
However, scp has a problem. That is, if the remote vm needs a password to login, it will prompt for the user to enter password. In our case, to control the process with code, we also need to use sshpass
.
We install sshpass
by using command sudo apt install sshpass
in Ubuntu linux.
To upload file, we can use command:
/usr/bin/sshpass -p password scp ~/1.txt [email protected]:/home/ve450
sshpass
can also read the password from the first line of a text file.
If the password is the first line of '2.txt',then we can access the file with
/usr/bin/sshpass -f ~/2.txt scp [email protected]:/home/ve450/1.txt ~/test.txt
Upload/download file with python
We can control the Upload/download process all with python scripts.
We write a script '~/a.py', and write a brief command like:
import os
os.system("/usr/bin/sshpass -f ~/2.txt scp ~/1.txt [email protected]:/home/ve450")
And we excecute executes program in vm with the configuration:
-s [database address] -u [vsphere username] -p [vsphere password] -i [address of vm] -r [usrname of VM] -w [user password of vm] -l /usr/bin/python3 -f "~/a.py"
And thus the upload/download command in 'a.py' will be executed.