-
Notifications
You must be signed in to change notification settings - Fork 15
/
sample.lua
57 lines (53 loc) · 1.53 KB
/
sample.lua
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
if #arg < 2 then
print("expect.exe sample.lua USERNAME@DOMAIN PASSWD")
os.exit(0)
end
local account = arg[1]
local password = arg[2]
local sshexe = os.getenv("windir") .. "\\System32\\OpenSSH\\ssh.exe"
local sshpid = spawn(sshexe,"-p","22",account)
if not sshpid then
print("ssh.exe is not found")
os.exit(1)
end
timeout = 10
capturelines = 3 -- default is 2
local promptkeyword = "$"
while true do
local rc = expect(
"password:",
"Are you sure you want to continue connecting (yes/no/[fingerprint])?",
"Could not resolve hostname")
if rc == 0 then
sendln(password)
sleep(1)
rc = expect(promptkeyword,"Permission denied, please try again")
if rc == 0 then
sleep(1)
sendln("exit")
sleep(1)
echo()
echo("-- This is the sample script")
echo("-- Write your code to do in your server instead of sendln(\"exit\")")
else
print()
echo(string.format(
"-- The expected prompt keyword(%s) was not found.",
promptkeyword))
-- kill(sshpid)
end
break
elseif rc == 1 then
sendln("yes")
print() -- move cursor down not to capture same keyword
elseif rc == -2 then
echo("TIMEOUT")
break
elseif rc == -1 then
echo("ERROR")
break
else
echo(string.format("Error keyword found \"%s\". Exit",_MATCH))
break
end
end