How to store string into remote file? - shell

How to store string into remote file ?
I need to override content of file on remote machine (I have username, password and ip and can access through ssh) with some string from command line.
How to achieve this on linux ?

You can modify the file in one operation with ssh:
ssh user#host "echo \"$local_variable\" > /path/to/file"
However, this is risky - What if there's a double quote character in the local variable? In Bash, you can get around it by quoting the value:
ssh user#host "echo $(printf %q "$local_variable") > /path/to/file"
The much simpler and safer way to do this, and avoid any weird escaping problems, is to simply save the contents to a file locally and then copy it over with scp or rsync.

If you have access to ssh you can execute any command on the remote machine:
ssh username#remotehost.com /usr/bin/mycommand
(For example you could echo a string into a file if you like)
if you want to get rid of the password prompt, you could use SSH Key Authentication:
ssh-keygen -t dsa
scp .ssh/id_rsa.pub username#remotehost.com:~/.ssh/authorized_keys2
Once your key is on the remote machine, you can use ssh without password. (Warning: Using a key which is not password protected, could be risky)

Related

How to run commands on a remote host with an existing user on that machine

I am writing a shell scripts that requires to run commands on multiple remote host based on condition it satisfies. how can i run the commands on the remote host using its key pair and assign the output of those commands to a variable on the primary host.
I tried using
sshpass -f keypair hostname 'command' | 'commands'
but this command executed on the host itself.
Use command substitution to assign the output of a command to a variable. You also need to run the ssh command and specify the remote username.
variable=$(sshpass -f keypair ssh username#hostname 'command')
1) Add host public keys (~/.ssh/id_rsa.pub)to remote user authenticated keys (~/.ssh/authorized_keys)
You will append the content of the public key to the authorized_keys
2) If you don't have this key you may generate it
ssh-keygen -t rsa
3) after that you can
ssh <REMOTE USER NAME>#RemoteHOST <command>
As example
ssh mebada#123.44.2.12 ls
4) If you have list of commands you may scp shell file and execute it from there ( without password too ) and it will override the remote
scp /path/to/shell.file user#host:/path/to/remote
ssh user#host chmod u+x /path/to/remote/shell.file
ssh user#host /path/to/remote/shell.file

How to connect the ssh server using the ssh passphrase using shell script

We are writing the automatic script to connect my servers using the shell script. We do not have the password for the server. We have only public ssh passphrase. We have tried passphrase instead of the password in this comment but it says permission denied sshpass -p password ssh user#host. Is there any way to connect the server through public passphrase in the shell script?
If you want to use a private key automatically, just remove the passphrase.
To remove a passphrase, you can use:
ssh-keygen -p -P passphrasse -N "" -f keyfile
Though sshpass should work too, while it makes a little sense using it for this purpose.

using ssh keys in bash script

I've setup ssh keys form server A to server B and I can login to server B without a password. I'm trying to setup a reverse ssh tunnel in a bash script. From the command line if I do
ssh -N -R 1234:localhost:22 user#mydomain.co.uk -p 22
form server A it works as expected i.e no password required, however if I use it in a script
#!/bin/bash
/usr/bin/ssh -N -R 1234:localhost:22 user#mydomain.co.uk -p 22
I get asked for the password
user#mydomain.co.uk's password:
How do I make it so it uses the keys?
You need to let ssh know where it should search for the keys, if they are not in standard location and not passphrase protected. The easiest thing is by specifying -i switch directly to ssh:
/usr/bin/ssh -i /path/to/key -N -R 1234:localhost:22 user#mydomain.co.uk -p 22
Or cleaner way in your ~/.ssh/config like this:
Host mydomain.co.uk
IdentityFile /path/to/key
But make sure the script is run with your user context, so the script will see the configuration file.
If you have keys in standard location (~/.ssh/id_rsa), your code should work just fine. Although it should work if you have your keys stored in ssh-agent, which you can verify using ssh-add -L before starting the script. ssh-agent also solve the problem, if he keys are passphrase protected.

Bash Script : Execute unix commands inside a remote server

I am trying to login to Server B from Server A and perform simple UNIX commands on Server B using a shell script. The code is as follows. But ls -al is displaying the result of Server A and not the one that is logged on to i.e Server B. Any inputs are highly appreciated. Thanks
#!/bin/bash
clear
sshpass -p password ssh hostname
ls -al
exit
When the shell interprets a script file, it creates a child process to
execute each command line. So, the command lines after sshpass -p password ssh hostname are not actually executed inside the ssh
session to hostname, but in the host where the bash instance is
running.
To achieve what you want, you can check ssh(1) usage line and note that there is a [command] argument, that says:
If command is specified, it is executed on the remote host instead
of a login shell.
So, one way to do it is sshpass -p password ssh hostname ls -la. Another way which can provide some more flexibility is:
#!/bin/bash
clear
cat | sshpass -p password ssh hostname <<EOF
ls -la
EOF
Which would make ssh start a login shell in the remote host and pass
to its stdin the lines provided in the Here Document. The remote
shell would then interpret those strings as commands and execute them.
If you just want to run ls -al on the remote server, put it on the same line as the ssh command like
sshpass -p password ssh hostname ls -al
it will automatically exit when it gets to the end of the command so you don't need to put exit
Also, if you're going to be doing this and don't want to interactively enter the password, you might want to look at sharing public/private keys and using that so it won't ever ask for a password (unless you password protect your private key)

how to pass password to sftp connection in shell script

I have an sftp connection to a server in Unix.
Without password, I use the syntax to connect and execute command
sftp -b $user#$server_name
Can anyone suggest me how can I write a shell script to connect a remote server non interactively using a password
Try with this below option,
lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
You could use ~/.ssh/config file.
#
# ~/.ssh/config
#
Host servername
Hostname 127.127.127.127
Port 22
User root
#EOF: config
Then simply connect with "ssh servername" and if you don't want to use password you can use SSH key. Here is good tutorial on how to do that > http://www.cyberciti.biz/tips/linux-multiple-ssh-key-based-authentication.html
If you just want to pass user/server from terminal, you can do this.
#!/bin/bash
sftp -b "$1"#"$2"
then use it like this './sftp.sh user server'
use SCP like this;
scp -P 22 user#server:/dir/file.tgz ~/Desktop/
use SFTP like this;
sftp user#server:/dir/file.tgz ~/Desktop/file.tgz
You can also try this;
sftp user#host <<EOF
get /dir/file.tgz
rm /dir/file.tgz
EOF
The best way to do this would be to create a key pair on the client, and add the key to the target user's ~/.ssh/authorized_keys.
To create a key pair, run ssh-keygen and when it asks for a password, just hit return to indicate "no password". Then either run ssh-copy-id $user#$server_name or manually create a ~/.ssh/authorized_keys on the server and copy the contents of the ~/.ssh/id_rsa.pub from the client into it (ssh-copy-id isn't available on all machines, so on some you'll have to do it manually).
Now you should be able to run ssh or scp without a password, as it should use your key instead. If it doesn't work, make sure that the permissions on your ~/.ssh/ directory and contents are correct on both machines; the directory should be 0700 (drwx------) and the files should be 600 (-rw-------). Also check that key authentication is enabled on both the client and the server.

Resources