Bash shell commanding to vpn connection from command line - linux

I am trying to write a bash shell (very first time) so that I can auto connect to a VPN server. I have never written a bash script before. can anyone tell me what command syntax I need for this?
#!/bin/bash
echo "Hi would you like to connect to vpn labs? Yes or No"
sleep 2
read answer
$connect = "rdesktop -u offsec -p ******** 192.168.***.***"
if $ answer != "No" then $connect
else
exit

For the right command to connect vpn, read the following. https://askubuntu.com/questions/57339/connect-disconnect-from-vpn-from-the-command-line.
First, run this in the terminal and see if it works. rdesktop -u offsec -p ******** 192.168.***.***"
Then you want to watch your syntax for your bash script.
#!/usr/bin/env bash
read -e -p "Hi would you like to connect to vpn labs? Yes or No " answer
sleep 2
if [ $answer != "No" ];
then rdesktop -u offsec -p ******** 192.168.***.***
else
exit
fi

I'd advise that you:
Learn the syntax of the main shell constructs (if-statement in
your case). You may use man bash and help if for that.
run each line in the shell and see if it's properly written or not, and
whether it does what you want or not.
Quick observations:
There is no '$' when you are setting the variable. It's used
when you reference the variable.
There is no space between $ and the variable name.

Related

How to connect to SQLPLUS and run SQL script within ssh unix?

I have the following bash script:
#!/bin/bash
set command "sqlplus -s user/password#tns"
ssh -t test#192.168.94.139 $command
Now, I want to run te following sql script (which is on the other device I'm accessing):
/usr/mikael/myfile.sql
How is the best practice to run the script from that path?
I've seen a suggestion to add "/usr/mikael/myfile.sql" to the end of the command, as :
set command "sqlplus -s user/password#tns /usr/mikael/myfile.sql"
Is that really good? (I'm working on a prod environment and don't want to mess with it).
Thanks.
set does not do what you think it does. Use
command="sqlplus -s user/password#tns #/usr/mikael/myfile.sql"

Is there a way to automatically answer for user prompt when doing ssh in a shell script without using expect or spawn?

I'm trying to test ssh trust between a linux box against 12 other linux boxes using a shell script and I'm trying to pass user input as 'yes' for the question below automatically.
Are you sure you want to continue connecting (yes/no)?
but the script is failing with error 'Host key verification failed'. I manually executed the ssh command with << EOT on one of the server but the I still get user prompt question. Is there any-other way to pass input value for user prompts automatically while running ssh command?
Note: I cannot use spawn or except do you some system limitation and I cannot install them due to organisations access restrictions.
I tried with the following options but none of them worked for me
[command] << [EOT, EOL, EOF]
echo 'yes'
[EOT, EOL, EOF]
yes | ./script.sh
printf "yes" | ./script.sh
echo "yes" | ./script.sh
./script.sh 'read -p "Are you sure you want to continue connecting (yes/no)?";echo "yes"'
sh```
for server in `cat server_list` ; do
UPPER_MACHINE_NAME=`echo $server | cut -d '.' -f 1`
UPPER_MACHINE_NAME=${UPPER_MACHINE_NAME^^}
ssh -tt user#$UPPER_MACHINE_NAME << EOT
echo 'yes'
touch /usr/Finastra/sshtest.txt
EOT
done
```

Read command in bash script not waiting for user input when piped to bash?

Here is what I'm entering in Terminal:
curl --silent https://raw.githubusercontent.com/githubUser/repoName/master/installer.sh | bash
The WordPress installing bash script contains a "read password" command that is supposed to wait for users to input their MySQL password. But, for some reason, that doesn't happen when I run it with the "curl githubURL | bash" command. When I download the script via wget and run it via "sh installer.sh", it works fine.
What could be the cause of this? Any help is appreciated!
If you want to run a script on a remote server without saving it locally, you can try this.
#!/bin/bash
RunThis=$(lynx -dump http://127.0.0.1/example.sh)
if [ $? = 0 ] ; then
bash -c "$RunThis"
else
echo "There was a problem downloading the script"
exit 1
fi
In order to test it, I wrote an example.sh:
#!/bin/bash
# File /var/www/example.sh
echo "Example read:"
read line
echo "You typed: $line"
When I run Script.sh, the output looks like this.
$ ./Script.sh
Example read:
Hello World!
You typed: Hello World!
Unless you absolutely trust the remote scripts, I would avoid doing this without examining it before executing.
It wouldn't stop for read:
As when you are piping in a way you are forking a child which has been given input from parent shell.
You cannot give the values back to parent(modify parent's env) from child.
and through out this process you are always in parent process.

How to get standard output from subshell?

I have a script like this?
command='scp xxx 192.168.1.23:/tmp'
su - nobody -c "$command"
The main shell didn't print any info.
How can I get output from the sub command?
You can get all of its output by just redirecting the corresponding output channel:
command='scp ... '
su - nobody -c "$command" > file
or
var=$(su - nobody -c "$command")
But if you don't see anything, maybe the diagnostics output of scp is disabled?
Is there a "-q" option somewhere in your real command?
You aren't actually running the scp. When you use the
VAR=value cmd ...
syntax, the VAR=value setting goes into the environment of cmd but it's not available in the current shell. The command after your -c is empty, or the previous value of $command if there was one.

How to emit a "beep" on my computer while running a script on a remote machine?

I run a long script on a remote machine and I would like to hear a beep when the script ends. On my machine I can add at the end of the script:
echo -e '\a' > /dev/console
but this is not working on the remote machine which complains :
-bash: /dev/console: Permission denied
How to achieve this ?
You could run the script by passing it as a parameter to ssh and then echo the beep locally:
ssh user#host /path/to/script; echo -e '\a' > /dev/console
Perhaps you might use /dev/tty instead of /dev/console. (I don't know how ssh handle beeps, so maybe you should start a terminal emulator, e.g. ssh -X -f remotehost xterm).

Resources