8

I'd like to make some scripted like network traffic from an android phone. Is it possible to complete the task by making an HTTP request from adb shell? I noticed there is "adb shell ping" command that may test the network connectivity, but it does not generate HTTP request. I am thinking something like some utility similar to telnet.

EDIT: I see Open a broswer will serve my purpose, although not quite elegantly Need command line to start web browser using adb

Community
  • 1
  • 1
CrepuscularV
  • 983
  • 3
  • 12
  • 25
  • 4
    `adb shell ping` is just executing /system/bin/ping in a shell. There's no telnet/curl/wget/... utilities on stock Android, but if you copy a version of busybox onto your phone, you should be able to do "adb shell busybox wget " to generate HTTP get requests. – adelphus Oct 30 '15 at 01:24

2 Answers2

4

Netcat may work, if available:

$ nc www.example.com 80
GET / HTTP/1.1
Host: www.example.com

i.e. enter the first line on the command line, and the two HTTP protocol lines to standard input.

This returns:

HTTP/1.1 200 OK
Age: 185995
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Mon, 10 May 2021 10:39:58 GMT
Etag: "3147526947+gzip+ident"
Expires: Mon, 17 May 2021 10:39:58 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (nyb/1D07)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
...
wodow
  • 3,871
  • 5
  • 33
  • 45
0

I think an easy way is to cross compile a simple fetch program.

Using rust:

main.rs

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut args = std::env::args().skip(1);
    let file = args.next().ok_or("no file")?;
    let name = args.next().ok_or("no name")?;
    let mut resp = ureq::get(&file).call()?.into_reader();

    std::io::copy(&mut resp, &mut std::fs::File::create(name)?)?;
    Ok(())
}

Cargo.toml

[package]
name = "fetch"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ureq = { version = "2.7.1"}

[profile.release]
strip = true  # Automatically strip symbols from the binary.

To cross compile https://github.com/bbqsrc/cargo-ndk make things easier, you still need to download android ndk for example from here https://dl.google.com/android/repository/android-ndk-r25c-linux.zip

cargo ndk -t arm64-v8a build --release 
adb push target/aarch64-linux-android/release/fetch /data/local/tmp

Now you can use it with

adb shell "/data/local/tmp/fetch target_url file_name"
Zero14
  • 31
  • 3