Here is my code:
#!bin/bash
id=$(sshpass -p password ssh -tt username#ipaddress -p PORT "grep --include=\*.cr -rlw '/usr/local/bin/' -e '$1' | cut -c16-")
echo $id
sshpass -p password rsync -avHPe 'ssh -p PORT' username#ipaddress:/usr/local/bin/"$id" /usr/local/bin/
id echos correctly, but I get an rsync error when trying to call the variable.
If I manually populate and run rsync, the command works, so I'm not sure what is going on.
Rsync gives me the following output on error.
rsync: link_stat "/usr/local/bin/match.cr\#015" failed: No such file or directory (2)
It seems to be grabbing extra characters? Any help is appreciated :)
Looks like your file contains Windows specific "CR LF" characters. You need to convert these to Linux specific "LF" characters in your script. You can use a tool like dos2unix or Notepad++.
Related
Hi i created following script to initialize my storage box to use rsync without password later. Last year it works if i remember correct...
cat .ssh/id_rsa.pub >> .ssh/storagebox_authorized_keys
echo -e "mkdir .ssh \n chmod 700 .ssh \n put $.ssh/storagebox_authorized_keys" \
".ssh/authorized_keys \n chmod 600 .ssh/authorized_keys" | sshpass -p ${storage_password} \
sftp -P ${storage_port} -i .ssh/id_rsa ${storage_user}#${storage_address}
today I get following error:
sshpass: invalid option -- 'i'
but the parameter -i belongs to sftp and not sshpass - is there an possibility to parse the parameters in the correct way?
edit: i switched the position of
-i .ssh/id_rsa ${storage_user}#${storage_address}
and get this error
sshpass: Failed to run command: No such file or directory
edit: it seems like an sftp problem...
after discussion, updating answer to properly support automation
step 1:
create an sftp "batch file" e.g: ~/.ssh/storage-box_setup.sftp
mkdir .ssh
chmod 700 .ssh
put /path/to/authorized_keys_file ".ssh/authorized_keys
chmod 600 .ssh/authorized_keys
/path/to/authorized_keys_file is a file containing public keys of ONLY the keys that should have access to your storage box (.ssh/storagebox_authorized_keys)
step 2:
update automation script command to
sshpass -p <password> -- sftp -P <port> -b ~/.ssh/storage-box_setup.sftp user#host
the -b flag was the answer you needed.
refer: man sftp
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication.
--
sshpass -p ${storage_password} -- \
sftp -P ${storage_port} -i .ssh/id_rsa \
${storage_user}#${storage_address}
the -- before sftp is a way to tell sshpass (and most other programs) to stop parsing arguments.
everything after -- is assumed as the last argument, which in the case of sshpass is the command to be executed ssh -i ~/.id_rsa ...
in case you're wondering switching the position of -i tells sshpass to execute -i as a program and hence fails with command not found
I am trying to add # in front of a line if string gets a match.
String will be read by user.
Direct sed command well for me but when is user read command then it throws error:
server cust01-stg01-ins01-wfm13-bck-610358970-22.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-25.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-21.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-23.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-24.int.stg.mykronos.com:80 resolve max_fails=0;
Now suppose user want to comment line with matching character "cust01-stg01-ins01-wfm13-bck-610358970-22" then below output is expected
#server cust01-stg01-ins01-wfm13-bck-610358970-22.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-25.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-21.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-23.int.stg.mykronos.com:80 resolve max_fails=0;
server cust01-stg01-ins01-wfm13-bck-610358970-24.int.stg.mykronos.com:80 resolve max_fails=0;
sudo sed -i "/cust01-stg01-ins01-wfm13-bck-610358970-23/s/^/#/" wfm_backend_nginx.state
this works well for me but when I am trying below command then it is throwing error.
[root#cust01-stg01-ins01-wfm13-bck-ilb1-610358970 tmp]# cat ilb_commnet.sh
#!/bin/bash -xe
sudo read -p "enter host_name :" Host
sudo sed -i -e "/${Host}/s/^/#/" /tmp/wfm_backend_nginx.state
Output is :
sed: -e expression #1, char 0: no previous regular expression
sudo read -p "enter host_name :" Host
sudo runs a separate process, that will read the input and save it into Host. Then the process will exit, destroying the whole environment with the read value.
Remove sudo - read the value in current process.
Using sudo is completely crazy here. You are running read in a privileged subshell, which then exits and takes the variable with it before you can use it for anything.
... Actually sudo read is simply a syntax error because sudo can't run a shell built-in. It tries to look for /usr/bin/read, /bin/read etc but doesn't find anything.
bash$ sudo read -p hello
sudo: read: command not found
Even without the sudo, your code should probably examine the variable before attempting to pass it to a privileged command. Maybe something like
#!/bin/bash -xe
while true; do
read -p "Enter host name: " Host
case $Host in
*[!A-Za-z0-9]*) echo "$0: Invalid characters in host name" >&2;;
*) break
esac
done
sudo sed -i -e "/${Host}/s/^/#/" /tmp/wfm_backend_nginx.state
I am trying to find a way to connect to a list of servers written in a simple textfile to run one command and write the output to a file...
The small problem is, I have to login with a password... but it would not a problem to paste the password into the script.
the full command would be:
ssh "server_from_list.txt uptime | awk -F, '{sub(".*up ",x,$1);print $1}' >> /home/kauk2/uptime.out
lets assume the password is: abcd1234
Any suggestions??? I am not fit in scripting, sorry...
Many thanks to you all in advance...
regards,
Joerg
Ideally you should set up password-less login, but failing that you can use sshpass. First, get a single command working by trying the following:
export SSHPASS=abcd1234
Then you can try:
sshpass -e ssh user#server1 'uname -a'
When you get that debugged and working, you can use GNU Parallel to run the command on all servers in a file called list.txt
user#server1
user#server2
user#server3
user#server4
The command will be:
parallel -k -a list.txt sshpass -e ssh {} 'uptime'
Okay, so I've been struggling with this one for awhile. I'm trying to create a python3 script that automatically uploads a file to a server. In the commandline, the following command works like a charm:
sftp -i key.pem -P 3912 admin#myServerIP:home/files/ <<< $"put test.txt"
But, when I try to run that command in my python script (either through os.system('command') or subprocess.call('command')) I keep getting the following error:
/bin/sh: 1: Syntax error: redirection unexpected
After doing some research, I believe I found that I need to run the command in /bin/bash, instead of the default dash, but I've tried everything, and connot for the life of me figure out how to do that! I hope this is a simple, stupid fix, so I can be on my way. Thanks!
You can replace your command with this one:
/bin/bash -c 'sftp -i key.pem -P 3912 admin#myServerIP:home/files/ <<< $"put test.txt"'
which should work fine even in Dash. :-)
(Incidentally, I think you meant to write "put test.txt" instead of $"put test.txt". But that's neither here nor there.)
Edited to add: I should mention that your specific example is easy to rewrite in a Dash-compatible way:
echo "put test.txt" | sftp -i key.pem -P 3912 admin#myServerIP:home/files/
which may make more sense in this case. But you will very likely find the bash -c ... approach useful for other things in the future.
I am trying to copy several files from a remote server into local drive in Bash using scp.
Here's the part of the code
scp -r -q $USR#$IP:/home/file1.txt $PWD
scp -r -q $USR#$IP:/home/file2.txt $PWD
scp -r -q $USR#$IP:/root/file3.txt $PWD
However, the problem is that EVERY time that it wants to copy a file, it keeps asking for the password of the server, which is the same. I want it to ask only once and then copy all my files.
And please, do not suggest rsync nor making a key authentication file since I do not want to do that.
Are there any other ways...?
Any help would be appreciated
You can use expect script or sshpass
sshpass -p 'password' scp ...
#!/usr/bin/expect -f
spawn scp ...
expect "password:"
send "ur_password"
An disadvantage is that your password is now in plaintext
I'm assuming that if you can scp files from the remote server, you can also ssh in and create a tarball of the remote files.
The -r flag is recursive, for copying entire directories but your listing distinct files in your command, so -r becomes superfluous.
Try this from the bash shell on the remote system:
$ mkdir /home/file_mover
$ cp /home/file1.txt /home/file_mover/
$ cp /home/file2.txt /home/file_mover/
$ cp /root/file3.txt /home/file_mover/
$ tar -cvf /home/myTarball.tar /home/file_mover/
$ scp -q $USR#$IP:/home/myTarball.tar $PWD
Well, in this particular case, you can write...
scp -q $USR#$IP:/home/file[1-3].txt $PWD