call "ssh" in system function in perl - linux

i have a question related to connecting via ssh with "system()" function in perl.
in my perl script i want to connect via ssh to another ip, run a command and return its value or redirect result value to a file.
system ("su - anotherUSer ; ssh someUsername#someIpAddress");
(i change my username so that i am not asked for a password)
when i execute this only line it changes the username correctly but not connects via ssh. In other words, the second part of the system call is not done (or is done but not reflected on the terminal).
If i enter mannualy to the server where this script executes and run this two commands, i can run them without errors.
When i run "exit" command to logout my anotherUser user an error raises:
(ssh: "username"."ip": node name or service name not known)
I also tested it escaping '#' and '.'
system ("su - anotherUSer ; ssh someUsername\#number\.number\.number\.number");
in this case when i run the "exit" command, it askes for the password(Remember that i swiched users so that password could be ommited).
I hope you understand my problem.
Thanks!!!

You're telling the wrong shell to execute ssh.
You spawn a shell and ask it to execute two commands, su and ssh. It first spawns su, which launches another shell. You didn't tell this new shell to do anything, so it waits for input. When it finally exits, the first shell executes the second command, ssh.
Use:
system("su -c 'ssh someUsername#someIpAddress' - anotherUSer");
But that's nasty! Why not just set up a key for the current user instead of becoming anottherUSer to use theirs?

Related

How to ssh login a system with exit profile

When I add the script snippet "exit 0" in profile, I can not ssh to this linux system any more.
How to fix it.
The actual answer is that you can ssh to that machine, however when doing an ssh it reads your profile where it reads directly exit 0 so it cleanly terminates your ssh session.
If you do something like :
ssh user#machine
Then ssh will try to invoke a login-shell. Assuming your default shell is bash, then you find the following in man bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it
looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
So since you have an exit statement in one of those files, your ssh session will terminate immediately.
how to solve it: remove the exit statement.

How to run shell script in another server in a script?

No where online can i find a way to run a shell script on a remote server from another script. This is for automation, so the script on the host machine will automatically trigger another script on a different server. The server that my script will ssh to will either have a password prompt or have RSA key pair set up
Thanks!
Just pass the command as an argument to ssh.
ssh someserver /path/to/some/script.bsh
Let's say you want to execute a script on node2 but you have your script on node1 file name of script is sp over location /home/user/sp. Simply
ssh node2 < /path-of-the-script-including-the-filename
Another way, using expect package.
Disclaimer: This you can use for testing environments since it has an
open password. but depends on your usecase
If your server does not have expect, you may add the package then. run the command. You can also put this command inside an .sh script.
expect -c 'spawn ssh user#10.11.12.13 "/path/to/my.sh"; expect "assword:"; send "Y0urp#ssw0rd\r"; interact'

Can you prompt for user input in a shell script that is running remotely?

Say I have a script that will be run on a remote machine.
While running, the script computes some value.
I want to prompt the user so she can change this value if needed.
Is this possible?
I am running the script like: ssh $usr#$machine 'bash -s' < a.sh "param1" "param2"
In a.sh the read alternateValue function call seems to be ignored.
Or can anyone suggest a different approach?
The read statement reads data from stdin, but you are redirecting stdin in your command line with the < operator, so read isn't going to do anything useful.
What if you were first to copy the script over to the remote host, and then run:
ssh $usr#$machine 'bash /path/to/a.sh param1 param2'
Because there is no redirection happening here, read would work without a problem.

Shell script - SSH

I am using shell script to add some file to server. Is there any way to write a shell script that will execute one part on local computer and the other part when you're logged into that server?
For example, I want to log in, do something, add some file, and then I want to list everything on that server.
ssh something#something
I enter password
Then list files from server.
You can just add a command to end of the ssh command; for example:
ssh username#host ls
will run ls on the server, instead of giving you a login shell.

Run bash script upon ssh

I have a host on which I created a script .
The script is being executed whenever the user is logging in via ssh bashrc launches the script.
Now I'm trying to get the script to execute even if the user is not actually logging in , and just running a command .
For example I want the script to be executed if a user is running the following :
ssh user#host.com some_command
Is there a way to achieve the above?
A solution affecting all the users could be using pam-exec and launch a script on the user login event. Check the pam-exec manual page and an example on how to use it pam-exec scripting.
A simple solution for a single user should be add the script in the rc file of the ssh user, add your script to:
~/.ssh/rc
I've done some tests and the rc solution works fine in your case, it gets executed when the user launches a remote command via ssh.
If you don't have a rc file just create it.
you can edit authorized_keys file and add a COMMAND , something like :
command="/home/michale/bin/dothis.sh" ...public key...
for more details read ssh and authorized_keys documentations.

Resources