forked from josenk/esxi-vm-create
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esxi-scp
executable file
·129 lines (106 loc) · 3.75 KB
/
esxi-scp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
import argparse # Argument parser
import os.path # To check if file exists
import re # For regex
import paramiko # For remote ssh
import yaml
from scp import SCPClient
#
# System wide defaults
#
ConfigData = dict(
# Your logfile
LOG=os.path.expanduser("~") + "/esxi-vm.log",
# Enable/Disable dryrun by default
isDryRun=False,
# Enable/Disable Verbose output by default
isVerbose=False,
# Enable/Disable exit summary by default
isSummary=False,
# ESXi host/IP, root login & password
HOST="esxi",
USER="root",
PASSWORD="",
# Default number of vCPU's, GB Mem, & GB boot disk
FILENAME="",
LOCATION=""
)
ConfigDataFileLocation = os.path.expanduser("~") + "/.esxi-vm.yml"
if os.path.exists(ConfigDataFileLocation):
FromFileConfigData = yaml.safe_load(open(ConfigDataFileLocation))
ConfigData.update(FromFileConfigData)
NAME = ""
LOG = ConfigData['LOG']
isDryRun = ConfigData['isDryRun']
isVerbose = ConfigData['isVerbose']
isSummary = ConfigData['isSummary']
HOST = ConfigData['HOST']
USER = ConfigData['USER']
PASSWORD = ConfigData['PASSWORD']
FILENAME = ConfigData['FILENAME']
LOCATION = ConfigData['LOCATION']
ErrorMessages = ""
MAC = ""
GeneratedMAC = ""
ISOfound = False
CheckHasErrors = False
LeastUsedDS = ""
DSPATH = ""
DSSTORE = ""
FullPathExists = False
#
# Process Arguments
#
parser = argparse.ArgumentParser(description='ESXi SCP utility.')
parser.add_argument('-d', '--dry', dest='isDryRunarg', action='store_true',
help="Enable Dry Run mode (" + str(isDryRun) + ")")
parser.add_argument("-H", "--Host", dest='HOST', type=str, help="ESXi Host/IP (" + str(HOST) + ")")
parser.add_argument("-U", "--User", dest='USER', type=str, help="ESXi Host username (" + str(USER) + ")")
parser.add_argument("-P", "--Password", dest='PASSWORD', type=str, help="ESXi Host password (*****)")
parser.add_argument("-n", "--fileName", dest='FILENAME', type=str, help="File name")
parser.add_argument("-l", "--location", dest='LOCATION', type=str, help="File Location")
parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true',
help="Enable Verbose mode (" + str(isVerbose) + ")")
parser.add_argument('--summary', dest='isSummaryarg', action='store_true',
help="Display Summary (" + str(isSummary) + ")")
args = parser.parse_args()
if args.isDryRunarg:
isDryRun = True
if args.isVerbosearg:
isVerbose = True
if args.isSummaryarg:
isSummary = True
if args.HOST:
HOST = args.HOST
if args.USER:
USER = args.USER
if args.PASSWORD:
PASSWORD = args.PASSWORD
if args.FILENAME:
FILENAME = args.FILENAME
if args.LOCATION:
LOCATION = args.LOCATION
print ("Preparing to transfer file -> " + FILENAME)
splitLine = re.split('[/\b]', FILENAME)
Result = "Fail"
while Result == "Fail":
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(HOST, username=USER, password=PASSWORD, banner_timeout=100, timeout=100, auth_timeout=100)
print ("Removing file -> " + splitLine[len(splitLine) - 1])
(stdin, stdout, stderr) = ssh.exec_command("rm -rf " + LOCATION + "/" + splitLine[len(splitLine) - 1])
print ("Transferring -> ")
with SCPClient(ssh.get_transport()) as scp:
scp.put(FILENAME, LOCATION)
print ("Evaluating transfer status -> ")
(stdin, stdout, stderr) = ssh.exec_command("ls " + LOCATION + "/" + splitLine[len(splitLine) - 1])
type(stdin)
if stderr.readlines():
print ("Transfer failed, retrying")
Result = "Fail"
else:
print ("Transfer succeeded")
Result = "Pass"
except:
Result = "Fail"