copy pscp a file from window server to to remote linux server - linux

I want to copy a directory (pscp) from windows server to Linux server
The target place name on Linux server has to be new each time.When i run the below command,
> pscp -p -l root
> -pw mypassword -r C:\ProgramFiles\Mybackups\root#linux_server:/root/mywindowsbackups/$(date)
The command substitution $(date) doesn't work.
can any one suggest how would i run this ?

Try the following:
:: This is stripping the `/` and `Day of the Week` from the date
set target_date=%date:/=-%
set target_date=%target_date:* =%
:: Copying the directory to the linux server based on this system's date
pscp -p -l root
-pw mypassword -r C:\ProgramFiles\Mybackups\root#linux_server:/root/mywindowsbackups/%target_date%

Related

How to remotely SCP latest file in one server to another?

I have 3 Linux machines, namely client, server1 and server2.
I am trying to achieve something like this. I would like to copy the latest file in a particular directory of server1 to server2. I will not be doing this by logging into server1 directly, but I always log on to client machine first. Let me list down the step by step approach which is happening now:
Log on to client machine using ssh
SSH into server1
Go to directory /home/user1 in server1 using the command ls /home/user1 -Art | tail -n 1
SCP the latest file to /home/user2 directory of server2
This manual operation is happening just fine. I have automated this using a one line script like below:
ssh user1#server1 scp -o StrictHostKeyChecking=no /home/user1/test.txt user2#server2:/home/user2
But as you can see, I am uploading the file /home/user1/test.txt. How can I modify this script to always upload the latest file in the directory /home/user1?
If zsh is available on server1, you could use its advanced globbing features to find the most recent file to copy:
ssh user1#server1 \
"zsh -c 'scp -o StrictHostKeyChecking=no /home/user1/*(om[1]) user2#server2:/home/user2'"
The quoting is important in case your remote shell on server1 is not zsh.
You can use SSH to list the last file and after user scp to copy the file as follow:
FILE=$(ssh user1#server1 "ls -tp $REMOTE_DIR |grep -v / | grep -m1 \"\""); scp user1#server1:$FILE user2#server2:/home/user2
Before launch these command you need to set the remote directory where to search for the last modified file as:
REMOTE_DIR=/home/user1

How can I get ls commands to output results in columns when using plink.exe

Using plink in cmd to ssh my MacOS and ls the dir's. However, the results are in a single column. Because the list is long but the names are short, I'm trying to display the results in 3 or 4 columns like ls does by default in terminal. I can't get it to do this when using plink.
Few options I've tried:
plink.exe -ssh -l <username> -pw <password> <IP> "ls /Users/Dir/"
plink.exe -ssh -l <username> -pw <password> <IP> "ls -c /Users/Dir/"
plink.exe -ssh -l <username> -pw <password> <IP> "ls /Users/Dir/ > Results.txt"
plink.exe -ssh -l <username> -pw <password> <IP> "column Results.txt"
Current results using plink with CMD
Folder 1
folder 2
folder 3
folder 4
folder 5
folder 6
Looking to get this result using plink with CMD
folder 1 folder 2 folder 3
folder 4 folder 5 folder 6
folder 7 folder 8 folder 9
When you use commands in a session without a terminal, they tend to produce output in machine-readable deterministic format.
For ls, that means one file per line, no coloring, etc.
When you execute a command using plink (or OpenSSH ssh) from its command-line, it does not allocate a pseudo terminal for the session. So the ls (and other commands) behave like you experience.
If you want the commands to behave like in the terminal, add -t switch to its command line to force the use of the pseudo terminal.
plink.exe -t -ssh -l <username> -pw <password> <IP> "ls /Users/Dir/"
Though if your goal is to parse the output, do not do that, as the format might change with a change to the environment and your code will break. You can also get lot of other side effects, like ANSI escape codes, pagination, etc. You better stick with the default format.
More generic question about executing commands with Plink:
Script via Plink in .bat behaves differently
Found a way around this by using 'pr' from winGnu. Switched the plink command to output the results to a file on the PC and pr to column it in cmd.
mode con:cols=180 lines=100
echo. > Results.txt
plink.exe -t -ssh -l <username> -pw <pass> <IP> "ls /Users/Dir/" > Results.txt
pr -3 -t -w180 Results.txt
del Results.txt

Assign contents of file to variable over remote ssh from a script running in Jenkins

I have opened a remote ssh session from a script and on remote server there is a file containing version information.
I am trying to assign that version to variable and move current version contents to folder name same as version.
The main script is running in jenkins
I am doing something like this
ssh -i /home/user/.ssh/id_rsa -t -t remoteServer<<EOF
cd $WEB_DIR
VERSION=$(cat $WEB_DIR/version.info)
mv -f $WEB_DIR $BACKUP_DIR/$VERSION
exit
EOF
My VERSION variable is always empty. When I run same locally on that server it gives me version value. Something is different over remote ssh session within a script
Actually I found the way to do it in 2 steps.
$WEB_DIR is set as local variable set in main script.
$WEB_DIR="/usr/local/tomcat/webapps/ROOT"
OLD_VERSION=$(ssh -i /home/user/.ssh/id_rsa -tt user#remoteServer "cat $WEB_DIR/version.info")
ssh -i /home/user/.ssh/id_rsa -t -t user#remoteServer<<EOF
cd $WEB_DIR
mv -f $WEB_DIR $BACKUP_DIR/$OLD_VERSION
# I am executing more commands in here
exit
EOF
Use of double quotes "" in first command is must if want to use local variable.

How can I copy file from local server to remote with creating directories which absent via SSH?

I can copy file via SSH by using SCP like this:
cd /root/dir1/dir2/
scp filename root#192.168.0.19:$PWD/
But if on remote server some directories are absent, in example remote server has only /root/ and havn't dir1 and dir2, then I can't do it and I get an error.
How can I do this - to copy file with creating directories which absent via SSH, and how to make it the easiest way?
The easiest way mean that I can get current path only by $PWD, i.e. script must be light moveable without any changes.
This command will do it:
rsync -ahHv --rsync-path="mkdir -p $PWD && rsync" filename -e "ssh -v" root#192.168.0.19:"$PWD/"
I can make the same directories on the remote servers and copy file to it via SSH by using SCP like this:
cd /root/dir1/dir2/
ssh -n root#192.168.0.19 "mkdir -p '$PWD'"
scp -p filename root#192.168.0.19:$PWD/

cygwin ssh batch script for windows 2008

I configured cygwin in Windows Server 2008, now we need to implement automation
I am writing a batch script to add user to cygwin\etc\passwd file using following command
mkpasswd -l -u %username% -p /home >> /etc/passwd
Please help me how to execute following cmd in batch file
echo off
C:
chdir C:\cygwin\bin
bash --login -i
mkpasswd -l -u %username% -p /home >> /etc/passwd
It's not working
You're mixing Windows and Unix in your windows batch file. The batch file is running as a Windows command, as is the mkpasswd command in it. Windows has no concept of /etc/passwd and will throw an error. Probably something like;
D:\cygwin\bin>mkpasswd -l -u testusr -p /home >> /etc/passwd
The system cannot find the path specified.
Given what you want to do with mkpasswd I'd suggest you find a way to run your automation from within Cygwin. Perhaps setting up a cron job.

Resources