Files
CyberPanel/plogical/test.py

77 lines
2.2 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import requests
import json
import pexpect
from CyberCPLogFileWriter import CyberCPLogFileWriter as logging
import time
from backupUtilities import backupUtilities
import signal
2017-10-24 19:16:36 +05:00
def verifyHostKey(IPAddress):
try:
backupUtilities.host_key_verification(IPAddress)
2017-10-24 19:16:36 +05:00
password = "hello"
2017-10-24 19:16:36 +05:00
expectation = []
2017-10-24 19:16:36 +05:00
expectation.append("continue connecting (yes/no)?")
expectation.append("password:")
2017-10-24 19:16:36 +05:00
setupSSHKeys = pexpect.spawn("ssh root@" + IPAddress)
2017-10-24 19:16:36 +05:00
index = setupSSHKeys.expect(expectation)
2017-10-24 19:16:36 +05:00
if index == 0:
setupSSHKeys.sendline("yes")
setupSSHKeys.expect("password:")
setupSSHKeys.sendline(password)
expectation = []
expectation.append("password:")
expectation.append(pexpect.EOF)
innerIndex = setupSSHKeys.expect(expectation)
if innerIndex == 0:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
elif innerIndex == 1:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
elif index == 1:
setupSSHKeys.expect("password:")
setupSSHKeys.sendline(password)
expectation = []
expectation.append("password:")
expectation.append(pexpect.EOF)
innerIndex = setupSSHKeys.expect(expectation)
if innerIndex == 0:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
elif innerIndex == 1:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
except pexpect.TIMEOUT, msg:
logging.writeToFile("Timeout [verifyHostKey]")
return [0,"Timeout [verifyHostKey]"]
except pexpect.EOF, msg:
logging.writeToFile("EOF [verifyHostKey]")
return [0,"EOF [verifyHostKey]"]
except BaseException, msg:
logging.writeToFile(str(msg) + " [verifyHostKey]")
return [0,str(msg)+" [verifyHostKey]"]
print verifyHostKey("23.95.216.56")