Trying to either send a file by reference or somehow append a file on a remote server.
Using scp which just sends a copy of the file right now. My code doesn't make sense since I'm sending a copy of a file, but I can't do anything with it, because it doesn't change the original file.
while read p;do
scp sample_file.txt $p"./home/user"
#Log onto remote server
#Get last entry of directory and put it in sample_file.txt
done <usernames_list.txt
Basically I want list_of_stuff.txt to look like
entry1
entry2
entry3
entry4
...etc
Does anyone know how to send the actual file (instead of scp which just sends a secure copy) to a remote server in UNIX? Or does anyone know how to append on a remote server?
From the sound of things, you don't need to copy the file to the remote server at all! You only need the output from running some commands on that server to append it to a file, so you can do that locally by piping the output from ssh:
while read p; do
ssh $p "get_last_entry_of_directory" >> sample_file.txt
done < usernames_list.txt
I've dummied out the command get_last_entry_of_directory because I'm not sure what's supposed to be involved there.
Related
I need to upload a file from source unix server to destination unix server (supports sftp). i'm using simple script below:-
cd /usr/bin
sftp userid#destination_server <<EOF
put myfile /
EOF
I get host key verification failed, Couldn't read packet: Connection reset by peer
I know this must have got something to do with correct public ssh key of my source being not set under destination server. But otherwise , is my script correct. Or do you suggest any other script based on my simple requirement stated above. Please note this doesn't need any password, just user name is sufficient and remote directory is just the root directory, hence using /.
Simply use a SFTP batch file:
sftp -b batchfile.sftp userid#destination_server
with batchfile.sftp containing exactly one line (or whatever more commands you should need)
put myfile /
In my work, I use 2 Linux servers.
The first one is used for web-crawling and create it as a text file.
The other one is used for analyzing the text file from the web crawler.
So the issue is that when a text file is created on web-crawling server,
it needs to be transferred automatically on the analysis server.
I've used shell programming guides referring some tips,
and set up the crawling server to be able to execute the scp command without requiring the password (By using ssh-keygen command, Add ssh-key on authorized_keys file located in /root/.ssh directory)
But I cannot figure out how to programmatically transfer the file when it is created.
My job position is just data analyze (Not programming)
So, the lack of background programming knowledge is my big concern
If there is a way to trigger the scp to copy the file when it is created, please let me know.
You could use inotifywait to monitor the directory and run a command every time a file is created in the directory. In this case, you would fire off the scp command. IF you have it set up to not prompt for the password, you should be all set.
inotifywait -mrq -e CREATE --format %w%f /path/to/dir | while read FILE; do scp "$FILE"analysis_server:/path/on/anaylsis/server/; done
You can find out more about inotifywait at http://techarena51.com/index.php/inotify-tools-example/
I have created a very small script below which i want help me to move files from one server to other server periodically via cronjob.
#!/bin/sh
HOST='1.1.1.1'
FILE='EndpointUsage*.*'
PASS='password#'
sftp kingadmin#$HOST
password $PASS <<END_SCRIPT
binary
lcd /var/tmp/
mput $FILE
quit
END_SCRIPT
Problem i am facing.
1) I need this script to give the password automatically, i do not want to give password manually whenever this script run. Currently when i ran the commands its asking for password as below.
LA:/var/tmp # ./portmove.sh
kingadmin#1.1.1.1's password:
2) I want to send the files to particular directory on remote server. Can you please help how to put the locations in the script so that my script can send the files to particular directory let say in every 10 minutes(which i can configure in cronjob)
Thanks you in advance.
Instead of using a password, consider using a public/private key pair.
You can then specify the key file instead of a password.
I need to upload a whole folder to SFTP server. I see one way only - via sftp prompt. So I execute command
sftp> put /var/sites/c/public_html/wp-content/uploads/* /wp-content/uploads/
but I get
skipping non-regular file /var/sites/c/public_html/wp-content/uploads/2010
and no files copying. what is need to do to achieve my goal, upload whole folder(subfolders and files) to the SFTP server.
put is used to upload a single file
to upload multiple files use mput
if that doesn't work try switching to scp instead of sftp
put supports the -r switch on my machine (I'm using OpenSSH_6.4p1, OpenSSL 1.0.1e 11 Feb 2013). If your sftp doesn't support -r you can also use scp. This should work as both sftp and scp use ssh to push files to the remote side, and scp is able to push files recursive on almost every system I've seen so far.
Can we ftp specific files from a directory. And these specific files that needs to be transferred will be specified in config file.
Can we use a for loop once logged into ftp (in a script) for this purpose.
Will a normal ftp work when transferring files from Unix to win ftp server.
Thanks,
Ravi
You can use straight shell. This assumes your login directory is /home/ravi
Try this one time only:
echo "machine serverB user ravi password ravipasswd" > /home/ravi/.netrc
chmod 600 /home/ravi/.netrc
test that .netrc works - ftp serverB should log you straight in.
Shell script that reads config.file, which is just a list of files to send
while read fname
do
ftp serverB <<EOF
get $fname
bye
EOF # leave the EOF in column #1 of the script file
done < config.file
This gets file from serverB. Change get $fname to put $fname to send files from serverA to serverB
That certainly is possible. You can transfeer files listed in some file by implementing a script using an ftp client (buildin or via calling a cli client). The protocol is system independant, therefore it is possible to transfer files between systems running different operating systems. There is only one catch: remember that MS-Windows uses a case insensitive file system, other systems differ in that.