2017-02-25 1 views
1

OpenWRT가있는 라우터에서 실행할 스크립트를 개발하려고합니다. 지금 나는 라우터의 IP를 얻을 수있다. Firebase에 업로드하고 싶습니다. 나는 이것을 실행하면CURL (Firebase REST API)에 변수 전달

var=$(/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'); 
curl -X PUT -d '{"IPv4": '"${var}"'}' \ 
     'https://name.firebaseio.com/Values.json' 

내가 구문 분석 오류 :

curl -X PUT -d '{"IPv4": "192.168.1.1"}' \ 
    'https://name.firebaseio.com/Values.json' 

는 내가 알고 싶은 것은 입력으로 IP 값을 전달입니다 : 나는 다음과 같은 경우 그것은 데이터베이스에 기록하세요 :

{ 
    "error" : "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names." 
} 

답변

1

당신이 ${var} 주위에 따옴표 앞에 백 슬래시를 넣어 경우 curl 호출이 작동합니다

curl -X PUT -d '{"IPv4": '\"${var}\"'}' \ 
     'https://name.firebaseio.com/Values.json' 

또는 더 나은, 단지 큰 따옴표로 전체 -d 인수를 넣어 사용은 내부에 큰 따옴표를 백 슬래쉬 :

대조적으로
curl -X PUT -d "{\"IPv4\": \"${var}\"}" \ 
     'https://name.firebaseio.com/Values.json' 

,이 질문에 포맷 있어요 방법이의 var 값에 인용 부호가 전송됩니다 당신의 JSON : 따옴표없이

$ curl --trace-ascii - -X PUT -d '{"IPv4": '"${var}"'}' \ 
     'https://name.firebaseio.com/Values.json' 

== Info: Trying 104.154.130.226... 
== Info: TCP_NODELAY set 
== Info: Connected to name.firebaseio.com (104.154.130.226) port 443 (#0) 
== Info: TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 
== Info: Server certificate: firebaseio.com 
== Info: Server certificate: Google Internet Authority G2 
== Info: Server certificate: GeoTrust Global CA 
=> Send header, 163 bytes (0xa3) 
0000: PUT /Values.json HTTP/1.1 
001b: Host: name.firebaseio.com 
0036: User-Agent: curl/7.51.0 
004f: Accept: */* 
005c: Content-Length: 25 
0070: Content-Type: application/x-www-form-urlencoded 
00a1: 
=> Send data, 25 bytes (0x19) 
0000: {"IPv4": 192.168.111.100} 
       ^^^^^^^^^^^^^^^ 

, 그 값이 JSON 파서 (안 문자열, 숫자가 아닌 아닌 객체가 아닌 배열이 아닌 true/,138를 인식 입력이 아니다이 아니라 null), 구문 분석에 실패합니다.