How can we open minicom from current terminal and pass multiple commands to execute and exit to 1st terminal using shell script - linux

I am trying to write a shell script that creates a new window and run a minicom terminal (connected to /dev/ttyACM0) in that.
Here's the script file my_script.sh:
#!/bin/bash
gnome-terminal --command minicom
echo "\n" >> /dev/ttyACM0
sleep 5
echo "\n" >> /dev/ttyACM0
echo "run x_boot" >> /dev/ttyACM0
sleep 5
echo "root" >> /dev/ttyACM0
sleep 3
echo "cd /tmp" >> /dev/ttyACM0
sleep 1
In the above code all the echo commands i'm passing directly to the device file not to the minicom terminal.
Requirements:
Now I need to send command1 to minicom
Make the terminal to sleep for 5 sec before sending next command
Send Command2
Wait again for 5sec.
And many commands automated
After that exit the terminal without closing minicom
Please help me with this.

Use minicom scripting (runscript) instead of bash echoes. It has both send and sleep commands:
-S, --script=SCRIPT : run SCRIPT at startup

Related

Calling a script from another script doesn´t execute all the commands when SSH

I am trying to call a script from another script because I am using "spawn" (from expect packet) to execute an SSH to another machine. Basically, after executing a comparison and routine in my main script (scriptA.sh) it calls for another script (scriptB.sh). The calls works fine, as I can see that the SSH is correctly executed. However, something looks wrong as not all the commands are executed correctly. If I execute those commands manually it works.
I have running an application on a machine that sometimes gets frozen and shows totally white screen. scriptB.sh kills firefox and re-open the broswer on background. The commands works fine if i execute them manually:
sudo killall firefox
export DISPLAY=:0.0 (I don´t need "sudo" for this)
/usr/local/bin/run_digital_signage_firefox.sh& (This command must be running without "sudo")
This is a very small resume from my scriptA.sh (I execute this script like this: sudo ./scriptA.sh):
#!/bin/bash
...something....
export username
export password
export IP
sh /home/mydirectory/scriptB.sh
...something....
and this is my scriptB.sh (I pass the username, password and IP using "export" in my scriptA.sh):
#!/bin/bash
/usr/bin/expect << EOF
spawn ssh test#$IP "sudo killall firefox && export DISPLAY=:0.0 && /usr/local/bin/run_digital_signage_firefox.sh&"
sleep 3
expect "*?ame:*" {
send "$username\r"
sleep 2
expect "*?assword:*"
send "$password\r"
sleep 2
expect "\r"
sleep 2
}
expect "*?(yes/no)*" {
send "yes\r"
sleep 2
expect "*?ame:*"
send "$username\r"
sleep 2
expect "*?assword:*"
send "$password\r"
expect "\r"
sleep 2
}
EOF
When scriptA.sh calls scriptB.sh it seems that it gets to kill firefox browser but not to initialize this with the command "/usr/local/bin/run_digital_signage_firefox.sh&". I must run this command without sudo but even if i try to add "sudo /usr/local/bin/run_digital_signage_firefox.sh&" in the SSH in scriptB.sh it does not work as well. It seems that the script is not executing "/usr/local/bin/run_digital_signage_firefox.sh&" neither "sudo /usr/local/bin/run_digital_signage_firefox.sh&". I need to execute the last part of the script to open again the firefox browser.
Both scripts have rwx permissions in ugo.

How to execute a local bash script on remote server via ssh with nohup

I can run a local script on a remote server using the -s option, like so:
# run a local script without nohup...
ssh $SSH_USER#$SSH_HOST "bash -s" < myLocalScript.sh;
And I can run a remote script using nohup, like so:
# run a script on server with nohup...
ssh $SSH_USER#$SSH_HOST "nohup bash myRemoteScript.sh > results.out 2>&1 &"
But can I run my local script with nohup on the remote server? I expect the script to take many hours to complete so I need something like nohup. I know I can copy the script to the server and execute it but then I have to make sure I delete it once the script is complete, would rather not have to do that if possible.
I've tried the following but it's not working:
# run a local script without nohup...
ssh $SSH_USER#$SSH_HOST "nohup bash -s > results.out 2>&1 &" < myLocalScript.sh;
You shouldn't have to do anything special - Once you kick off a script on another machine, it should finish running even if you terminate the connection:
For example
ssh $SSH_USER#$SSH_HOST "bash -s > results.out 2>&1" < myLocalScript.sh &
# Please wait a few seconds for the connection to be established
kill $! # Optional: Kill the last process
If you want to test it, try a simple script like this
# myLocalScript.sh
echo 'File created - sleeping'
sleep 30
echo 'Finally done!'
The results.out file should immediately be created on the other machine with "File created - sleeping" in it. You can actually kill the local ssh process with kill <your_pid>, and it will still keep running on the other machine, and after 30 seconds, print "Finally done!" into the file, and exit.

Output of bash script is going to another putty session

I have a script as below, when I am running this script in the 1st putty session. The output of script is displayed on the 1st putty session. If I open another (2nd) putty session and run the same script on 2nd putty session, still the output goes to the old putty (1st) session.
echo "*******Output command $args starts ***********"
pf=`oprmsg 'VARY CN(*),ACTIVATE'`
echo $pf
sleep 1
echo `oprmsg "$#"`
sleep 7
echo `oprmsg 'VARY CN(*),DEACTIVATE'`
sleep 1
echo "******* Output of command $args ends *************"
any idea why?

Why this Debian-Linux autostart netcat script won't autostart?

I placed a link to my scripts in the rc.local to autostart it on linux debian boot. It starts and then stops at the while loop. It's a netcat script that listens permantently on port 4001.
echo "Start"
while read -r line
do
#some stuff to do
done < <(nc -l -p 4001)
When I start this script as root with command ./myscript it works 100% correctly. Need nc (netcat) root level access or something else?
EDIT:
rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/etc/samba/SQLScripts
exit 0
rc.local starts my script "SQLScripts"
SQLScripts
#! /bin/sh
# The following part always gets executed.
echo "Starting SQL Scripts" >> /var/log/SQLScriptsStart
/etc/samba/PLCCheck >> /var/log/PLCCheck &
"SQLScripts" starts "PLCCheck" (for example only one)
PLCCheck
#!/bin/bash
echo "before SLEEP" >> /var/log/PLCCheck
sleep 5
echo "after SLEEP" >> /var/log/PLCCheck
echo "vor While" >> /var/log/PLCCheck
while read -r line
do
echo "in While" >> /var/log/PLCCheck
done < <(netcat -u -l -p 6001)
In an rc script you have root level access by default. What does "it stops at the while loop" mean? It quits after a while, or so? I guess you need to run your loop in the background in order to achieve functionality usual in autostart scripts:
echo "Starting"
( while read -r line
do
#some stuff to do
done << (nc -l -p 4001) ) &
echo "Started with pid $( jobs -p )"
I have tested yersterday approximatly the same things, and I have discover that you can bypass the system and execute your netcat script with the following crontask. :
(every minute, but you can ajust that as you want.)
* * * * * /home/kali/script-netcat.sh // working for me
#reboot /home/kali/script-netcat.sh // this is blocked by the system.
According to me, I think that by default debian (and maybe others linux distrib) block every script that try to execute a netcat command.

Running Shell Script after boot on Raspberry PI

I'm making a web kiosk display board using a raspberry pi and I want to send some key strokes to the browser window 2 minutes after it's loaded. The script sends the logon details for a webserver.
I've got a script that sends the keystrokes which works fine from the telnet console:
#!/usr/bash
username="username"
password="password"
echo "Setting Display"
export DISPLAY=:0
echo "Sending Username"
for char in $(sed -E s/'(.)'/'\1 '/g <<<"$username"); do
xdotool key $char
done
xdotool key Tab
echo "Sending Password"
for char in $(sed -E s/'(.)'/'\1 '/g <<<"$password"); do
xdotool key $char
done
xdotool key Return
echo "Waiting 5 Seconds"
sleep 5
echo "Setting Remember Password"
xdotool key Tab
xdotool key Tab
xdotool key Return
echo "Finished"
I've tried to add bash /home/pi/logon.sh to the rc.local file - but it doesn't send the keystrokes to the browser?
Does any one know why that would be? As I say - it works fine from the telnet window if I run the script, but it doesn't work when run from boot.
I had sleep 120 on the line before it to stop if firing right away and wait until the browser has loaded - and I know the script is running from rc.local, because when I remove the sleep command, I see the echos from the script.
Any ideas?
The reason it wasn't working was because the script needed to be run as the user pi.
I changed the code in the rc.local script to this: su - pi -c "bash /home/pi/logon.sh &"
This makes the script run as the user pi and the ampersand is used to make the script run separate to the rc.local script by forking it. (http://hacktux.com/bash/ampersand)
Put this in your crontab
#reboot /path/to/script
Edit it using
#crontab -e

Resources