sassoftware / saspy

A Python interface module to the SAS System. It works with Linux, Windows, and Mainframe SAS as well as with SAS in Viya.
https://sassoftware.github.io/saspy
Other
366 stars 149 forks source link

connecting with plink #564

Closed sterisa closed 8 months ago

sterisa commented 8 months ago

Discussed in https://github.com/sassoftware/saspy/discussions/563

Originally posted by **sterisa** October 20, 2023 Hi, I am connecting from windows to a linux machine using ssh. I put plink as the ssh but it does not connect ("Invalid response from SAS on inital submission."). When I execute the command manually everything is ok. Any idea? Thanks ``` sas = saspy.SASsession() Using SAS Config named: ssh _[I am pressing Ctrl-C after a while]_ ^CException caught! SAS attention handling not supported over ssh. Please enter (T) to terminate SAS or (C) to continue.>? t SAS Connection failed. No connection established. Double check your settings in sascfg_personal.py file. Attempted to run program C:\Program Files\Centrify\Centrify PuTTY\plink with the following parameters:['C:\\Program Files\\Centrify\\Centrify PuTTY\\plink', '-t', 'srisa@prodpmsas4', '/base/sas/SAS_9.4/SASFoundation/9.4/bin/sas_en', '-fullstimer', '-nodms', '-stdio', '-terminal', '-nosyntaxcheck', '-pagesize', 'MAX', ''] Try running the following command (where saspy is running) manually to see if you can get more information on what went wrong: C:\Program Files\Centrify\Centrify PuTTY\plink -t srisa@prodpmsas4 /base/sas/SAS_9.4/SASFoundation/9.4/bin/sas_en -fullstimer -nodms -stdio -terminal -nosyntaxcheck -pagesize MAX No SAS process attached. SAS process has terminated unexpectedly. Invalid response from SAS on inital submission. printing the SASLOG as diagnostic Traceback (most recent call last): File "C:\pithon.shared\prod39\lib\site-packages\IPython\core\interactiveshell.py", line 3552, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in sas = saspy.SASsession() File "C:\pithon.shared\prod39\lib\site-packages\saspy\sasbase.py", line 610, in __init__ raise SASIOConnectionError(res) saspy.sasexceptions.SASIOConnectionError: Failure establishing SASsession. No SAS process attached. SAS process has terminated unexpectedly. Please refer to the Configuration Instructions in the SASPy Documentation at https://sassoftware.github.io/saspy/configuration You can also look for the error you've recieved in the Troublshooting guide at https://sassoftware.github.io/saspy/troubleshooting If you need more help, please open an Issue on the SASPy GitHub site at https://github.com/sassoftware/saspy/issues ```
tomweber-sas commented 8 months ago

Hey, I'm not familiar with plink. I don't know if that is compatible to use instead of ssh. But, looking at the output above it looks like the path isn't correct. There's a blank in the middle of the command, which could be from not using double backslashes or raw string; cuz it's windows. You need to type either

r'C:\path\path\path'

or

'C:\\path\\path\\path'

I don't know if that will make it work or not, but it's worth a try. Can you show me your configuration and also what happens when you run the command itself, like the message states? Just try it from a command prompt and see what it does.

Try running the following command (where saspy is running) manually to see if you can get more information on what went wrong:
C:\Program Files\Centrify\Centrify PuTTY\plink -t srisa@prodpmsas4 /base/sas/SAS_9.4/SASFoundation/9.4/bin/sas_en -fullstimer -nodms -stdio -terminal -nosyntaxcheck -pagesize MAX  

I did find plink on my system and tried it from a command prompt. It doesn't seem to use rsa keys and it's prompting me for my password, which is what's causing it to 'hang' when I tried specifying it as the ssh value for saspy. It seems to allow of the -i identity file, which saspy supports, so that might get around the password prompt; I don't have one to try, but it seems like it could work.

If we can see what happens when you're trying to use it directly then we can try to see if it can work from saspy!

Thanks, Tom

sterisa commented 8 months ago

Tom Thanks for the help! Yes, the path made a difference (I was testing out commands in a powershell, which apparently does not behave like a regular command line) + I needed to add extra quotes But plink works for me without a password... I presume it connects to some central kerberos service. I am using plink elsewhere when I need to kick off something from windows into linux. ssh does instead ask for a password Long story short, I am now getting an "access is denied"

Should i switch to ssh?

Python 3.9.16 (main, Mar  8 2023, 10:39:24) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.33.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.33.0
Python 3.9.16 (main, Mar  8 2023, 10:39:24) [MSC v.1916 64 bit (AMD64)] on win32
import saspy
sas = saspy.SASsession()
Using SAS Config named: ssh
The OS Error was:
**Access is denied**
SAS Connection failed. No connection established. Double check your settings in sascfg_personal.py file.
Attempted to run program "C:\Program Files\Centrify\Centrify PuTTY\plink" with the following parameters:['"C:\\Program Files\\Centrify\\Centrify PuTTY\\plink"', '-t', 'srisa@prodpmsas4', '/base/sas/SAS_9.4/SASFoundation/9.4/bin/sas_en', '-fullstimer', '-nodms', '-stdio', '-terminal', '-nosyntaxcheck', '-pagesize', 'MAX', '']
If no OS Error above, try running the following command (where saspy is running) manually to see what is wrong:
"C:\Program Files\Centrify\Centrify PuTTY\plink" -t srisa@prodpmsas4 /base/sas/SAS_9.4/SASFoundation/9.4/bin/sas_en -fullstimer -nodms -stdio -terminal -nosyntaxcheck -pagesize MAX  
No SAS process attached. SAS process has terminated unexpectedly.
Invalid response from SAS on inital submission. printing the SASLOG as diagnostic
Traceback (most recent call last):
  File "C:\pithon.shared\prod39\lib\site-packages\IPython\core\interactiveshell.py", line 3552, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-97095405f39b>", line 1, in <cell line: 1>
    sas = saspy.SASsession()
  File "C:\pithon.shared\prod39\lib\site-packages\saspy\sasbase.py", line 610, in __init__
    raise SASIOConnectionError(res)
saspy.sasexceptions.SASIOConnectionError: Failure establishing SASsession.
No SAS process attached. SAS process has terminated unexpectedly.
Please refer to the Configuration Instructions in the SASPy Documentation at https://sassoftware.github.io/saspy/configuration
You can also look for the error you've recieved in the Troublshooting guide at https://sassoftware.github.io/saspy/troubleshooting
If you need more help, please open an Issue on the SASPy GitHub site at https://github.com/sassoftware/saspy/issues

This is now my config:

SAS_config_names=['ssh']
SAS_config_options = {'lock_down': False,
                      'verbose'  : True,
                      'prompt'   : True
                     }
SAS_output_options = {'output' : 'html5',       # not required unless changing any of the default
                      'style'  : 'HTMLBlue'}
default  = {'saspath'  : '/opt/sasinside/SASHome/SASFoundation/9.4/bin/sas_u8'
            }

ssh      = {'saspath' : '/base/sas/SAS_9.4/SASFoundation/9.4/bin/sas_en',
            'ssh'     : '"'+r'C:\Program Files\Centrify\Centrify PuTTY\plink'+'"',
            # 'sshpass':'',
            'host'    : 'prodpmsas4',
            # 'encoding': 'latin1',
            'luser': 'srisa',
            'options' : ["-fullstimer"]
            }

iomlinux = {'java'      : '/usr/bin/java',
            'iomhost'   : 'linux.iom.host',
            'iomport'   : 8591,
            }

iomwin   = {'java'      : '/usr/bin/java',
            'iomhost'   : 'windows.iom.host',
            'iomport'   : 8591,
            }

winlocal = {'java'      : 'java',
            'encoding'  : 'windows-1252',
            }

winiomlinux = {'java'   : 'java',
            'iomhost'   : 'linux.iom.host',
            'iomport'   : 8591,
            }

winiomwin  = {'java'    : 'java',
            'iomhost'   : 'windows.iom.host',
            'iomport'   : 8591,
            }

winiomIWA  = {'java'    : 'java',
            'iomhost'   : 'windows.iom.host',
            'iomport'   : 8591,
            'sspi'      : True
            }

iomcom = {
    'iomhost' : 'mynode.mycompany.org',
    'iomport' : 8591,
    'provider': 'sas.iomprovider',
    'encoding': 'windows-1252'}

httpsviya = {'url'     : 'https://viya.deployment.com',
             'context' : 'SAS Studio compute context',
             'authkey' : 'viya_user-pw',
             'options' : ["fullstimer", "memsize=1G"]
             }

httpviya = {'url'     : 'https://sastpw.rndk8s.openstack.sas.com:23456',
           #'port'    :  23456,   # can put different port here or ^ if it's not using the default port
            'context' : 'SAS Studio compute context',
            'authkey' : 'viya_user-pw',
            'options' : ["fullstimer", "memsize=1G"]
            }

When I copy paste the command in a cmd window:

C:\gitlab\models\python>"C:\Program Files\Centrify\Centrify PuTTY\plink" -t srisa@prodpmsas4 /base/sas/SAS_9.4/SASFoundation/9.4/bin/sas_en -fullstimer -nodms -stdio -terminal -nosyntaxcheck -pagesize MAX
1                                                          The SAS System                             10:25 Friday, October 20, 2023

NOTE: Unable to open SASUSER.REGSTRY. WORK.REGSTRY will be opened instead.
NOTE: All registry changes will be lost at the end of the session.

WARNING: Unable to copy SASUSER registry to WORK registry. Because of this, you will not see registry customizations during this
         session.
NOTE: Unable to open SASUSER.PROFILE. WORK.PROFILE will be opened instead.
NOTE: All profile changes will be lost at the end of the session.
NOTE: Copyright (c) 2002-2012 by SAS Institute Inc., Cary, NC, USA.
NOTE: SAS (r) Proprietary Software 9.4 (TS1M3)
      Licensed to AZTEC USA PACIFIC INVESTMENT LINA SERVER II, Site 70079613.
NOTE: This session is executing on the Linux 2.6.32-754.50.1.el6.x86_64 (LIN X64) platform.

NOTE: Updated analytical products:

      SAS/STAT 14.1
      SAS/ETS 14.1

NOTE: Additional host information:

 Linux LIN X64 2.6.32-754.50.1.el6.x86_64 #1 SMP Mon Feb 13 08:49:58 EST 2023 x86_64 Red Hat Enterprise Linux Server release 6.10
      (Santiago)

[.......]

NOTE: AUTOEXEC processing completed.
data x;i=1;erun^H^H^H;

1     data x;i=1;e;un
                 ----
                 180
ERROR 180-322: Statement is not valid or it is used out of proper order.
data x;i=1;run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.X may be incomplete.  When this step was stopped there were 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           7.47 seconds
      user cpu time       0.01 seconds
      system cpu time     0.00 seconds
      memory              627.59k
      OS Memory           10400.00k
      Timestamp           10/20/2023 10:25:43 AM
      Step Count                        12  Switch Count  1
      Page Faults                       1
      Page Reclaims                     386
      Page Swaps                        0
      Voluntary Context Switches        9
      Involuntary Context Switches      0
      Block Input Operations            184
      Block Output Operations           136

2     data x;i=1;run;
NOTE: The data set WORK.X has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      user cpu time       0.00 seconds
      system cpu time     0.01 seconds
      memory              765.96k
      OS Memory           10400.00k
      Timestamp           10/20/2023 10:25:43 AM
      Step Count                        13  Switch Count  0
      Page Faults                       0
      Page Reclaims                     227
      Page Swaps                        0
      Voluntary Context Switches        0
      Involuntary Context Switches      0
      Block Input Operations            0
      Block Output Operations           144

3

4     endsas;

5     endsas;
NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
NOTE: The SAS System used:
      real time           3:29.70
      user cpu time       0.06 seconds
      system cpu time     0.04 seconds
      memory              9184.34k
      OS Memory           10660.00k
      Timestamp           10/20/2023 10:28:54 AM
      Step Count                        13  Switch Count  128
      Page Faults                       1
      Page Reclaims                     14744
      Page Swaps                        0
      Voluntary Context Switches        317
      Involuntary Context Switches      3
      Block Input Operations            184
      Block Output Operations           976

C:\gitlab\models\python>
tomweber-sas commented 8 months ago

Oh, your path does have a blank in it. I was thinking there was a single backslash being interpreted as an escape char which changed the path; I see that a lot in paths when people don't realize windows backslashes are treated as that sometimes.

I think how you had it before is actually right; not having extra quotes embedded with the path. It may be that those quotes are causing the new error. When I try adding extra quotes to my ssh path, I get a file not found error. Executing that path internally doesn't require, and doesn't work, like typing it into a prompt, where windows needs the extra quotes to work right. I think you should remove those extra quotes from the config.

So, that then leaves a question about if it's working from the command line, why is it hanging from saspy? I've been trying to get it to use a .pem file I'm creating from my actual rsa keys, but it's not using that. It tries but says it can't use it without a reason why. I'm still trying to get around the password prompt. Then I can see if it can work from saspy or not. I still wonder if there could be some environment difference from where you're running python and where you were able to run the command from the command prompt? Just to alleviate that question, can you run python from that same environment where the command worked? Just in case it's an environment thing? I've seen things where when running form cmd prompt, the environment isn't the same as from anaconda, for instance.

If that still hangs, then there's still something not working with this. If you can set up passwordless ssh, and use that, then it should just work. I'm still trying to get plink to work w/out my pw, so I can see if it still fails from saspy and then maybe try to debug it further.

sterisa commented 8 months ago

I tried a few more ways, but now I switched to rsa keys. Not great as this is now client+user specific... but it works!

Thanks

Stefano

sterisa commented 8 months ago

Thanks

tomweber-sas commented 8 months ago

Hey, I'm glad you got ssh to work! I was able to finally get plink to connect; had to convert my id_rsa key to putty's format, .ppk Then plink connected for me without prompting me for a password. So, then I was able to try that with saspy, and it hung!

So, I debugged it to see what was going on. Turns out is is connecting, and I am interacting w/ SAS. The problem is that for some reason, the stderr output is coming back on stdout. So that's why it's hanging; I'm not getting the output from SAS on the correct channel. I don't see any options in plink for that; I'm not sure why that's happening. I don't know if it somehow merges stderr into stdout or what, but it won't work like that.

So, at least I see what's happening. Just don't know why or if it can work like it should, or how to make it. But, I'm glad you're working with ssh!

tomweber-sas commented 8 months ago

yep, it has stderr merged in with stdout, so this can't work as there's no way to split the two after they've been merged. I need the log separate from the list for this to be able to work.