Transfer and execute script from client, on router

Hi,

I try to make a script to work with pc and router, kind of script in a script.

want to scp to router to send a script in a folder, ssh to router and run the script. Unfortunately, after scp and ssh to router, I dont know how to proceed, my script is ending after ssh, so I guess I need another script?

here the main script from my pc. (the main script send another script to my router )

#!/bin/bash
#
# create /bin folder on router
ssh root@192.168.1.1
mkdir /root/bin
exit

# Scp to router (vpn script)
scp /media/perkel/D/ovpnconfig.sh root@192.168.1.1:/root/bin/
#
# Add permission and start script
ssh root@192.168.1.1
cd /root/bin
chmod +x ovpnconfig.sh
./ovpnconfig.sh

I would need your help.

thanks

1 Like

Why are you scripting this from your PC? Why not just ssh into your router and run it there?

Just need another blank line after

./ovpnconfig.sh

Or is it not executing the prior commands after entering the router?

No, it shouldn't (unless the script has been run previously).
/root/ is actually the root user's home directory (equivalent of ~/)

The typical binary directories are

/bin
/sbin
/usr/bin
/usr/sbin
1 Like

That is not /root/

/root/ is here:

Don't believe me?? ssh into your router and then issue the following commands:

cd /root/
ls
2 Likes

Really? Over this?

In this case, there is a very significant difference between / and /root/ (or /bin and /root/bin), but I guess we do live in a post-fact world, so you may ignore the truth if you so desire.

@PerkelSimon - please disregard the below statement from @anon89577378 as it is incorrect:

6 Likes

When you execute "SSH" from a script, the script opens a remote shell, that will wait for commands. But the next lines on your script are not going to be sent to that shell, they are going to be execute locally, when the remote shell (eventually) ends.

What you need is to tell SSH the commands you want to execute remotely, like "ssh user@device command".

5 Likes
#!/bin/bash
#
# create /bin folder on router
ssh root@192.168.1.1 mkdir -p /root/bin

# Scp to router (vpn script)
scp /media/perkel/D/ovpnconfig.sh root@192.168.1.1:/root/bin/

#
# Add permission and start script
ssh root@192.168.1.1 "chmod +x /root/bin/ovpnconfig.sh; /root/bin/ovpnconfig.sh"
5 Likes

I could, but I want to do all steps from one script.

it is one script, just making several connections to the target.

1 Like

I will try.
Thanks everyone.

You re right

I have to create it

Thanks.

But after mkdir command, I thought I needed to do exit, cause scp is from my PC not from router....?

Run logread -f and start the script. You will see that the connection is closed after each execution of the commands.

2 Likes

so it would be about the same if I want to scp a build to router and syupgrade?

ie:

#!/bin/bash
#
# scp build to router
scp /media/perkel/D/WRT/openwrt/bin/targets/mvebu/cortexa9/openwrt-snapshot-r18769+8-07eccc29ab-mvebu-cortexa9-linksys_wrt3200acm-squashfs-sysupgrade.bin root@192.168.1.1:/tmp/
#
# ssh sysupgrade
ssh root@192.168.1.1 "cd/tmp; sysupgrade -v /tmp/openwrt-snapshot-r18769+8-07eccc29ab-mvebu-cortexa9-linksys_wrt3200acm-squashfs-sysupgrade.bin"

EDIT: I tried it, it works, but cd/tmp ; not found

cd /tmp
4 Likes

arrrrrr.. sorry, my mistake, typo

Hi,

just to chime in on this giving a different solution. You can also use a HERE document (heredoc) to send all your commands to a bash shell on the remote SSH server. That's more like your "script in a script" requirement:

Basic structure looks like this:

ssh server1 /bin/bash << 'HERE'
 command1              # this is basically your
 command2              # script in a script.
HERE

Everything between HERE and HERE is going to be fed into your SSH client as is. So your script would look something like this:

#!/bin/bash
#
# create /bin folder on router
ssh root@192.168.1.1 /bin/bash << 'HERE'
mkdir /root/bin
exit
HERE

BTW, you can replace HERE with any word you like that's not part of your script.

Good explanation is here: https://stackoverflow.com/questions/7114990/pseudo-terminal-will-not-be-allocated-because-stdin-is-not-a-terminal (Look for Dejay Clayton answer).

Regards
BtflHmstr

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.