简体   繁体   中英

Remotely running commands via ssh

I need to automate the copy of a zip file to a remote Linux machine and then the unzipping of that file to a user's home directory.

Let's assume we have user1 and user2, user1 is a real person but has no home directory and user2 is an application user that has a home directory but cannot directly get shell access to a host. The mechanism to gain a shell for user2 is to ssh to the box as user1 and then su to user2. (please do not pass comment on this setup as I work for a large corporation and I am unable to change this aspect, it is decided by IT security and not up for discussion).

I would like to use

scp ziptocopy.zip user1@hostname:/var/tmp/

but as I don't have anywhere on the remote host to store a key file for user1 I cannot us public/private key pairs to perform this, can anyone suggest a way to do this?

The next piece is even more tricky as I want to ssh as user1 and then su to user2 and run

unzip /var/tmp/ziptocopy.zip

Again any suggestion on how I can do this? I have done a search and found an example that uses expect, this has potential for the scp but I cannot get this to work, but how would I get expect to cope with 2 password prompts?

Thanks

I'd look into using Python and the pexpect/pxssh modules. These days pexpect is a nice alternative for automation tasks that used to be dealt with using Expect.

See this answer for some example code where an SSH session is scripted:

How to make a ssh connection with python?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM