IP set in shell script on BBB

Hi
I am testing to set static ip or dhcp in shell script on BBB
I want to set a static ip or dhcp using connmanctl in a shell script
When ‘connmanctl services’ command is entered, the following result is displayed.

# connmanctl services
  *AO Wired                ethernet_9059af5f4217_cable
    HOME-0842            wifi_009e959b585c_484f4d452d30383432_managed_psk
    ATT440               wifi_009e959b585c_415454343430_managed_psk

I want to get eth0 (ethernet_9059af5f4217_cable)
and then I would like to set static ip or dhcp using connmanctl.
My shell script is as below.

#!/bin/bash

echo "set static ip----------------" 
eth0_ip="192.168.10.1"
netmsk="255.255.255.0"
gateway="192.168.10.1"
eth0_id=$(connmanctl services)
connmanctl config ${eth0_id} --ipv4 manual ${eth0_ip} ${netmsk} ${gateway}

# echo "set dhcp ----------------"
# connmanctl config ${eth0_id} --ipv4 dhcp

Which part should be modified to set static ip or dhcp in shell script?
help me please!!

Regards.

This will work if you have one interface:

eth0_id=$(connmanctl services | grep Wired | awk -F' ' '{ print $3 }')

These setting are also stored under: /var/lib/connman/

debian@bbb-pwr01-ser09:~$ sudo cat /var/lib/connman/ethernet_c8a030abf366_cable/settings
[ethernet_c8a030abf366_cable]
Name=Wired
AutoConnect=true
Modified=2020-04-04T16:32:49.607789Z
IPv4.method=dhcp
IPv4.DHCP.LastAddress=192.168.1.137
IPv6.method=auto
IPv6.privacy=disabled

Regards,

Hi
Thank you for your solution.
Your solution works well.
but I have another problem

When I registered the shell script to the service.
It didnot work.
Shell script and service are as following
set_ip.sh

#!/bin/bash

echo "set eth0 to static ip ---------------------------"
eth0_ip="192.168.10.1"
netmask="255.255.255.0"
gateway="192.168.10.1"
eth0_id=$(connmanctl services | grep Wired | awk -F' ' '{ print $3 }')
connmanctl config ${eth0_id} --ipv4 manual ${eth0_ip} ${netmask} ${gateway}

#echo "set eth0 to dhcp -------------------------------"
#connmanctl config ${eth0_id} --ipv4 dhcp 

set_ip.service

[Unit]
Description=Enable IP type
After=generic-board-startup.service

[Service]
Type=simple
ExecStart=/usr/bin/set_ip.sh

[Install]
WantedBy=multi-user.target

I made dhcp work and then enable static ip in set_ip.sh.
when I turn off and on the power, dhcp, not static ip worked

Conversely, I made static ip work and then enable dhcp in set_ip.sh
when i turn off and on the power, static ip, not dhcp worked.
I want to enable static ip or dhcp in shell script.
How can I do? Help me

Regards.

@taek8461, I have to ask, what are you really trying to do? This ‘script’ is going to be too fragile. Connman has a concept of “provisioning files”, for what you’ve shown so far, it think it’s best you utilze this concept:

https://manpages.debian.org/buster/connman/connman-service.config.5.en.html

Note, on bootup, connman will use what ever you last set in those files, but if you make any changes to the “provisioning file” make sure you restart connman

Regards,

1 Like

Hi RobertCNelson

I just wanted my customers to easily change ip address in shell script.
I understand what you said.
so I will let them use ‘connman command’

Thank you so much~
Regards.