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
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.
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".
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.