keepcalculator

06 Numbers and Powers

Send SMS Text Message From Command Line

Card No. 06·87592 · Updated · Standard formula, shown below

Yes, you can send an SMS text message directly from the command line on Linux, macOS, and Windows. The method depends on your hardware and carrier: you can use a USB GSM modem with tools like gammu or mmcli, a connected Android phone via adb, or an online API service like Twilio or AWS SNS for internet-based sending.

Why Send SMS From the Command Line?

Command-line SMS is useful for automation, scripting, and server notifications. Instead of logging into a web dashboard, you can trigger a text message from a cron job, a CI/CD pipeline, or a monitoring script. It also works well for bulk messaging when you need to send the same alert to many recipients. For developers, it eliminates the need to build a separate UI just to fire off a quick message.

Another common use case is two-factor authentication (2FA) for your own services, or alerting when a long-running task finishes. Because the command line is scriptable, you can pipe output directly into the SMS command, making it a natural fit for shell pipelines.

Method 1: Using a USB GSM Modem with Gammu

Gammu is a mature, open-source tool that works with many GSM modems, including old Nokia phones, Huawei dongles, and generic USB 3G/4G sticks. It communicates over AT commands through a serial port or USB.

  1. Install gammu: sudo apt install gammu (Debian/Ubuntu), brew install gammu (macOS), or download from the official site for Windows.
  2. Find your modem’s port: gammu detect (Linux/macOS) or check Device Manager on Windows (usually COM3, COM4, etc.).
  3. Create a config file ~/.gammurc with the port and connection type (e.g., port = /dev/ttyUSB0, connection = at115200).
  4. Send a message: gammu sendsms TEXT 1234567890 -text "Hello from terminal"

Gammu also supports concatenated multi-part messages and sending to multiple numbers in a loop. The modem must have a SIM card with an active SMS plan. This method works entirely offline—no internet connection is needed, only the cellular network.

Method 2: Using a Connected Android Phone via ADB

If you have an Android phone with USB debugging enabled, you can use the Android Debug Bridge (adb) to send SMS through the phone’s own messaging app. This is handy when you don’t have a separate modem but do have a spare phone.

  1. Enable Developer Options and USB Debugging on the phone.
  2. Install adb (part of Android Platform Tools).
  3. Connect the phone via USB and run adb devices to confirm it’s listed.
  4. Send a message with: adb shell service call isms 7 i32 0 s16 "com.android.mms" s16 "1234567890" s16 "null" s16 "Hello from adb" s16 "null" s16 "null"

This command works on many Android versions but may vary slightly by manufacturer. A simpler alternative is to use adb shell input text to type into the messaging app, but that requires the screen to be on and the app open. The service call method sends directly without opening the UI. Note that the phone must have a working SIM and a messaging plan.

Method 3: Using an Online SMS API (Twilio, AWS SNS, etc.)

For cloud-based sending, you can use a REST API from a service like Twilio, AWS SNS, or Vonage. These services provide a phone number and charge per message. They require an internet connection and an account with API credentials.

With Twilio, you can send a message using curl:

curl -X POST https://api.twilio.com/2010-04-01/Accounts/YOUR_ACCOUNT_SID/Messages.json \
--data-urlencode "To=+1234567890" \
--data-urlencode "From=+1987654321" \
--data-urlencode "Body=Hello from curl" \
-u YOUR_ACCOUNT_SID:YOUR_AUTH_TOKEN

AWS SNS has a CLI: aws sns publish --phone-number +1234567890 --message "Hello". These services are reliable and scale well, but they cost money per message and require you to register a sender ID or buy a phone number. They also support international sending, which is often harder with a local modem.

How to Choose the Right Method

  • Offline/private: USB modem with Gammu—no internet, full control, but requires hardware.
  • No extra hardware: Android phone via adb—uses existing phone, but needs USB debugging and a cable.
  • Cloud/scale: SMS API—works anywhere, handles high volume, but costs money and needs internet.
  • Free/one-off: Email-to-SMS gateways (e.g., number@carrier.com)—not exactly command line, but can be triggered with mail or curl.

Consider the volume: for a few messages a day, a modem or phone is fine. For thousands, an API is essential. Also think about reliability—APIs have uptime guarantees, while a consumer modem may drop connection.

Common Problems and Troubleshooting

  • Modem not detected: Check permissions (sudo chmod 666 /dev/ttyUSB0), try a different USB port, or use lsusb to see if the device is present.
  • AT command errors: Some modems need a different baud rate or connection string. Try connection = at or at19200.
  • ADB not seeing phone: Install the correct USB driver, enable USB debugging, and accept the RSA key prompt on the phone.
  • API authentication failure: Double-check your SID and token, and ensure the phone number is verified (Twilio requires verification for trial accounts).
  • Message not delivered: Verify the number format (E.164, e.g., +1 for US), check if the SIM has credit, and look at the modem’s signal strength with gammu getnetworklevel.

Costs and Safety Considerations

Using a USB modem or Android phone only costs your normal SMS plan rates. APIs charge per message—typically a fraction of a cent to a few cents depending on destination and provider. Always check the pricing page of the service you choose.

Safety: Never send sensitive information like passwords or 2FA codes via SMS if you can avoid it—SMS is not encrypted and can be intercepted. Also, be aware of your carrier’s terms: sending unsolicited bulk messages may violate anti-spam laws. For automation, always test with a single number first.

Frequently Asked Questions

Can I send SMS without a modem or phone?

Yes, using an online API like Twilio or AWS SNS. You only need an internet connection and an account. The service handles the SMS delivery through its own infrastructure.

Is it free to send SMS from the command line?

If you use a USB modem or an Android phone, you pay only for the SMS plan on the SIM. Online APIs are not free—they charge per message, though some offer a small free trial credit.

How do I send SMS from a Linux server?

Install gammu and a USB modem, or use an API with curl. The same methods work on any Linux distribution. Ensure the modem is recognized and the SIM is active.

Can I send SMS from Windows command prompt?

Yes. Gammu has a Windows build, or you can use curl to call an API. For a phone, you can use adb on Windows as well.

What is the difference between SMS and MMS from the command line?

SMS is plain text up to 160 characters (or 153 with concatenation). MMS can include images, audio, and longer text, but requires more complex AT commands or API parameters. Gammu supports MMS with additional configuration, and APIs like Twilio support MMS by adding a media URL.

Is it possible to receive SMS from the command line?

Yes. With Gammu, you can run gammu getallsms to read messages from the modem. With an API, you can set up a webhook to receive messages, or use the service’s CLI to fetch message logs.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →