If lan is connected? - linux

I want to write a script and put it in /etc/rc.local so that
if lan is connected then
rfkill block all
else
nmcli con up id 'Network id' --nowait
How should I write the if part and how should I check for "lan is connected" ?

You can check : How to detect the physical connected state of a network cable/connector?
I think at least one of the answer should work for you !

Related

Virtual Serial Port on Linux with Elfin EE-10

I want to connect the UART-Port of our central heater to my ProxMox-Server (LXC - Debian) over Web with Virtual Serial Port.
I bought this: https://www.aliexpress.com/item/4000945033185.html and connected it to the central heater and is reachable in the local network.
Since I have never done something in this direction I'm not exactly sure how to set up the connection and I hope someone can help me.
In the Web-Interface of the device, I can set up different protocols (see screenshot).
Thank you for your help.
You need to know more about what the heater is writing and expecting on the serial port. Did you select the right serial baud rate for it?
Have you read the EE-10 IOT_Device_Series_Software_Funtion_20200623 guide (in Downloads tab)? One possibility for further debug is to select the telnetd mode and connect from Linux with telnet to the IP address of the device, default port 23. (Ensure your Linux firewall allows this).
It seems it should ask for login and password, as in the web page. Then you might get an EPORT> prompt which is the EE-10 cli. Enter Exit to enter transparent mode when what you type is sent directly to the heater, and what you see comes from the heater.

Check if host is connected to LAN

I am writing a Python surveillance script for my home.
I have manually set a static LAN IP address to my phone by it's MAC address.
The Python script should check from time to time if my phone is still connected to the network. When I disconnect the script shall then continue and start the RTSP stream in my script.
The question
What is the simplest and best way to check if my phone is connected to the network or not?
I think the simplest way would be to ping your phone from time to time. Here's a bash example which I'm sure could be adapted to python one way or another:
ping -c2 192.168.1.200 >/dev/null && echo phone is connected || echo phone is offline
Just check the return code of the ping command.

Detecting that I am connected to a vpn/wifi and if not I automatically reconnect

so I made a script that logs into my schools vpn automatically by entering my username and password for me in my raspberry pi. Now I am trying to see if there is a way I can have a script running in the background that detects that I am connected to my vpn and also to my local wifi. If it detects there is no connection, then it will run my script of logging into the vpn automatically.
Below is the script I use to login to my vpn automatically my login.sh file:
#!/usr/bin/expect
spawn sudo openconnect vpn.ucr.edu/engineering
expect -r "\[sudo] .*\: " {
send "pw_for_my_linux\n"
}
expect "Username:" {
send "my_vpn_username\n"
}
expect "Password:" {
send "vpn_password\n"
}
interact
Perhaps you can use ethtool and check the connection status by looking at network interfaces.
ifconfig
(view network interface names, select network interface known to be associated with working vpn)
sudo ethtool eth0
Where 'eth0' is replaced by the name of the network interface that your vpn software is using
OUTPUT should look something like this if it's communicating (replace example net interface name eth0 with relevant one you can see in ifconfig):
Current message level: 0x00000007 (7)
drv probe link Link detected: yes
OUTPUT if down?:
Settings for eth0:
Link detected: no
In your bash code you can poll the 'link detected' status, parse out the answer to determine if you try reconnecting. Warning, haven't been able to test this on a vpn connection -- I have to use a special client other than openconnect, and for me I know the VPN is down if I run
ifconfig | grep "tun0"
If that returns something, VPN is up. If my network interface called tun0 is missing, VPN is not up.
Hope this gets you moving in the right direction!

How to programmatically check for connection?

In Linux (Ubuntu), I want to programmatically check if there is Internet connection (or if eth0 is connected).
I'm doing this because I am writing a program that requires network connection on a system that is highly prone to lose connection.
So I was thinking maybe a script that I can run periodically to check.
Can you give me good suggestions?
Here is a quick script that will accomplish what you want:
EMAIL=youremail#something.com
ping -c 5 8.8.8.8 >> /dev/null
if [ $? -eq 0 ]
then
echo "Able to reach internet!" | mail $EMAIL
else
echo "Unable to reach internet!" | mail $EMAIL
fi
Obviosly you can change the mail to something else to do depending on what your goal is
EDIT: to explain, this pings googles dns server to ensure you are connected and sends you an email one way or the other. The email part on failure will only work of course if you have a local email server on your network.
/sbin/ifconfig would be an excellent "get adapter status" command to script.
cron would be an excellent way to execute the script.
I also suggest to ping or perhaps wget some distant server (preferably the one you want to connect to). The network could work well on the local campus, but not well on intercontinental connections (e.g. because some cables has been cut).

pppd connection with GSM/GPRS modem, re-dialing after a few times

I'm currently connecting my GSM/GPRS modem HUAWEI e1550 to my ISP's network. I'm able to get it connected, but after disconnecting and connecting a few times I'm not able to do it anymore, and my modem starts not to answer, and I don't no why!
I connect through pppd call tmn-3g <- my ISP conf ppp file
This is my /etc/ppp/chat file:
ABORT BUSY
ABORT 'NO CARRIER'
ABORT ERROR
REPORT CONNECT
TIMEOUT 10
"" "ATZ"
OK "AT&F"
OK 'AT+CGDCONT=1,"IP","internet"'
SAY "Calling...\n"
TIMEOUT 120
OK "ATD*99***1#"
CONNECT \c
And here is my /etc/ppp/peers/tmn-3g
/dev/ttyUSB0
460800
lock
crtscts
modem
noauth
defaultroute
user tmn
password tmn
connect "/usr/sbin/chat -V -f /etc/ppp/chat-tmn-3g"
noipdefault
usepeerdns
nobsdcomp
novj
Can you help me with this?
Is it possible to restart my usb ports so that I can restart all the process?I though this could be a solution..
Or is there any other way to do it?
tks in advance..
This is more a comment than an answer (don't have comment privilege on stackoverflow...)
Seems this thread is about a similar issue.
I had a somewhat similar one as well, maybe this can help.

Resources