How to reply to a USSD menu using Linux command line tools? - linux

I want to obtain information about the account balance of a SIM card I have installed in a mobile broadband modem in my computer. For my mobile network operator, this works using the USSD code 100#.
I found that gammu can send that USSD code using gammu getussd '100#'. In response, gammu shows me this USSD menu:
Press Ctrl+C to break...
USSD received
Status : Action needed
Service reply : "Reply with your option:
1.Account Balance
2.Buy a Data Plan
3.Top Up Now
No response in specified timeout. Probably phone not connected.
How can select an option from that menu (like, 1)?

It seems to me that ModemManager (and its command line client interface mmcli) is currently the most sophisticated, highest-quality solution for interacting with USSD and USSD menus under Linux.
Installation (under Debian / Ubuntu Linux):
sudo apt-get install modemmanager modem-manager-gui
Usage (following the manpage):
Listing your modems: mmcli -L. This will show a modem device path like /org/freedesktop/ModemManager1/Modem/12 and you can use the number at its end to specify the modem to use after the -m option in the following commands.
Showing attributes of your modem: mmcli -m 12.
Enabling the modem (needed before using it for USSD): mmcli -m 12 -e
Starting a USSD session. For example, for Ncell this command shows the main USSD menu: mmcli -m 12 --3gpp-ussd-initiate="*100#"
Responding to a USSD menu. After the session is started, you may use a command like this to respond, here using option 1: mmcli -m 12 --3gpp-ussd-respond="1"
Canceling the USSD session on the given modem: mmcli -m 12 --3gpp-ussd-cancel.
Obtaining the status of all USSD sessions (of all available modems): mmcli --3gpp-ussd-status.
With most hardware, this should work properly and immediately as described.
(With my hardware however, I could not use mmcli so far due to a bug. All USSD related commands in mmcli would reply error: modem not unlocked yet, and mmcli -m 12 | grep " lock" would show that the sim-pin2 lock is enabled. But it is not (means I could not get past this by disabling the lock). And even if it would be enabled, that lock would not limit USSD usage (it's rather just meant for limiting outgoing calls to certain numbers). So, I'll have to remove this overzealous test condition from here, compile it myself and test again … .)

The Linux command line tool gsm-ussd has support for USSD sessions that will allow you to answer to USSD menus. For detailed installation and configuration instructions see here. The version 0.4 Debian package offered there for installation is the latest dev branch version.
However, the support for this is not yet stable [source]. At least for me, it does not work. With different hardware and / or mobile network operators, you might have more luck.
The way it is meant to be used is like this (using Ncell as example):
$ gsm-ussd -m /dev/ttyUSB1 "*100#"
USSD session open, to cancel use "gsm-ussd -c".
1 Account
2 Services operations
3 Offices information
4 How to call Call Center
5 Change password
Note:
Back:* Top:#
To reply and select an option, you would send something like:
$ gsm-ussd -m /dev/ttyUSB1 "1"
To end the session and return back to normal single-command USSD mode, you would execute gsm-ussd -c. More complete documentation is here.

I found that I can successfully navigate USSD menus with AT commands directly. Given my system's issues with the otherwise preferable mmcli solution (see my other answer for details), this is so far the only working solution to navigate USSD for me.
How to get this to work:
(1) Install an AT terminal. I chose atinout to communicate with the modem via AT commands. You can also use any other AT terminal software like putty, minicom etc.. To install atinout:
sudo apt-get install ruby-ronn;
git clone git://git.code.sf.net/p/atinout/code atinout;
cd atinout;
make;
sudo checkinstall make install;
(2) If you use ModemManager (which is the default under Ubuntu Linux), you might have to disable it first to allow atinout to access your modem:
sudo stop modemmanager;
(3) Now, to receive and reply to an USSD menu, you would use a command like this:
atinout - /dev/ttyUSB1 - < <(echo "AT+CUSD=1,\"100#\",15") && sleep 4 && \
atinout - /dev/ttyUSB1 - < <(echo "AT") && \
\
atinout - /dev/ttyUSB1 - < <(echo "AT+CUSD=1,\"1\",15") && sleep 4 && \
atinout - /dev/ttyUSB1 - < <(echo "AT");
This assumes your modem is at /dev/ttyUSB1 and the code to receive the USSD menu is 100#. Adapt to your situation.
Explanation: The command sends 100# to the network to receive the USSD menu, and then 1 to choose the first option. Spreading one USSD menu session over multiple atinout calls like this does not break the session if you don't exceed the timeouts (which are usually >20s).
Troubleshooting: If the above all-in-one command does not work, try executing the individual parts manually, repeating those that fail. If something fails repeatedly ("resource busy" etc.), your modem might be in a strange state. Reset it, or just let the computer go through a suspend / resume cycle.

It's currently not possible with Gammu command line, but you can use simple Python script using python-gammu: https://github.com/gammu/python-gammu/blob/master/examples/service_numbers.py

The answer from the link below worked great for me.
You can add the modemmanager PPA and update your apt. There's a bug mentioned in the link for some usb modems, but that has been fixed with the update
sudo add-apt-repository ppa:aleksander-m/modemmanager-xenial<br>
sudo apt-get update
Then follow this: https://stackoverflow.com/a/31864567/6161579
Thanks #tanius

Related

Serial port unavaliable arduino

Trying to upload a code to arduino, but whether in the Arduino IDE or Arduino Create, both return this erro while uploading. Running on Linux Tara(mint 19 cinnamon).
./opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -hardware opt/arduino-builder/hardware -hardware ./opt/cores -tools opt/arduino-builder/tools -tools ./opt/tools -built-in-libraries opt/libraries/latest -logger humantags -fqbn arduino:avr:mega:cpu=atmega2560 -build-cache /tmp -build-path /tmp/716441957/build -verbose -libraries /tmp/716441957/custom -libraries /tmp/716441957/pinned /tmp/716441957/sketch_oct8a
Sketch uses 8280 bytes (3%) of program storage space. Maximum is 253952 bytes.
Global variables use 443 bytes (5%) of dynamic memory, leaving 7749 bytes for local variables. Maximum is 8192 bytes.
Programming with: Serial
Flashing with command:/home/jesus/.arduino-create/arduino/avrdude/6.3.0-arduino9/bin/avrdude -C/home/jesus/.arduino-create/arduino/avrdude/6.3.0-arduino9/etc/avrdude.conf -q -q -patmega2560 -cwiring -P/dev/ttyACM0 -b115200 -D -Uflash:w:/tmp/arduino-create-agent734074237/sketch_oct8a.hex:i
avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied
ioctl("TIOCMGET"): Inappropriate ioctl for device
ioctl("TIOCMGET"): Inappropriate ioctl for device
1#
First, check the port in your IDE. In Arduino tools->port
If the port is hidden or you can not move the cursor over this then run this commands in your terminal.If everything ok then skips this and follow the second part.
sudo apt-get install librxtx-java -y
sudo usermod -aG dialout $USER
sudo apt-get install gnome-system-tools
2#
After this again check the first method. If it is not working then run this commands
ls -l /dev/ttyACM*
sudo usermod -a -G dialout <username>
You probably have another program running which is already using this port.
You should close most other programs like putty or another serial monitor app.
Otherwise, try to reconnect the Arduino to the PC.
I know that these ideas below come from using a Teensy, but they may help you.
Sometimes there are the ACM* ports listed in the Arduino IDE. Try looking at the ser ports. I know when I am running my Teensy, sometimes I have them switched and need to select the correct one.
Also, from my experience with the Teensy, you might need to add a udev rule to allow permissions to access the port from non-root user. Here is the link that shows the udev file.
There is no direct way to solve this issue. In addition to it, you are not using an IDE. I will list the things you need to check, I am sure this will solve your problem.
I am not good at Linux environment so I will refer to applications names as window, you go the corresponding application in Linux.
Go to linux device manager and see for your arduino board. It should have proper naming like "arduino uno at port 3", then use the correct port in your command. If this name does not come properly then it means Driver is not available in your machine. So, go to step2.
Find the driver online and install it in your system, I am not sure about the support of linux with arduino, once it is done please repeat step1.
So, I conclude in short that you do not have the proper driver (which is strongly believe) or pointing the wrong port. I am not an expert with the udev rules, but it is definitely an issue you can experience with these kinds of micro-controllers.

Using netstat in Linux

I want to see statistics for each protocol (TCP/UDP) on my Linux Yocto. I try use netstat -s command (http://www.thegeekstuff.com/2010/03/netstat-command-examples/).
But i receive error netstat:invalid option -- 's'. It is because BusyBox's netstat version doesn't contain the -s option.
Is it possible to install "normal" netstat?
I try to install nettools (which includes netstat) using opkg install, but package manager can't find it.
I also try to compile and install net-tools source from here - https://sourceforge.net/projects/net-tools/, but i receive error during installation.
Maybee i can download package from here: rpmfind.net ?
Is it possible to see statistic without using netstat? (i need information, which command netstat -s returns)
(Example of netstat -s output:
udp:
71208 datagrams received
0 with incomplete header
0 with bad data length field
0 with bad checksum
0 with no checksum
832 dropped due to no socket
16 broadcast/multicast datagrams dropped due to no socket
1971 dropped due to full socket buffers
0 not for hashed pcb
68389 delivered
137685 datagrams output
)
the last netstat release is super old. you should build it straight out of git instead (don't worry, we try to keep it sane at all times). use the "Download Snapshot" link in the online git browser.
that said, have you seen the iproute2 project ? it includes tools like nstat and generally preferred over net-tools when it comes to scripting.

How do I deploy code to hardware nodes that are all on seperate networks?

This is an interesting problem I've been thinking about recently and have not come up with or found a solution that I find acceptable.
I'm playing with Raspberry Pi's and have 6 currently that I want to use throughout a few of my personal properties for surveillance purposes.
Making them work and sending video streams to my server is all easy, well and good - but how in the world do I deploy code updates to these "nodes" that are not on the same network, some are behind wi-fi networks that I don't have port forwarding access to also, so it's not like I can just post forward and SSH into them and run some .sh magic to update things.
The best I've come up with is using autossh to keep a constant, connection open to one of my servers through reverse ssh, and then ssh in to them through my parent server in parallel and running a .sh script on them when I want to update.. But this seems overly excessive and I'm sure there's some solution or platform out there that exists to solve this - how else do companies like Redbox or Nest for example update firmware on their systems remotely?
I'm actually doing something similar. I have Pi's deployed around the city that I live in. In order to not have to worry about port-forwarding and people changing their router configurations, I started using a service called Pagekite http://pagekite.net/
I'm not affiliated with them, but I can't say enough good things about the service and price. My Pi's are hooked up to screens that need to display certain things at certain times, and I'm able to VNC in very easily no matter where the Pi is to see what's currently playing. I can obviously just ssh in as well.
The following steps from my pi setup guide deal with installing pagekite and getting it to start on boot:
echo deb http://pagekite.net/pk/deb/ pagekite main | sudo tee -a /etc/apt/sources.list
sudo apt-key adv --recv-keys --keyserver keys.gnupg.net AED248B1C7B2CAC3
sudo apt-get update
sudo apt-get install pagekite
sudo leafpad /etc/pagekite.d/10_account.rc
Replace NAME.pagekite.me with the name of the kite
Replace YOURSECRET with whatever the secret is from the pagekite admin console
Remove the line “abort_not_configured” and the comment above it
sudo cp /etc/pagekite.d/80_sshd.rc.sample /etc/pagekite.d/80_sshd.rc
sudo invoke-rc.d pagekite restart
sudo reboot
This assumes you've made an account and setup a "kite"
I think you basically need a reliable reverse tunnel such as Pagekite, especially if you plan on expanding your network, as it will turn into a nightmare at a certain size. I believe I'm just going to keep a list of ssh usernames, ssh passwords, and pagekite addresses, then write a script that loops through them and rsync's my local directory with the new code to the remote directory on the pi.

gatttool and hcitool on Linux

I'm trying to connect to a device using gatttool on Linux. I run hcitool lescan to get the device MAC address, and then gatttool -i hci1 -b <macaddr> -t random -I. Then I type connect. I see [CON] as expected, but the problem is that it disappears automatically after about 1s, with no error messages. I have been searching for hours, but I don't see why that is so?
Note: I have run hcitool lecc before running gatttool one time just to try it, but it seems to me that since then, gatttool connection is not working properly anymore (unexpected disconnection as explained above). Is it because I have used hcitool lecc? If yes, is there a way to "undo" it?
Not sure if this helps you because I'm seeing this behavior on my Raspberry Pi (an up-to-date wheezy version) and bluez 5.18 (released April 2014.)
I can connect to my BLE peripheral from the command line after doing an lecc without any problems. I don't think you need to reset after an lecc. Regardless, if you do want to reset your BT dongle because it is in some funky state, look at this excellent step-by-step guide to reset the BT dongle. I have followed these steps with consistent results.
I can connect and query say primary but more often than not, the connection drops before I can manually type in primary. See the results below. If I use the up/down arrows after connecting to quickly scroll to primary, I am able to see primary services offered by this peripheral without any problems.
~ $ sudo gatttool -I -i hci0 -m 48 -b 20:CD:39:A8:11:AF
[20:CD:39:A8:11:AF][LE]> connect
Attempting to connect to 20:CD:39:A8:11:AF
Connection successful
[20:CD:39:A8:11:AF][LE]> primary
Command Failed: Disconnected

Cannot connect to X server :0.0 with a Qt application

Compiling on Fedora 10.
I have just started my first qt GUI application. I used all the default settings.
Its just a simple form. It builds OK without any errors. But when I try and run the application. I get the following message:
Starting /home/rob/projects/qt/test1/test1/test1...
No protocol specified
test1: cannot connect to X server :0.0
Thanks for any advice,
The general causes for this are as follows:
DISPLAY not set in the environment.
Solution:
export DISPLAY=:0.0
./myQtCmdHere
( This one doesn't appear to be the one at fault though, as its saying which X display its trying to connect to. Also, its not always 0.0, but most of the time it is )
Non-Authorised User trying to run the X Application
Solution ( as X owning user, ie: yourself )
xhost +local:root # where root is the local user you want to grant access to.
Also, if you'd like your X server to be able to receive connection over TCP, these days you must almost always explicitly enable this. To test whether you're server is allowing remote TCP connections try:
telnet 127.0.0.1 6000
If telnet is able to connect, then your X server is listening. If it can't, then neither will any remote X application and you need to enable remote TCP connections on your server.
Adding to above answers.
I was in a similar situation while running tests for Code2Pdf at travis.
I solved the problem using xvfb-run. Quoting from the manpage,
xvfb-run is a wrapper for the Xvfb(1x) command which simplifies the task of running commands (typically an X client, or a script containing a list of clients to be run) within a virtual X server environment.
The script that I wrote was:
check_install_xvfb() { # check and install xvfb
if hash xvfb-run 2>/dev/null; then
:
else
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install xvfb
fi
}
check_install_xvfb
export DISPLAY=localhost:1.0
xvfb-run -a bash .misc/tests.sh
# .misc/tests.sh is script that runs unit tests. You can replace it with something suitable to you.
Please bear with my bash code style. I am a noob bash programmer.
Running the above script helped me.
You can see the failing build and passing build.
Hope it helps

Resources