为SSH生成子shell,并继续执行程序流程 [英] Spawn subshell for SSH and continue with program flow

查看:100
本文介绍了为SSH生成子shell,并继续执行程序流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个shell脚本,该脚本根据我的位置(home/campusA/campusB)自动执行某些启动任务.我上大学,然后在两个不同的校园(因此称为CampusA/campusB)上课.我的位置取决于我连接的无线网络.出于此脚本的目的,我们可以假设调用脚本时我将连接到这些网络之一,并且我的脚本基于对iwconfig的调用知道我连接的是哪个网络.

I'm trying to write a shell script that automates certain startup tasks based on my location (home/campusA/campusB). I go to University and take classes in two different campuses (hence campusA/campusB). My location is determined by which wireless network I'm connected to. For the purposes of this script, we can assume that I will be connected to one of these networks when the script is called and my script knows which one I'm connected to based on a call to iwconfig.

这就是我想要的:

cat file1 > file2 # always do this, regardless of where I am
if Im at home:
    start tweetdeck, thunderbird, skype

else if Im at campusA:
    activate the login script # I need to login on a webform before I get internet access. 
                              # I have written a script to automate this. 
                              # Wait for this script to finish before doing anything else
    myProg2 & # I want myProg2 running in the background until I shutdown my computer.

else if Im at campusB:
    ssh username@domain # this is the problematic line
    myProg2 & # I want myProg2 running in the background until I shutdown my computer.

start tweetdeck, thunderbird
close the terminal with the "exit" command

问题在于,campusB的无线网络位于防火墙后面,只有在我成功按username@domain进行ssh后,该防火墙才能授予我Internet访问权限.在成功执行ssh之后,我需要保持终端窗口处于活动状态才能保持互联网访问.如果关闭终端窗口,则无法访问互联网(这很糟糕).

The problem is that campusB's wireless network is behind a firewall, which grants me internet access ONLY after I successfully ssh by username@domain. After a successful ssh, I need to keep the terminal window active in order to hold keep the internet access. If I close the terminal window, I lose internet access (this is bad).

当我尝试仅执行ssh username@domain时,脚本停止,因为我没有退出ssh命令.我不能^C退出,这意味着脚本的其余部分将永远不会执行.如果我只是关闭终端窗口以终止ssh会话,我也会遇到同样的问题.

When I try doing just ssh username@domain, the script stops because I don't exit the ssh command. I can't ^C out of it, which means that the rest of the script is never executed. I also have the same problem if I just close the terminal window in an attempt to kill the ssh session.

有些谷歌搜索将我带到 subshell 使用错误或无法使用来解决我的问题.那么我该如何解决这个问题呢?我将不胜感激-我已经在这里工作了一段时间,无法找到任何有用的信息.如果有所作为,我宁愿不将我的ssh密码存储在脚本中

Some googling brought me to subshell, which I'm either using wrong or can't use to solve my problem. So how should I go about solving this problem? I'd appreciate any help - I've been at this for a while now and am unable to find anything helpful. If it makes a difference, I'd rather not store my ssh password in the script

进一步,对< c3>调用(< c9>)似乎没有任何作用(有人可以解释为什么吗?)

Further, ampersanding the ssh call (ssh username@domain &) doesn't seem to do any good (can anyone explain why?)

提前谢谢

编辑

我必须澄清,ssh连接必须处于活动状态才能访问Internet.因此,当我关闭终端窗口时,我需要ssh连接保持活动状态.

I must clarify, that the ssh connection has to be active in order for me to have internet access. Thus, when I close the terminal window, I need the ssh connection to still be active.

推荐答案

我有一个脚本在6台服务器上循环运行,并在后台通过ssh调用.在脚本的一部分中,有一个行为异常的供应商应用程序;应用程序未正确放开"连接. (脚本的其他部分在后台使用ssh效果很好).

I had a script that looped on 6 servers, calling via ssh in the background. In 1 part of the script, there was a mis-behaving vendor application; the application didn't 'let go' of the connection properly. (other parts of the script using ssh in background worked fine).

我发现使用ssh -t -t解决了该问题.也许这也可以帮助您. (一个队友在网上找到了这个,我们花了很多时间,我再也没有回过头来建议这个问题.我们系统上的手册页没有暗示这样的事情是可能的)

I found that using ssh -t -t cured the problem. Maybe this can help you too. (a teammate found this on the web, and we had spent so much time, I never went back to read the article that suggested this. The man page on our system gave no hint that such a thing was possible)

希望这会有所帮助.

这篇关于为SSH生成子shell,并继续执行程序流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆