How to exit script and return?

Hi,

I try a script for my config, I would like to have the most complete config but I need to exit to scp a file from m pc, but after that, I guess I need to run another script to complete the config?

here a part of my script;

#!/bin/sh
 
# === Update root password =====================
# Update the root password. 
# 
echo 'Updating root password'
NEWPASSWD=XXXXXXXXXXXX
passwd <<EOF
$NEWPASSWD
$NEWPASSWD
EOF

# === Set up the WAN (eth0) interface ==================
#
# echo 'Configuring WAN link DHCP'
uci set network.wan.metric=1024
uci set network.wan.peerdns=0
uci add_list network.wan.dns=192.168.1.1
uci commit network
ifup wan
echo 'Waiting for link to initialize'
sleep 20

# === Set the Time Zone ========================
# Set the time zone to non-default (other than UTC)
#
TIMEZONE='EST5EDT,M3.2.0,M11.1.0 '
ZONENAME='America/Montreal'
echo 'Setting timezone to' $TIMEZONE
uci set system.@system[0].timezone="$TIMEZONE"
echo 'Setting zone name to' $ZONENAME 
uci set system.@system[0].zonename="$ZONENAME"
uci commit system

# dnsmasq && Dnscript

cd /etc/dnscrypt-proxy2
rm dnscrypt-proxy.toml
exit
scp /run/media/perkel/dnscrypt-proxy.toml root@192.168.1.1:/etc/dnscrypt-proxy2/
ssh root@192.168.1.1

uci set dhcp.@dnsmasq[0].noresolv='1'
uci set dhcp.@dnsmasq[0].doh_backup_noresolv='-1'
uci set dhcp.@dnsmasq[0].server='127.0.0.53#53'
uci commit network

etc etc....... This is what I found to try to have my own toml file into router, but after I dont know what to do to continu the script, a second one ?

Is there a reason you need a script to add a custom dynscrypt-proxy.toml.

:wink:

It would be great to adding directly by script, if not, I will add it myself after...

A quick search of https://stackoverflow.com/questions/1346509/automate-scp-file-transfer-using-a-shell-script returns this among others.

password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>
1 Like

I wonder why after rm dnscrypt-proxy.toml the script stops, no exit and no scp ..? idea?

#!/bin/sh


cd /etc/dnscrypt-proxy2
rm dnscrypt-proxy.toml
exit
scp /run/media/perkel/dnscrypt-proxy.toml root@192.168.1.1:/etc/dnscrypt-proxy2/
#ssh root@192.168.1.1

Exit Without status does just that. Exits the script.

2 Likes

What are you actually trying to do?
Have a script in the router that runs in the router and does some UCI configuration and also copies a file from somewhere to the router?

Is the script included in your build?
If yes, why are you not including also the .toml file that you need.
You can include pretty much anything as "custom file".

2 Likes

Ok I see.

I tried that, better but still problem with exit

#!/bin/sh
#
ssh root@192.168.1.1 "cd /etc/dnscrypt-proxy2; rm dnscrypt-proxy.toml; exit 192.168.1.1; scp /run/media/perkel/dnscrypt-proxy.toml root@192.168.1.1:/etc/dnscrypt-proxy2/"

I tried exit 192.168.1.1

There is no that kind of "exit to some IP".
Exit just means "stop this script"

1 Like

I can add my custom .toml file into my custom open wrt build?
I know I can add it from scp :wink: but if not....

Sure.

Apparently you have not read the wiki...

Leading to

2 Likes

I will read it for sure, but if in the future I want to that script works, only that one with exit

Is is possible, exit the openwrt session?

#!/bin/sh
#
ssh root@192.168.1.1 "cd /etc/dnscrypt-proxy2; rm dnscrypt-proxy.toml; exit 192.168.1.1; scp /run/media/perkel/dnscrypt-proxy.toml root@192.168.1.1:/etc/dnscrypt-proxy2/"
1 Like

Yes, it is possible to exit the session; and that is exactly what is happening here... Now, why don't you just put the "scp" command before the "exit"?

2 Likes

yep I've put the scp after the exit command, but as the others have said, the exit command, only stop the script, I'm still in my ssh openwrt session after that, to scp I need to exit the openwrt shell session... not?

Sorry, i meant to put the "scp" after the "exit"; I have edited my message.

1 Like

because I take a file from computer to router, so I thought I would need to be out of router session?

You can't really temporarily "exit" a remote session to local host, execute a command (in the same script) in the local host, and then continue the same script running in the remote host...

(at least that is how I read your script in the first message...)

3 Likes

ok thank you, it is what I thought.
But if I can just leave the remote session, Do I need to do 2x exit, exit to leave script and exit again to leave remore session? To do scp after from computer

What I don't understand is that if the script is located at the router and you are runnign it there, why do you need to try running the scp at the local PC in the middle?

Why not just run the scp from the router? in style of
scp root@my_pc:/run/media/perkel/dnscrypt-proxy.toml /etc/dnscrypt-proxy2/

1 Like

Ok. scp root@my_pc... i did not know :wink:
Usually I only use scp PC to router or vice versa.
But now is not to send file from router to pc but router to copy File from PC to bring back to router.