Find the answer to your Linux question:
Results 1 to 8 of 8
I'm trying to use curl to retrieve the session-id from transmission-daemon and write it to a variable. Been fooling around with grep and cut for several hours now and I'm ...
  1. #1
    Just Joined! otkaz's Avatar
    Join Date
    Feb 2009
    Location
    Houston, TX
    Posts
    19

    grep question

    I'm trying to use curl to retrieve the session-id from transmission-daemon and write it to a variable. Been fooling around with grep and cut for several hours now and I'm getting very close but hit a wall
    Code:
    SESSIONID=`curl -s -I hxxp://localhost:9091/transmission/rpc/ | grep -i ^X-Transmission-Session-Id: | cut -d" " -f2-`
    returns
    + SESSIONID=$'Ato8KGo5X7nDEi4IYauQi14YHf5IRyQ0YzFRP6 gX4Js2qoKp\r'
    I need it to write the variable like this
    + SESSIONID=Ato8KGo5X7nDEi4IYauQi14YHf5IRyQ0YzFRP6gX 4Js2qoKp
    here is the out put from hxxp://localhost:9091/transmission/rpc/ -v
    Code:
    + curl -s -I hxxp://localhost:9091/transmission/rpc/ -v
    * About to connect() to localhost port 9091 (#0)
    *   Trying localhost... connected
    * Connected to localhost (localhost) port 9091 (#0)
    > HEAD /transmission/rpc/ HTTP/1.1
    > User-Agent: curl/7.21.3 (x86_64-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
    > Host: localhost:9091
    > Accept: */*
    > 
    < HTTP/1.1 409 Conflict
    HTTP/1.1 409 Conflict
    < Server: Transmission
    Server: Transmission
    < X-Transmission-Session-Id: Ato8KGo5X7nDEi4IYauQi14YHf5IRyQ0YzFRP6gX4Js2qoKp
    X-Transmission-Session-Id: Ato8KGo5X7nDEi4IYauQi14YHf5IRyQ0YzFRP6gX4Js2qoKp
    < Date: Sat, 14 Jan 2012 01:33:05 GMT
    Date: Sat, 14 Jan 2012 01:33:05 GMT
    * no chunk, no close, no size. Assume close to signal end
    
    < 
    * Closing connection #0
    Thanks in advance for any suggestions
    the hxxp is due to me not having 15 posts yet

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    I am guessing that there are carriage returns stuck in the output of your curl command. Try piping the output to the dos2unix utility, e.g.:

    Code:
    SESSIONID=`curl ...|grep -i ^X-Transmission-Session-Id: |cut -d" " -f2-|dos2unix`
    Last edited by atreyu; 01-14-2012 at 06:33 PM. Reason: typo

  3. #3
    Just Joined! otkaz's Avatar
    Join Date
    Feb 2009
    Location
    Houston, TX
    Posts
    19
    Quote Originally Posted by atreyu View Post
    I am guessing that there are carriage returns stuck in the output of your curl command. Try piping the output to the dos2unix utility, e.g.:

    Code:
    SESSIONID=`curl ...|grep -i ^X-Transmission-Session-Id: |cut -d" " -f2-|dos2unix`
    Thanks for the suggestion. Unfortunately dos2unix is not on the system I'm writing the script for. Its being written for openelec which I cannot install software on unless I want to compile the kernel from source and add it that way. I need to find a solution that does not involve installing anything additional if possible. The carriage return seems to be messing things up when I use the variable with curl. Tells me the header is bad.

  4. #4
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    There are other ways to strip the carriage return. Here's a sed method (the echo bit is required if you don't have gnu sed):

    Code:
    SESSIONID=`curl ...|grep -i ^X-Transmission-Session-Id: |cut -d" " -f2-|sed `echo "s/\r$//"`

  5. #5
    Just Joined! otkaz's Avatar
    Join Date
    Feb 2009
    Location
    Houston, TX
    Posts
    19
    Quote Originally Posted by atreyu View Post
    There are other ways to strip the carriage return. Here's a sed method (the echo bit is required if you don't have gnu sed):

    Code:
    SESSIONID=`curl ...|grep -i ^X-Transmission-Session-Id: |cut -d" " -f2-|sed `echo "s/\r$//"`
    That did the trick. I do have sed thankfully. Script does exactly what I need it to now. Thanks so much!

  6. #6
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    And thanks to you for pointing out the OpenELEC Linux distribution. That may come in handy.

  7. #7
    Just Joined! otkaz's Avatar
    Join Date
    Feb 2009
    Location
    Houston, TX
    Posts
    19
    yah no problem If your running a HTPC its pretty nice to have a stripped down kernel. Makes boot time very fast, but if your wanting to add your own software its a bit of a hassle. I'm very conflicted on it. It has a default ssh password that cant be changed with out compiling from source and it runs everything as root. I like to access my machine remotely a lot for queuing up torrents and such, so its a pretty big security hole in my network. If your going to try it out I recommend the newest daily eden build. Been very stable for me. ssh is disabled by default in the newest builds and can be switched on if needed. Good luck and thanks again!

  8. #8
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    Interesting...thanks for the 411!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...