Newb Shell - How to get response in a variable - json

Just a quick question to solve an issue I've been facing for days now: how to get an wget json response in a shell variable?
I have so far a wget command like this:
wget "http://IP:PORT/webapi/auth.cgi?account=USER&passwd=PASSWD"
The server reponse is normally something like:
{"data":{"sid":"9O4leaoASc0wgB3J4N01003"},"success":true}
What I'd like to do is to grep the sid value in a variable (as it is used as login ticket), but also the success value in order to ensure that the command has been executed correctly...
I think it is a very easy command to build, but I've never practised wget/http reponse in shell command...
Thanks a lot for your help!
EDIT: Thanks for your help. I did gave a try to both answers, but I am having the same error message (whatever I do):
--2022-07-16 14:21:38-- http://xxxxxxxx:port/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=3&account=USER&passwd=PWD&session=SurveillanceStation&format=sid
Connecting to 192.168.1.100:5000... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
auth.cgi?api=SYNO.API.Auth&method=Login&version=3&account=USER&passwd=PASSWD&session=SurveillanceStation&format=sid: Permission denied
Cannot write to `auth.cgi?api=SYNO.API.Auth&method=Login&version=3&account=USER&passwd=PASSWD&session=SurveillanceStation&format=sid' (Permission denied).
The annoying thing: execution the URL from a web browser works just fine... :/

You can first store the result of wget command in variable and then use it:
VAR=$(wget "http://IP:PORT/webapi/auth.cgi?account=USER&passwd=PASSWD")
and then using jq extract from JSON file:
sid=$(echo $VAR|jq .data.sid)
success=$(echo $VAR|jq .success)
If you have problem with execution of wget you can try something like:
wget -O output_file 'http://xxxxxxxx:port/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=3&account=USER&passwd=PWD&session=SurveillanceStation&format=sid'
and then set variables:
sid=$(jq .data.sid output_file )
success=$(jq .success output_file )

I do not know why I am facing this Permission Denied error. Thus I gave a try to save cookie on a dedicated folder... And it works just fine :)
The final command lloks like:
VAR=$(wget -q --keep-session-cookies --save-cookies "/var/tmp/cookie_tmp" -O- "http://IP:PORT/webapi/auth.cgi?api=SYNO.API.Auth&method=login&version=1&account=USER&passwd=PWD&session=SurveillanceStation");
Thanks for your help (I learned a lot about sed ;) )

So this can be done using the stream editor or "sed". There is a lot to learn but for this post here is an idea of a code:
sid=$(wget <your url> | sed 's/.*sid":"\(.*\)"},.*/\1/')
success=$(wget <your url> | sed 's/.*success":\(.*\)}/\1/')
This will create 2 variables $sid and $success.
you can learn more about sed in depth here.
Hope this helped!

Related

SnowSQL connection issue with Snowflake

I have searched this forum for help on my specific issue, but can't find enough documentation to solve the error I am getting. I believe there is a configuration issue between snowsql and my trial snowflake account.
C:\Users\barberc> snowsql -a tyXXXXX.us-east-2 -u XXXXXX#gmail.com
Password:
250001 (n/a): Could not connect to Snowflake backend after 0 attempt(s).Aborting
If the error message is unclear, enable logging using -o log_level=DEBUG and see the log to find out the cause. Contact support for further help.
Goodbye!
I have also tried appending the ".aws" on it, but that doesn't seem to help.
I am trying to load data from a .csv into a database to work with.
Thank you in advance
enter image description here
You should run this SQL command to get your Snowflake account URL
use role accountadmin;
select system$whitelist();
Look for [{"type":"SNOWFLAKE_DEPLOYMENT","host":"xxxxx.ap-southeast-2.snowflakecomputing.com","port":443},
Extract the hostname before snowflakecomputing.com, that would be the account name to use in snowsql.
In this example, it will be
snowsql -a xxxxx.ap-southeast-2
You may also want to check with curl command, to make sure you can reach the Snowflake account.
curl -vvv https://xxxxx.ap-southeast-2.snowflakecomputing.com/console/login
If you found HTTP 200, that means a connection is established. Otherwise, there could be a proxy/firewall or routing issue.

Passing user and password in URL doesn't work

I have some URL like this : X.XXX.XXX.XXX:10080
I tried to add a user and password in this url like this: http://OLM:OLM794$#X.XXX.XXX.XXX:10080
User = OLM
Psw = OLM794$
And it doesn't work
Also when I run : curl http://OLM:OLM794$#X.XXX.XXX.XXX:10080 it shows me :
curl: (6) Could not resolve host:OLM:OLM794X.XXX.XXX.XXX, it removes $# and the port :10080
When I try : curl -u OLM X.XXX.XXX.XXX:10080 then I enter the password, it works, Im able to connecte to that server.
My need is to call my url with user and password like this:
http://OLM:OLM794$#X.XXX.XXX.XXX:10080
But it doesn't work.
I read this solution : Using cURL with a username and password? but I haven't found solution
Have you an idea why ?
It would seem curl parses and reconstructs the URL, leaving out characters that it thinks are illegal. For me, the same command gives a different error though.. It could be that your curl version differs from mine.
$ curl -v http://OLM:OLM794$#127.0.0.1:10000
* Rebuilt URL to: http://OLM:OLM794127.0.0.1:10000/
* Port number ended with 'O'
* Closing connection -1
curl: (3) Port number ended with 'O'
The solution is quite trivial, just url-escape the $ symbol: %24:
$ curl http://OLM:OLM794%24#127.0.0.1:10000
According to RFC1738, the dollar-sign was allowed as an unreserved character and could be directly used, but this old RFC has since been updated many times. RFC3986, for example, does not mention it as an unreserved character anymore. This means the $ symbol has received a special meaning and should be encoded in any URL where it does not serve the function it was given.

smbclient --authentication-file "session setup failed: NT_STATUS_INVALID_PARAMETER" and "SPNEGO(gse_krb5) NEG_TOKEN_INIT failed: NT_STATUS_NO_MEMORY"

(I have Centos 7 with samba-client.x86_64 4.6.2-8.el7 against windows server 2008 that is in a AD Domain controlled by separate windows server 2008 AD domain controller)
Started with this:
smbclient -W my.domain -U myuser //svr.my.domain/fred mypassword -c list
... which worked great, then decided to move domain,user and password into a file and use -A as described in the smbclient manpage. File windows-credentials, content:
username=myuser
domain=my.domain
password=mypassword
... with command line:
smbclient -A windows-credentials //svr.my.domain/fred -c list
.... did not work, gave error:
SPNEGO(gse_krb5) NEG_TOKEN_INIT failed: NT_STATUS_NO_MEMORY
session setup failed: NT_STATUS_NO_MEMORY
... an hour on the internet suggested lots of people had this trouble and just about each had a different ticked answer, and none of them worked for me. Tried various combinations of their answers - in particular, https://askubuntu.com/questions/1008992/ubuntu-17-10-to-access-windows-files-shares-within-workplace-it, and ended up with...
Created a separate my.smb.conf with just:
[global]
# seems to get rid of
# SPNEGO(gse_krb5) NEG_TOKEN_INIT failed: NT_STATUS_NO_MEMORY
client use spnego = no
# seems to get rid of
# session setup failed: NT_STATUS_NO_MEMORY
client ntlmv2 auth = no
... and used:
smbclient -s my.smb.conf -A windows-credentials //svr.my.domain/fred -c list
... and it looks like it works, but I'm not really sure as there seems to be credentials caching and a complete lack of information on how this stuff works or is supposed to work.
Can anyone actually explain any of this? Even if not, perhaps yet another answer to this problem will help someone somewhere.
This appears to be specific to Windows 2008. Attaching to Windows Server 2016 works without the modified smb.conf file. I have been unable to locate any real details.
In case of problems with smbclient
you can mount smb folder and use it like local folder
mount -t cifs //<ip>/<share folder>$ /mnt -o user=<user>,pass=<password>,domain=<workdomain>

Watson Knowledge Studio integration with Watson Discovery

I was able to get the my_config.json file by doing the provided curl statement and update/save it, but when I PUT it given the requested curl I am receiving a 415 error, unsupported media type. According to the documentation, it's supposed to accept the .json. This is what I'm using: curl -X PUT -u "discovered":"discoverypw" -H “Content-Type: application/json” -d#my_config.json
I supposed you are trying to update a configuration.
I tried the operation as described at the following page and could not reproduce your problem. I could update my configuration.
https://www.ibm.com/watson/developercloud/discovery/api/v1/#replace_configuration

How to check whether jenkins is fully up and running using webservice ?

I want to know , how to check whether jenkins is fully up and running using webservice ?
i want to use jenkins webservice to check this. Is there any way to do this ?
Thanks.
Probably the easiest way would be to perform a simple HTTP get on the Jenkins server root URL. You get a successful status (200) if Jenkins is fully up. If it is not you'll get 503 - Service Temporarily Unavailable (or possibly other errors depending on specific situation).
From the command line you can use a tool such as wget to perform that request.
Could also use curl:
while [[ $(curl -s -w "%{http_code}" http://server -o /dev/null) != "200" ]]; do
sleep 5
done
In addition to previous answers: If security is enabled, you will get HTTP 403 from the root URL. To get HTTP 200, you can check http://server/login.