1

I used this script to login SAP session:

from subprocess import call
import win32com.client
import time
import os

GUIPath = 'C:/Program Files (x86)/SAP/FrontEnd/SAPgui/'
WinTitle = 'SAP'
Name = """PRD"""
SID = 'PRD'
InstanceNo = '01'

shell = win32com.client.Dispatch("WScript.Shell")
call(os.path.join(GUIPath, 'SAPgui.exe') + " " + Name + " " + InstanceNo)

however, it always return with the error:

hostname 'PRD' unknown
check you application server name

anyone knows how to fix this? thanks

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Tommy Thang
  • 63
  • 1
  • 7
  • As the message says, it should be the **host** name of the SAP system corresponding to the SAP system ID (code of 3 characters) that you define in SAP Logon or SAP Logon Pad. Ask the SAP administrator if needed or look at the details of the SAP system defined in your SAP Logon application. – Sandra Rossi Feb 01 '20 at 08:04
  • The thing is with the exact parameter, I can run it in Power Shell. however, in Python, it is not working.... – Tommy Thang Feb 01 '20 at 10:36
  • I just tried with PowerShell `&"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapgui.exe" S4G 22`, and I've got the same error as yours (hostname 'S4G' unknown...) If I use the host name (in my case `192.168.1.8`) instead of S4G, it works. – Sandra Rossi Feb 01 '20 at 13:58
  • I used this script in Power ISE: #-Set the path to the SAP GUI directory------------------------------- $SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\" #-Set the SAP system ID or the IP address----------------------------- $Name = """PRD""" $SID = “PRD” #-Set the instance number of the SAP system--------------------------- $InstanceNo = "01" #-Starts the SAP GUI-------------------------------------------------- $SAPGUI = $SAPGUIPath + "sapgui.exe" & $SAPGUI $Name $InstanceNo it works. – Tommy Thang Feb 01 '20 at 15:17
  • Is the IP in the "message server" column when you use saplogon.exe ? I tried that also, but still have issue – Tommy Thang Feb 01 '20 at 15:20
  • I think I found it, need to use backslash string to make the double quote of my Name: Name = "\"PRD\"" – Tommy Thang Feb 01 '20 at 15:32

2 Answers2

3

The variable Name needs to be "PRD" (the string itself must contain double quotes). Python considers Name = """PRD""" the same as Name = "PRD" so it's incorrect because the variable Name will just contain PRD (missing double quotes).

Hence, need to use string backslash (Name = "\"PRD\"" or other possibilities mentioned here) to maintain the double quote in the variable Name.

Complete code:

from subprocess import call
import win32com.client
import time
import os

GUIPath = 'C:/Program Files (x86)/SAP/FrontEnd/SAPgui/'
WinTitle = 'SAP'
Name = "\"PRD\""
SID = 'PRD'
InstanceNo = '01'

shell = win32com.client.Dispatch("WScript.Shell")
call(os.path.join(GUIPath, 'SAPgui.exe') + " " + Name + " " + InstanceNo)
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Tommy Thang
  • 63
  • 1
  • 7
  • Thanks for feedback. About `sapgui.exe`, I thought that it'd accept only a host name or IP address as first argument, but it also accepts a logon entry description if it's **between double quotes**. I tried this PowerShell and it worked: `&"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapgui.exe" '"Dev system"'`. It took the logon entry `Dev system` that I had defined in my SAP Logon application. It must be the exact description of the logon entry (and not the system ID). It's case sensitive. SAP didn't describe this feature in [Note 103019](https://launchpad.support.sap.com/#/notes/103019). – Sandra Rossi Feb 01 '20 at 19:16
  • By the way, you have passed a logon entry (because it's between double quotes) as first argument of sapgui.exe, so passing the instance number (your second argument) is ignored, as I have shown in my previous comment. – Sandra Rossi Feb 02 '20 at 00:22
  • yes, correct. Any instance will be accepted. btw, I tried with the IP address of "message server", but it didn't work. Are you using the same IP address of "message server" or what? Can you show your full script to access SAP with PowerShell through IP address? – Tommy Thang Feb 02 '20 at 04:29
  • Please ask another question (short answer: No, of the dispatcher (dp). If you want to use the message server (ms), few possibilities from [note 103019](https://launchpad.support.sap.com/#/notes/103019): `/R//G/` (e.g. "/R/ERP/G/PUBLIC"), `/M//S//G/` (e.g. "/M/server.domain/S/1080/G/PUBLIC"), `/R//M//G/` (e.g. "/R/ERP/M/server.domain/G/PUBLIC"), etc.) – Sandra Rossi Feb 02 '20 at 09:50
1

Use this simple one-liner for connecting:

import subprocess
subprocess.check_call(['C:\Program Files (x86)\SAP\FrontEnd\SAPgui\\sapshcut.exe', '-system=DCG210', '-client=100', '-user=USERNAME', '-pw=PASSWORD'])

You should use subprocess module instead of os.call, it is preferred now.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90