SunnyDB

사용중인 인터넷 공인IP를 e-mail로 전송할 VBScript 본문

Useful Tip

사용중인 인터넷 공인IP를 e-mail로 전송할 VBScript

Ksunny 2018. 9. 4. 11:48

사용중인 인터넷 공인IP를 e-mail로 전송할 VBScript


-저가 공유기의 잦은 고장으로 DDns이용 불편.

-통신사 공유기를 사용하려다 보니 무료 또는 유료 DDns를 이용해야 함(가입 및 DDns갱신절차 성가심)

-무료 DDns의 주기적인 갱신이 불편.

-IP확인만 되면 좋겠다는 생각에 머리 굴림.

-VBScript를 짜집기 하여 나만의 공인IP를 e-mail로 보내고 실시간 확인이 가능하게 함.


운영방법 :

  .공유기의 포트포워드 설정

  .원격제어 프로그램(Vnc Server) 설치

  .공인IP를 이메일에서 확인

  .원격제어(Vnc Viewer - 공인IP:포트번호(112.131.120.125:25111) 진행

  

아래 Script를 첨부파일 다운로드 또는 메모장에 작성하여 사용한다.

IpAddress_to Gmail.vbs

1.메모장을 연다.

2.Script 드래그 복사 후 붙여넣기, 본인 Gmail계정에 맞게 수정 

3.파일명 지정(IpAddress_to_Gmail.vbs) 후 저장

4.더블클릭 및 실행(컴퓨터 켜질때마다 실행 하게 하거나...예약된작업 또는 작업스케줄러에 등록 사용)


Google계정 - 로그인 및 보안 - 계정 액세스 권한을 가진 앱 -

    보안 수준이 낮은 앱 허용: 사용 으로 설정해주어야 VBScript를 이용한 메일발송이 가능하다.

 



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' 고정 IP 확인(우선 내부 IP)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' GMAIL 계정 필수'

' 사용방법

' monitorName = "Launcher.exe" ' 감시할 대상의 프로세스 이름 / 작업관리자에 프로스세 목록중 감시대상 프로세스를 작성!

' from = "ID@gmail.com" ' 보내는 사람 메일

' strTo = "ID@mail.com" ' 받는 사람 메일

' subject = "ICARUS MONITOR RESULT" ' 메일 제목

' body = "Auto mail... " ' 메일 내용

' sslUser = "Gmail ID" ' 메일 계정

' sslPassword = "Gmail PASS" ' 메일 패스워드

' tickTime = 1 ' 프로세스 감시 timer ex) 1분 

' 종료 방법

' 작업관리자 프로세스 목록중 wscript.exe 를 선택 후 프로세스 종료하기 누르면 끝.

' 추후 피드백 받아 프로세스 종료 및 PC 종료 적용 예정.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim WshShell, oExec, objMessage


EmailSubject = "Sending Email by CDO"

EmailBody    = "This is the body of a message sent via" & vbCRLF & _

               "a CDO.Message object using SMTP authentication ,with port 465."


Const EmailFrom = "aaaaafrom@gmail.com"

Const EmailFromName = "ipSendMan"

Const EmailTo = "bbbbbto@gmail.com"

Const SMTPServer = "smtp.gmail.com"        '변경금지

Const SMTPLogon = "aaaaafrom@gmail.com"

Const SMTPPassword = "aaaaafrompassword"

Const SMTPSSL = True                       '변경금지

Const SMTPPort = 465                       '변경금지


Const cdoSendUsingPickup = 1    'Send message using local SMTP service pickup directory.

Const cdoSendUsingPort = 2  'Send the message using SMTP over TCP/IP networking.


Const cdoAnonymous = 0  ' No authentication

Const cdoBasic = 1  ' BASIC clear text authentication

Const cdoNTLM = 2   ' NTLM, Microsoft proprietary authentication

tickTime = 60 ' 60분을 의미(1시간단위로 체크하기 위함)


Dim oDate

oDate = date


' First, create the message


url = "http://jsgetip.appspot.com"     '자바스크립트 공인IP조회 제공사이트

Set req = CreateObject("Msxml2.XMLHttp.6.0")

req.open "GET", url, False

req.send


PubIP1st = REPLACE (req.responseText, "function ip(){return""", "")

PubIP2nd = REPLACE (PubIP1st, """};", "")

PcName = CreateObject("WScript.Network").ComputerName

'WScript.Echo EmailSubject

EmailSubject = oDate & " " & PcName & " IP : " & PubIP2nd             'Email 제목

EmailBody    = oDate & " " & PcName & " IP : " & PubIP2nd & "입니다!"  'Email 내용


Dim filesys, filetxt, getname, path

fileName = "C:\ipCheck\ipCheck.txt" ' 데이터 파일 이름입니다.!! 

Set fso = CreateObject("scripting.FileSystemObject") ' 파일오브젝트 설정 


FolderName = fso.GetParentFolderName(fileName) ' 폴더만 빼냄니다. 

if(NOT fso.FolderExists(FolderName)) then '만약 폴더가 없으면 

fso.CreateFolder(FolderName) '폴더를 만들어야죠 

end if 


Dim strLine1, strLine2, strLine3

if fso.fileExists(fileName) then '파일이 있으면 

Set ts = fso.openTextFile(fileName,1) '파일을 읽기전용으로 열고 

strLine1 = ts.ReadLine '첫번째라인 읽음 

' strLine2 = ts.ReadLine '두번째라인 읽고

' strLine3 = ts.ReadLine '세번째라인 읽고 

' msgbox strLine1 & " : " & strLine2 & " : " & strLine3 


    If strLine1 <> PubIP2nd Then '기존IP와 갱신된IP가 다르면                         

       'msgbox strLine1 & " : " & PubIP2nd

       Set filetxt = fso.CreateTextFile("C:\ipCheck\ipCheck.txt", True) '텍스트 파일 생성

       filetxt.WriteLine(PubIP2nd) '텍스트 파일에 내용 입력

       filetxt.Close


       Callmail                  '기존IP와 갱신된IP가 다를때만 e-mail발송                        

End If

else '파일이 없으면 

' Set ts = fso.OpenTextFile(fileName,2,True) '쓰기전용으로 열고 

' ts.WriteLine "0" ' 첫라인 0으로 쓰고.... 

' ts.WriteLine "0" 

' ts.WriteLine "0" 

' msgbox "ipCheck.txt 파일 생성!"


Set filetxt = fso.CreateTextFile("C:\ipCheck\ipCheck.txt", True) '텍스트 파일 생성

    filetxt.WriteLine(PubIP2nd) '텍스트 파일에 내용 입력

    filetxt.Close

    Callmail                    '파일이 없어서 새로 만들고 e-mail발송                        

end if 


'파일생성확인 Msgbox를 띄우고자 함

'path = fso.GetAbsolutePathName("C:\ipCheck\ipCheck.txt") '절대 경로를 가져온다

'getname = fso.GetFileName(path) '파일명을 가져온다


'If fso.FileExists(path) Then

'   'msgbox ("Your file, '" & getname & "', has been created.")

'End If


'Callmail                  '기존IP와 갱신된IP 다름 여부와 상관없이 e-mail발송하고자 할때 주석 풀고 사용


Sub CallMail


Set objMessage = CreateObject("CDO.Message")

objMessage.Subject = EmailSubject

objMessage.From = """" & EmailFromName & """ <" & EmailFrom & ">"

objMessage.To = EmailTo

objMessage.TextBody = EmailBody


' Second, configure the server

objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPLogon


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SMTPSSL


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60


objMessage.Configuration.Fields.Update

'Now send the message!   e-mail발송 후 Msgbox 알림

On Error Resume Next

objMessage.Send


If Err.Number <> 0 Then

    MsgBox Err.Description,16,"Error Sending Mail"

Else 

    'MsgBox "Mail was successfully sent !",64,"Information"

End If


End Sub

Comments