ssh text into file on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh text into file on remote server
# 1  
Old 02-08-2009
ssh text into file on remote server

Hello,

I have about 90 servers that I need to update snmp configs. I am trying to write a script that will echo 4 new lines of text into the snmpd.conf file. I have tested it locally and it works when the server ssh into itself but when I try to run the script to ssh into a remote server it logs into the server but does not echo the text into the config file.

I would like to know if its possible to modify a file on a remote server via ssh?

Thanks
# 2  
Old 02-08-2009
Quote:
Originally Posted by mr_dthomas
Hello,

I have about 90 servers that I need to update snmp configs. I am trying to write a script that will echo 4 new lines of text into the snmpd.conf file. I have tested it locally and it works when the server ssh into itself but when I try to run the script to ssh into a remote server it logs into the server but does not echo the text into the config file.

What did you try?

There should be no difference between transferring locally and to a remote machine. You may, of course, have to specify a different path.
Quote:

I would like to know if its possible to modify a file on a remote server via ssh?

Yes. I transfer three web sites that way.
# 3  
Old 02-08-2009
Here is my test script

Code:
#!/bin/bash
#The SERVERS variable cats a list of servers
SERVERS=`cat servers`;
for i in $SERVERS;
do
        ssh root@${i} echo "Server1" >>file
done

This will write to a file on the local machine running the ssh command not the remote host...
# 4  
Old 02-08-2009
After reading this article I corrected my syntax
How To Append File Using SSH

This line:
ssh root@${i} echo "Server1" >>file

Should look like this:
cat file | ssh root@${i} "cat >> file"

#!/bin/bash
#The SERVERS variable cats a list of servers
SERVERS=`cat servers`;
for i in $SERVERS;
do
cat file | ssh root@${i} "cat >> file"
done

I create a local file called "file" that contained the information I need entered in the remote "file" file.

Thanks

Thanks
# 5  
Old 02-08-2009
Try this:

Code:
ssh root@${i} 'echo "Server1" >>file'

The single quotes "protect" the command and its redirection so that it is not evaluated by the local shell.
# 6  
Old 02-09-2009
If you are ssh'ing between hosts and NOT changing users (ie: you are root and connecting to the next host as root as well), you need not specify "ssh root@host". Insteady, use "ssh host" (reducing one piece of complexity).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to check via SSH and credentials if file on remote server exists?

Hi there, I am sorry to ask that kind of beginner thing, but all the code I found online didnt work for me. All I want to do is: Check via SSH if a File exists on my webserver. The SSH login has to be with username and password. So I would be very thankful if somebody could write the line.... (8 Replies)
Discussion started by: Jens885544
8 Replies

2. Shell Programming and Scripting

Copying a text file from my Ubuntu Windows Sub System to a remote instance through SSH

*Following questions involves use of YAML, BASH, SSH and Software called Ansible* I am trying to learn how to use a Linux environment (in my case a Ubuntu Windows Sub System) to copy a text file from my files to a remote instance (in this case Amazon Web Services) by connecting via SSH. I... (8 Replies)
Discussion started by: Suhaba
8 Replies

3. Shell Programming and Scripting

Multi server access through remote server using ssh

Team, Presently I have 5 ip address kept in ip_abc1 file, for each of the ip address listed, i need to login on each ipaddress one at a time and login as below for that specific ip address ssh -p 8101 karaf@<ip.address_for the specific ip address as logged in> password features:list... (4 Replies)
Discussion started by: whizkidash
4 Replies

4. Shell Programming and Scripting

Retrieving file size in a remote server using SSH

Hi, I have public and private keys and that's works fine for me. now I am sending files one by one on remote server and I want to check is file successfully delivered or not by comparing size of file on local machine and remote server using ‘stat -c%s'. Below operations need to be done on... (2 Replies)
Discussion started by: ketanraut
2 Replies

5. UNIX for Advanced & Expert Users

Unable to ssh to remote server

Hi, I have two SunOs sparc servers mac1 and mac2. I have exchanged keys between them inorder to passwordless login ssh from mac1 to mac2. However, it is failing after authentication. Part of the debug is as below. Please suggest whats wrong and how do i fix that!! Note: i do not have... (1 Reply)
Discussion started by: mohtashims
1 Replies

6. Shell Programming and Scripting

Script to ssh to remote server

Hi All, I need to prepare a script. Description: Currently i am in server "x(ubuntu os)", here i need to develop a script to ssh to another server "y(ubuntu os)", i have password less authentication to "y". i have done the below #!/bin/bash #ssh to the server "y" and confirming i am... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

7. Shell Programming and Scripting

ssh to remote server and check if file exists

Hi everyone, I am trying to figure out a way to ssh to remote server and check if file exists, and if it doesn't I want to leave the script with an exit status of 5. I have the following that I am attempting to use, but it is not returning anything: check() { ssh ${SOURCE_SERV} "ls -l... (4 Replies)
Discussion started by: jimbojames
4 Replies

8. Shell Programming and Scripting

Problem in appending text to a file located in remote server

ssh -q "server_name sudo echo 'dbagroup::1234' >> sudo /etc/group"if i execute the above code its not getting appended. my requirement is to login to remote server and append dbagroup::1234 in /etc/group i am able to achieve this with tee -a command want to know why its not working with >>... (5 Replies)
Discussion started by: chidori
5 Replies

9. Shell Programming and Scripting

Append Text To Remote Servers File Via SSH

Gentleman & Ladies, Please make me feel like and novice and explain why this is not working? I am attempting to ssh to a remote server via ssh and keys. I want to inject a file on the remote server with text. I am not achieving this. I would like to echo/inject the text on the remote... (1 Reply)
Discussion started by: abacus
1 Replies

10. Shell Programming and Scripting

SSH Remote Server issue

I have a script that connects to remote servers using a public key. Some of the servers are not set up for the public key and I receive the following when I attempt to ssh: The authenticity of host 'XXX' can't be established. RSA key fingerprint is... (1 Reply)
Discussion started by: la_womn
1 Replies
Login or Register to Ask a Question